From 58f8cade1fa80ca0d59947c1ebd965c4c215ab71 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Tue, 12 Nov 2024 11:59:29 +0200 Subject: [PATCH] fix(variables): Add number suffix support to the variable extractor Signed-off-by: Suleiman Dibirov --- template/variables.go | 3 +++ template/variables_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/template/variables.go b/template/variables.go index fb32f9c3..84edee59 100644 --- a/template/variables.go +++ b/template/variables.go @@ -108,6 +108,9 @@ func extractVariable(value interface{}, pattern *regexp.Regexp) ([]Variable, boo if r >= 'A' && r <= 'Z' { return false } + if r >= '0' && r <= '9' { + return false + } if r == '_' { return false } diff --git a/template/variables_test.go b/template/variables_test.go index 00133d5a..7239b016 100644 --- a/template/variables_test.go +++ b/template/variables_test.go @@ -50,6 +50,15 @@ func TestExtractVariables(t *testing.T) { "bar": {Name: "bar"}, }, }, + { + name: "variable-without-curly-braces-and-with-number-suffix", + dict: map[string]interface{}{ + "foo": "$bar_1", + }, + expected: map[string]Variable{ + "bar_1": {Name: "bar_1"}, + }, + }, { name: "variable", dict: map[string]interface{}{ @@ -68,6 +77,15 @@ func TestExtractVariables(t *testing.T) { "bar": {Name: "bar", DefaultValue: "", Required: true}, }, }, + { + name: "required-variable-with-number-suffix", + dict: map[string]interface{}{ + "foo": "${bar_1?:foo}", + }, + expected: map[string]Variable{ + "bar_1": {Name: "bar_1", DefaultValue: "", Required: true}, + }, + }, { name: "required-variable2", dict: map[string]interface{}{ @@ -95,6 +113,24 @@ func TestExtractVariables(t *testing.T) { "bar": {Name: "bar", DefaultValue: "foo"}, }, }, + { + name: "default-variable-with-number-suffix", + dict: map[string]interface{}{ + "foo": "${bar_1:-foo}", + }, + expected: map[string]Variable{ + "bar_1": {Name: "bar_1", DefaultValue: "foo"}, + }, + }, + { + name: "default-variable2-with-number-suffix", + dict: map[string]interface{}{ + "foo": "${bar_1-foo}", + }, + expected: map[string]Variable{ + "bar_1": {Name: "bar_1", DefaultValue: "foo"}, + }, + }, { name: "multiple-values", dict: map[string]interface{}{