Skip to content

Commit

Permalink
Merge pull request #653 from cloudfoundry/improvement-allow-interpola…
Browse files Browse the repository at this point in the history
…tion-with-whitespace

allow white-space in (( var_name )) template
  • Loading branch information
jpalermo authored Jun 6, 2024
2 parents 38ff205 + 55cc5bc commit c5662d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion director/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (t Template) interpolateRoot(obj interface{}, tracker varsTracker) (interfa
type interpolator struct{}

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

Expand Down
38 changes: 38 additions & 0 deletions director/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@ var _ = Describe("Template", func() {
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("foo\n")))
})
Context("the (( variable )) placeholder uses spaces", func() {
It(`can interpolate values even if var name is prefixed with a whitespace`, func() {
template := NewTemplate([]byte("(( key))"))
vars := StaticVariables{"key": "foo"}

result, err := template.Evaluate(vars, nil, EvaluateOpts{})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("foo\n")))
})
It(`can interpolate values even if var name is suffixed with a whitespace`, func() {
template := NewTemplate([]byte("((key ))"))
vars := StaticVariables{"key": "foo"}

result, err := template.Evaluate(vars, nil, EvaluateOpts{})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("foo\n")))
})
It(`can interpolate values even if var name is surrounded by whitespace`, func() {
template := NewTemplate([]byte("(( key ))"))
vars := StaticVariables{"key": "foo"}

result, err := template.Evaluate(vars, nil, EvaluateOpts{})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("foo\n")))
})
It("can interpolate a specific indexed value of an variable that is an array", func() {
template := NewTemplate([]byte("(( key)): (( value.1))"))
vars := StaticVariables{
"key": "foo",
"value": []interface{}{"bar", "baz"},
}

result, err := template.Evaluate(vars, nil, EvaluateOpts{})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal([]byte("foo: baz\n")))
})

})

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

0 comments on commit c5662d1

Please sign in to comment.