From cb23c744b95e9dba062cfe73abc71ac5e8697f56 Mon Sep 17 00:00:00 2001 From: Coenen Benjamin Date: Mon, 11 Jun 2018 10:10:51 +0200 Subject: [PATCH] fix(sdk): fix interpolate with dash into helper (#2857) Signed-off-by: Benjamin Coenen --- sdk/interpolate/interpolate.go | 13 ++++++++++--- sdk/interpolate/interpolate_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/sdk/interpolate/interpolate.go b/sdk/interpolate/interpolate.go index def2797863..0103ab42b9 100644 --- a/sdk/interpolate/interpolate.go +++ b/sdk/interpolate/interpolate.go @@ -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) diff --git a/sdk/interpolate/interpolate_test.go b/sdk/interpolate/interpolate_test.go index 4b5e48795a..f610a2022d 100644 --- a/sdk/interpolate/interpolate_test.go +++ b/sdk/interpolate/interpolate_test.go @@ -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"}, @@ -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) {