From b5e8b52dee9daaa1690886d24c27b2b2e8f9e0c0 Mon Sep 17 00:00:00 2001 From: Oleh Fedorenko Date: Thu, 27 Jul 2023 13:57:24 +0000 Subject: [PATCH] Fixes #36258 - Improve regex for KeyValueList normalizer --- lib/hammer_cli/options/normalizers.rb | 7 ++----- test/unit/options/normalizers_test.rb | 6 +++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/hammer_cli/options/normalizers.rb b/lib/hammer_cli/options/normalizers.rb index 65beb627..47a6b1d9 100644 --- a/lib/hammer_cli/options/normalizers.rb +++ b/lib/hammer_cli/options/normalizers.rb @@ -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 @@ -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 @@ -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(/[^,\[\]]+/) diff --git a/test/unit/options/normalizers_test.rb b/test/unit/options/normalizers_test.rb index 577480ec..61f8bb85 100644 --- a/test/unit/options/normalizers_test.rb +++ b/test/unit/options/normalizers_test.rb @@ -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 @@ -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