From 4bbf5df7dc56ef134b5915e7cd16c2ee72a615c9 Mon Sep 17 00:00:00 2001 From: Ben Meier Date: Tue, 9 Jul 2024 10:43:34 +0100 Subject: [PATCH] chore(placeholders): refactor matching capture and add regression test --- framework/substitution.go | 4 ++-- framework/substitution_test.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/framework/substitution.go b/framework/substitution.go index 6fa83af..548f51f 100644 --- a/framework/substitution.go +++ b/framework/substitution.go @@ -24,7 +24,7 @@ import ( var ( // placeholderRegEx will search for ${...} with any sequence of characters between them. - placeholderRegEx = regexp.MustCompile(`\$(\$|{([^}]*)})`) + placeholderRegEx = regexp.MustCompile(`\$((?:\$?{([^}]*)})|\$)`) ) func SplitRefParts(ref string) []string { @@ -52,7 +52,7 @@ func SubstituteString(src string, inner func(string) (string, error)) (string, e } // support escaped dollars - if matches[1] == "$" { + if strings.HasPrefix(matches[1], "$") { return matches[1] } diff --git a/framework/substitution_test.go b/framework/substitution_test.go index 0e54982..6eabdaa 100644 --- a/framework/substitution_test.go +++ b/framework/substitution_test.go @@ -108,6 +108,9 @@ func TestSubstituteString(t *testing.T) { {Input: "$abc", Expected: "$abc"}, {Input: "abc $$ abc", Expected: "abc $ abc"}, {Input: "$${abc}", Expected: "${abc}"}, + {Input: "$$${abc}", ExpectedError: "invalid ref 'abc': unknown reference root, use $$ to escape the substitution"}, + {Input: "$$$${abc}", Expected: "$${abc}"}, + {Input: "$$$$${abc}", ExpectedError: "invalid ref 'abc': unknown reference root, use $$ to escape the substitution"}, {Input: "$${abc .4t3298y *(^&(*}", Expected: "${abc .4t3298y *(^&(*}"}, {Input: "my name is ${metadata.name}", Expected: "my name is test-name"}, {Input: "my name is ${metadata.thing\\.two}", ExpectedError: "invalid ref 'metadata.thing\\.two': key 'thing.two' not found"},