Quellcode durchsuchen

Update KV ignore whitespace before and after `=` (#2236)

* Update KV ignore whitespace before and after `=`

* Update helpers.go

Don't need whitespace infront of KEY

* Add some tests to ensure edge cases

* Ensure quoted and unquoted values act the same
Laurence Jones vor 2 Jahren
Ursprung
Commit
4fbc3402fb
2 geänderte Dateien mit 13 neuen und 1 gelöschten Zeilen
  1. 12 0
      pkg/exprhelpers/exprlib_test.go
  2. 1 1
      pkg/exprhelpers/helpers.go

+ 12 - 0
pkg/exprhelpers/exprlib_test.go

@@ -1399,6 +1399,18 @@ func TestParseKv(t *testing.T) {
 			expected: map[string]string{"foo": "bar=toto"},
 			expr:     `ParseKV(value, out, "a")`,
 		},
+		{
+			name:     "ParseKV() test: empty unquoted string",
+			value:    `foo= bar=toto`,
+			expected: map[string]string{"bar": "toto", "foo": ""},
+			expr:     `ParseKV(value, out, "a")`,
+		},
+		{
+			name:     "ParseKV() test: empty quoted string ",
+			value:    `foo="" bar=toto`,
+			expected: map[string]string{"bar": "toto", "foo": ""},
+			expr:     `ParseKV(value, out, "a")`,
+		},
 	}
 
 	for _, tc := range tests {

+ 1 - 1
pkg/exprhelpers/helpers.go

@@ -51,7 +51,7 @@ var dbClient *database.Client
 
 var exprFunctionOptions []expr.Option
 
-var keyValuePattern = regexp.MustCompile(`\s*(?P<key>[^=\s]+)\s*=\s*(?:"(?P<quoted_value>[^"\\]*(?:\\.[^"\\]*)*)"|(?P<value>[^=\s]+))`)
+var keyValuePattern = regexp.MustCompile(`(?P<key>[^=\s]+)=(?:"(?P<quoted_value>[^"\\]*(?:\\.[^"\\]*)*)"|(?P<value>[^=\s]+)|\s*)`)
 
 func GetExprOptions(ctx map[string]interface{}) []expr.Option {
 	ret := []expr.Option{}