Skip to content

Commit c5662d1

Browse files
author
Joseph Palermo
authored
Merge pull request #653 from cloudfoundry/improvement-allow-interpolation-with-whitespace
allow white-space in (( var_name )) template
2 parents 38ff205 + 55cc5bc commit c5662d1

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

director/template/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (t Template) interpolateRoot(obj interface{}, tracker varsTracker) (interfa
8888
type interpolator struct{}
8989

9090
var (
91-
interpolationRegex = regexp.MustCompile(`\(\((!?[-/\.\w\pL]+)\)\)`)
91+
interpolationRegex = regexp.MustCompile(`\(\( ?(!?[-/\.\w\pL]+) ?\)\)`)
9292
interpolationAnchoredRegex = regexp.MustCompile("\\A" + interpolationRegex.String() + "\\z")
9393
)
9494

director/template/template_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,44 @@ var _ = Describe("Template", func() {
2020
Expect(err).NotTo(HaveOccurred())
2121
Expect(result).To(Equal([]byte("foo\n")))
2222
})
23+
Context("the (( variable )) placeholder uses spaces", func() {
24+
It(`can interpolate values even if var name is prefixed with a whitespace`, func() {
25+
template := NewTemplate([]byte("(( key))"))
26+
vars := StaticVariables{"key": "foo"}
27+
28+
result, err := template.Evaluate(vars, nil, EvaluateOpts{})
29+
Expect(err).NotTo(HaveOccurred())
30+
Expect(result).To(Equal([]byte("foo\n")))
31+
})
32+
It(`can interpolate values even if var name is suffixed with a whitespace`, func() {
33+
template := NewTemplate([]byte("((key ))"))
34+
vars := StaticVariables{"key": "foo"}
35+
36+
result, err := template.Evaluate(vars, nil, EvaluateOpts{})
37+
Expect(err).NotTo(HaveOccurred())
38+
Expect(result).To(Equal([]byte("foo\n")))
39+
})
40+
It(`can interpolate values even if var name is surrounded by whitespace`, func() {
41+
template := NewTemplate([]byte("(( key ))"))
42+
vars := StaticVariables{"key": "foo"}
43+
44+
result, err := template.Evaluate(vars, nil, EvaluateOpts{})
45+
Expect(err).NotTo(HaveOccurred())
46+
Expect(result).To(Equal([]byte("foo\n")))
47+
})
48+
It("can interpolate a specific indexed value of an variable that is an array", func() {
49+
template := NewTemplate([]byte("(( key)): (( value.1))"))
50+
vars := StaticVariables{
51+
"key": "foo",
52+
"value": []interface{}{"bar", "baz"},
53+
}
54+
55+
result, err := template.Evaluate(vars, nil, EvaluateOpts{})
56+
Expect(err).NotTo(HaveOccurred())
57+
Expect(result).To(Equal([]byte("foo: baz\n")))
58+
})
59+
60+
})
2361

2462
It("can interpolate a specific indexed value of an variable that is an array", func() {
2563
template := NewTemplate([]byte("((key)): ((value.1))"))

0 commit comments

Comments
 (0)