Skip to content

Commit

Permalink
Allow square brackets in variables (macro expansion) (#1226)
Browse files Browse the repository at this point in the history
Allow square brackets in variables

Co-authored-by: Yehor Komarov <ekomarov@ivi.ru>
Co-authored-by: Matteo Pace <pace.matteo96@gmail.com>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent 244ba00 commit 606a1bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion experimental/plugins/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 26 additions & 14 deletions experimental/plugins/macro/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &macro{}
err := m.compile(test)
Expand Down Expand Up @@ -118,19 +121,28 @@ func TestCompile(t *testing.T) {
})

t.Run("valid macro", func(t *testing.T) {
m := &macro{}
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 := &macro{}
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])
}
}
})

Expand Down

0 comments on commit 606a1bb

Please sign in to comment.