From 4fbc3402fba932c8bd34b671527dcf7909d264c0 Mon Sep 17 00:00:00 2001 From: Laurence Jones Date: Fri, 26 May 2023 15:35:46 +0100 Subject: [PATCH] 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 --- pkg/exprhelpers/exprlib_test.go | 12 ++++++++++++ pkg/exprhelpers/helpers.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/exprhelpers/exprlib_test.go b/pkg/exprhelpers/exprlib_test.go index d45338064..53f7d7d15 100644 --- a/pkg/exprhelpers/exprlib_test.go +++ b/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 { diff --git a/pkg/exprhelpers/helpers.go b/pkg/exprhelpers/helpers.go index 4a1404304..2a0c9fd31 100644 --- a/pkg/exprhelpers/helpers.go +++ b/pkg/exprhelpers/helpers.go @@ -51,7 +51,7 @@ var dbClient *database.Client var exprFunctionOptions []expr.Option -var keyValuePattern = regexp.MustCompile(`\s*(?P[^=\s]+)\s*=\s*(?:"(?P[^"\\]*(?:\\.[^"\\]*)*)"|(?P[^=\s]+))`) +var keyValuePattern = regexp.MustCompile(`(?P[^=\s]+)=(?:"(?P[^"\\]*(?:\\.[^"\\]*)*)"|(?P[^=\s]+)|\s*)`) func GetExprOptions(ctx map[string]interface{}) []expr.Option { ret := []expr.Option{}