rsyslog / liblognorm

a fast samples-based log normalization library
http://www.liblognorm.com
GNU Lesser General Public License v2.1
99 stars 64 forks source link

Repeat - Fix multiple patterns with one name #369

Open KGuillemot opened 1 year ago

KGuillemot commented 1 year ago

Actual behavior

Actually, when using the following rulebase :

version=2
rule=:%
        {"name":"numbers", "type":"repeat",
            "parser":[
                       {"type":"number"},
                       {"type":"literal", "text":":"},
                       {"type":"number", "name": "."}
                     ],
            "while":[
                       {"type":"literal", "text":", "}
                    ]
         }%

And the following line to parse :

1:2, 3:4, 5:6, 7:8

I get the following error :

$ echo '1:2, 3:4, 5:6, 7:8' | lognormalizer -r ./test.rb
liblognorm error: rulebase file ./test.rb[12]: 'repeat' parser supports dot name only if single parser is used in 'parser' part, invalid construct: [ { "type": "number" }, { "type": "literal", "text": ":" }, { "type": "number", "name": "." } ]
liblognorm error: rulebase file ./test.rb[12]: repeat parser needs 'parser','while' parameters
Erreur de segmentation

The error 'repeat' parser supports dot name only if single parser is used should be raised only if 2 parsers have a name configured.

Fixed behavior

After fixing, when using the rule and line listed above, the error does not raise and the parsing behavior is as expected.

$ echo '1:2, 3:4, 5:6, 7:8' | lognormalizer -r ./test.rb
{ "numbers": [ "2", "4", "6", "8" ] }

Non-regression tests

After fix, the error correctly raises when multiple parsers use a name :

version=2
rule=:%
        {"name":"numbers", "type":"repeat",
            "parser":[
                       {"type":"number", "name": "n1"},
                       {"type":"literal", "text":":"},
                       {"type":"number", "name": "."}
                     ],
            "while":[
                       {"type":"literal", "text":", "}
                    ]
         }%
$ echo '1:2, 3:4, 5:6, 7:8' | lognormalizer -r ./test.rb
liblognorm error: rulebase file ./test.rb[12]: 'repeat' parser supports dot name only if single parser is used in 'parser' part, invalid construct: [ { "type": "number", "name": "n1" }, { "type": "literal", "text": ":" }, { "type": "number", "name": "." } ]
liblognorm error: rulebase file ./test.rb[12]: repeat parser needs 'parser','while' parameters
Erreur de segmentation