Skip to content

Commit

Permalink
Make string variable interpolation deterministic, and single-pass.
Browse files Browse the repository at this point in the history
Right now we replace string variables one at a time, in an order governed by
iteration through a map[string]string. This has two implications:
- We may expand variables twice. If one variable expands to another variable, that one may be expanded. And so on.
- Whether or not this secondary expansion occurs (and even how it occurs!) can vary from run to run.

This commit changes to replace everything at once, using the regexp package. It also adds a test to ensure replacements
are stable and non-recursiive.
  • Loading branch information
dlorenc committed Jul 28, 2020
1 parent ae87d00 commit 8257daa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/substitution/substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ func matchGroups(matches []string, pattern *regexp.Regexp) map[string]string {
}

func ApplyReplacements(in string, replacements map[string]string) string {
replacementsList := []string{}
for k, v := range replacements {
in = strings.Replace(in, fmt.Sprintf("$(%s)", k), v, -1)
replacementsList = append(replacementsList, fmt.Sprintf("$(%s)", k), v)
}
return in
// strings.Replacer does all replacements in one pass, preventing multiple replacements
// See #2093 for an explanation on why we need to do this.
replacer := strings.NewReplacer(replacementsList...)
return replacer.Replace(in)
}

// Take an input string, and output an array of strings related to possible arrayReplacements. If there aren't any
Expand Down
18 changes: 18 additions & 0 deletions pkg/substitution/substitution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ func TestApplyReplacements(t *testing.T) {
}
}

func TestNestedReplacements(t *testing.T) {
replacements := map[string]string{
// Foo should turn into barbar, which could then expand into bazbaz depending on how this is expanded
"foo": "$(bar)$(bar)",
"bar": "baz",
}
input := "$(foo) is cool"
expected := "$(bar)$(bar) is cool"

// Run this test a lot of times to ensure the behavior is deterministic
for i := 0; i <= 1000; i++ {
got := substitution.ApplyReplacements(input, replacements)
if d := cmp.Diff(expected, got); d != "" {
t.Errorf("ApplyReplacements() output did not match expected value %s", diff.PrintWantGot(d))
}
}
}

func TestApplyArrayReplacements(t *testing.T) {
type args struct {
input string
Expand Down

0 comments on commit 8257daa

Please sign in to comment.