Skip to content

Commit

Permalink
Fixes #36258 - Improve regex for KeyValueList normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren authored and adamruzicka committed Jul 28, 2023
1 parent 761d84a commit b5e8b52
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 2 additions & 5 deletions lib/hammer_cli/options/normalizers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def format(value)
end

class KeyValueList < AbstractNormalizer

PAIR_RE = '([^,=]+)=([^,\{\[]+|[\{\[][^\{\}\[\]]*[\}\]])'
FULL_RE = "^((%s)[,]?)+$" % PAIR_RE
FULL_RE = '([^=,]+)=([\{\[][^\{\}\[\]]*[\}\]]|[^\0]+?)(?=,[^,]+=|$)'

class << self
def completion_type
Expand All @@ -68,7 +66,6 @@ def common_description
def format(val)
return {} unless val.is_a?(String)
return {} if val.empty?

if valid_key_value?(val)
parse_key_value(val)
else
Expand All @@ -89,7 +86,7 @@ def valid_key_value?(val)

def parse_key_value(val)
result = {}
val.scan(Regexp.new(PAIR_RE)) do |key, value|
val.scan(Regexp.new(FULL_RE)) do |key, value|
value = value.strip
if value.start_with?('[')
value = value.scan(/[^,\[\]]+/)
Expand Down
6 changes: 5 additions & 1 deletion test/unit/options/normalizers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@
end

describe 'key=value format' do
it 'should parse values with commas' do
_(formatter.format('a=1,2,b=3,4')).must_equal({ 'a' => '1,2', 'b' => '3,4'})
end

it "should parse a comma separated string" do
_(formatter.format("a=1,b=2,c=3")).must_equal({'a' => '1', 'b' => '2', 'c' => '3'})
end
Expand Down Expand Up @@ -236,7 +240,7 @@
end

it "should parse a comma separated string 2" do
_{ formatter.format("a=1,b,c=3") }.must_raise ArgumentError
_(formatter.format("a=1,b,c=3")).must_equal({ 'a' => '1,b', 'c' => '3' })
end

it 'should parse explicit strings' do
Expand Down

0 comments on commit b5e8b52

Please sign in to comment.