From 606a1bbe5a48782fef22c9db2dae5688c9638ccf Mon Sep 17 00:00:00 2001 From: George Komarov Date: Thu, 21 Nov 2024 20:15:04 +0300 Subject: [PATCH] Allow square brackets in variables (macro expansion) (#1226) Allow square brackets in variables Co-authored-by: Yehor Komarov Co-authored-by: Matteo Pace --- experimental/plugins/macro/macro.go | 2 +- experimental/plugins/macro/macro_test.go | 40 +++++++++++++++--------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/experimental/plugins/macro/macro.go b/experimental/plugins/macro/macro.go index fabd039d3..7f5c01cb1 100644 --- a/experimental/plugins/macro/macro.go +++ b/experimental/plugins/macro/macro.go @@ -166,7 +166,7 @@ func (m *macro) compile(input string) error { } func isValidMacroChar(c byte) bool { - return c == '.' || c == '_' || c == '-' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') + return c == '[' || c == ']' || c == '.' || c == '_' || c == '-' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') } // String returns the original string diff --git a/experimental/plugins/macro/macro_test.go b/experimental/plugins/macro/macro_test.go index 7ea662df5..d80c0eb31 100644 --- a/experimental/plugins/macro/macro_test.go +++ b/experimental/plugins/macro/macro_test.go @@ -69,7 +69,10 @@ func TestCompile(t *testing.T) { }) t.Run("malformed macros", func(t *testing.T) { - for _, test := range []string{"%{tx.count", "%{{tx.count}", "%{{tx.{count}", "something %{tx.count"} { + for _, test := range []string{ + "%{tx.count", "%{{tx.count}", "%{{tx.{count}", "something %{tx.count", + "%{ARG_NAMES:/exec/", // Wildcard variable names are not supported + } { t.Run(test, func(t *testing.T) { m := ¯o{} err := m.compile(test) @@ -118,19 +121,28 @@ func TestCompile(t *testing.T) { }) t.Run("valid macro", func(t *testing.T) { - m := ¯o{} - err := m.compile("%{tx.count}") - if err != nil { - t.Fatalf("unexpected error: %s", err.Error()) - } - - if want, have := 1, len(m.tokens); want != have { - t.Fatalf("unexpected number of tokens: want %d, have %d", want, have) - } - - expectedMacro := macroToken{"tx.count", variables.TX, "count"} - if want, have := m.tokens[0], expectedMacro; want != have { - t.Errorf("unexpected token: want %v, have %v", want, have) + type testCase struct { + input string + expectedMacro macroToken + } + for _, tc := range []testCase{ + {"%{tx.count}", macroToken{"tx.count", variables.TX, "count"}}, + {"%{ARGS.exec}", macroToken{"ARGS.exec", variables.Args, "exec"}}, + {"%{ARGS_GET.db[]}", macroToken{"ARGS_GET.db[]", variables.ArgsGet, "db[]"}}, + } { + m := ¯o{} + err := m.compile(tc.input) + if err != nil { + t.Fatalf("unexpected error: %s", err.Error()) + } + + if len(m.tokens) != 1 { + t.Fatalf("unexpected number of tokens: want %d, have %d", 1, len(m.tokens)) + } + + if m.tokens[0] != tc.expectedMacro { + t.Errorf("unexpected token: want %v, have %v", tc.expectedMacro, m.tokens[0]) + } } })