Skip to content

Commit

Permalink
fix(sdk): fix interpolate with dash into helper (#2857)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Coenen <benjamin.coenen@corp.ovh.com>
  • Loading branch information
bnjjj authored and yesnault committed Jun 11, 2018
1 parent 6b30445 commit cb23c74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 10 additions & 3 deletions sdk/interpolate/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,22 @@ func Do(input string, vars map[string]string) (string, error) {
nameWithDot := strings.Replace(e, "__", ".", -1)
nameWithDot = strings.Replace(nameWithDot, "µµµ", ".", -1)
nameWithDot = strings.Replace(nameWithDot, "\"", "\\\"", -1)

helperPos := strings.Index(e, "|")
// "-"" are not a valid char in go template var name, as we don't know e, no pb to replace "-" with "µ"
eb := strings.Replace(e, "-", "µµµ", -1)
var eb string
if helperPos > 0 {
eb = strings.Replace(e[:helperPos], "-", "µµµ", -1) + e[helperPos:]
} else {
eb = strings.Replace(e, "-", "µµµ", -1)
}

// check if helper exists. if helper does not exist, as
// '{{"conf"|uvault}}' -> return '{{"conf"|uvault}}' in defaultCDS value
// '{{ defaultCDS "{{\"conf\"|uvault}}" "" }}'

if pos := strings.Index(eb, "|"); pos > 0 && len(eb) > pos {
helper = strings.TrimSpace(eb[pos+1:])
if helperPos > 0 && len(eb) > helperPos {
helper = strings.TrimSpace(eb[helperPos+1:])
if strings.HasPrefix(helper, "default") {
// 7 = len("default") --> helper[7:]
input = strings.Replace(input, sm[i][1], "{{ defaultCDS "+helper[7:]+" "+eb+" }}", -1)
Expand Down
25 changes: 24 additions & 1 deletion sdk/interpolate/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func BenchmarkDoNothing(b *testing.B) {
args: args{
input: `
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam felis nulla, vulputate ac eros vel, placerat dignissim turpis. Sed et ex lectus. Donec viverra nisi vel dictum rhoncus. Sed dictum tempus quam, ut efficitur arcu viverra vitae. Suspendisse aliquam venenatis scelerisque. Praesent et mattis enim. In efficitur imperdiet nulla a sagittis. Maecenas aliquet magna in sollicitudin ornare.
Suspendisse viverra enim nec ante blandit tempus. Sed ut erat suscipit, semper ex eu, eleifend neque. Sed orci justo, bibendum laoreet libero cursus, venenatis fringilla dui. Curabitur tristique odio ut neque sollicitudin ultrices. Integer metus nibh, dignissim non pellentesque et, volutpat vel ante. Pellentesque ultrices ante vel mauris aliquam porttitor. Nunc nec sem facilisis, ullamcorper ex sed, elementum elit. Nulla risus magna, tempor et ultricies id, vehicula ac massa. Mauris venenatis libero libero, id lobortis mi semper aliquam. Aenean neque turpis, feugiat vel rutrum vitae, auctor quis nisl. Donec placerat nec mauris vitae malesuada. Proin quis gravida nulla. Pellentesque in pellentesque metus, in finibus dui. Sed rutrum, libero sit amet cursus scelerisque, sem orci condimentum nunc, quis egestas tellus orci ac nisl. Mauris viverra tincidunt diam ac sollicitudin. Nunc venenatis, nibh at laoreet pellentesque, lacus tellus molestie lorem, et sollicitudin nunc neque ut turpis.
`,
vars: map[string]string{"cds.env.name": "", "cds.env.token": "aValidTokenString", "cds.env.addr": "", "cds.env.vAppKey": "aValue"},
Expand Down Expand Up @@ -295,6 +295,29 @@ workflow:
git.author: ""
git.branch: master`,
},
{
name: "- inside function parameter",
args: args{
input: `name: "coucou-{{ .name | default "0.0.1-dirty" }}"`,
vars: map[string]string{
"git.branch": "master",
"git.author": "",
},
},
want: `name: "coucou-0.0.1-dirty"`,
},
{
name: "- inside function parameter but not used",
args: args{
input: `name: "coucou-{{ .name | default "0.0.1-dirty" }}"`,
vars: map[string]string{
"git.branch": "master",
"git.author": "",
"name": "toi",
},
},
want: `name: "coucou-toi"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit cb23c74

Please sign in to comment.