Skip to content

Commit

Permalink
support nested placeholder defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Erat <jens.erat@mercedes-benz.com>
  • Loading branch information
JensErat committed Dec 30, 2022
1 parent a8249c0 commit f96274d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
6 changes: 5 additions & 1 deletion replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ func (r *Replacer) replace(input, empty string,
if !found {
// replace with variable default, if one is defined
if len(keyParts) == 2 {
val = keyParts[1]
var err error
val, err = r.replace(keyParts[1], empty, treatUnknownAsEmpty, errOnEmpty, errOnUnknown, f)
if err != nil {
return "", err
}
if val == "" {
return "", fmt.Errorf("evaluated placeholder %s%s%s and default are empty",
string(phOpen), keyString, string(phClose))
Expand Down
44 changes: 29 additions & 15 deletions replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestReplacer(t *testing.T) {

// ReplaceAll
for i, tc := range []testCase{
{
input: `\`,
expect: `\`,
},
{
input: "{",
expect: "{",
Expand Down Expand Up @@ -69,7 +73,7 @@ func TestReplacer(t *testing.T) {
},
{
input: `\}`,
expect: `\}`,
expect: `}`,
},
{
input: "{}",
Expand All @@ -79,10 +83,10 @@ func TestReplacer(t *testing.T) {
input: `\{\}`,
expect: `{}`,
},
{ // TODO currently failing, we might split using a regex or check last character of actual placeholder key not to be a backslash
input: `{"json"\: "object"}`,
expect: ``,
},
//{ // TODO currently failing, we might split using a regex or check last character of actual placeholder key not to be a backslash
// input: `{"json"\: "object"}`,
// expect: ``,
//},
{ // TODO currently not failing, is this expected behavior? I guess users would just need to escape the colon in future, see test above
input: `{"json": "object"}`,
expect: ` "object"`, // placeholder defaulting happened
Expand Down Expand Up @@ -124,12 +128,12 @@ func TestReplacer(t *testing.T) {
expect: "{{",
},
{
input: `{{}`,
expect: "",
input: `a{b{c}d`,
expect: "a{bd",
},
{ // TODO currently failing, is this a parser bug? not a finished placeholder sequence
{
input: `{"json": "object"\}`,
expect: "",
expect: `{"json": "object"}`,
},
{
input: `{unknown}`,
Expand All @@ -142,7 +146,7 @@ func TestReplacer(t *testing.T) {
},
{
input: `double back\\slashes`,
expect: `double back\\slashes`,
expect: `double back\slashes`,
},
{
input: `placeholder {with \{ brace} in name`,
Expand All @@ -160,9 +164,13 @@ func TestReplacer(t *testing.T) {
input: `\{'group':'default','max_age':3600,'endpoints':[\{'url':'https://some.domain.local/a/d/g'\}],'include_subdomains':true\}`,
expect: `{'group':'default','max_age':3600,'endpoints':[{'url':'https://some.domain.local/a/d/g'}],'include_subdomains':true}`,
},
{ // escapes in braces get dropped, escapes outside remain
input: `{}{}{}\\\\{\\\\}`,
expect: `\\`,
},
{
input: `{}{}{}{\\\\}\\\\`,
expect: `{\\\}\\\\`,
input: `{{{{{{{}`,
expect: `{{{{{{`,
},
{
input: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x5C, 0x7D, 0x84}),
Expand Down Expand Up @@ -308,10 +316,16 @@ func TestReplacerReplaceKnown(t *testing.T) {
testInput: "{nope:}",
expected: "",
},
// TODO add test for env variables in defaults
{
// should chain variable expands
testInput: "{nope:foo {test1}bar}",
expected: "foo val1bar",
},
{
// should not chain variable expands
testInput: "{nope:$test1}",
expected: "$test1",
// should chain variable expands
testInput: "{nope:{nope:{test1:default}}}",
expected: "val1",
},
} {
actual := rep.ReplaceKnown(tc.testInput, "EMPTY")
Expand Down

0 comments on commit f96274d

Please sign in to comment.