Skip to content

Commit

Permalink
Merge pull request #37 from gruntwork-io/map-variable-template
Browse files Browse the repository at this point in the history
For map variables, render the key in addition to the value
  • Loading branch information
brikis98 committed Jul 14, 2017
2 parents 6685111 + 9d2f018 commit 67292d1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/variables-recursive/README.md
Expand Up @@ -8,4 +8,6 @@ Baz = {{ .Baz }}
FooList = {{ range $index, $element := .FooList }}{{ if gt $index 0 }}, {{ end }}{{ $element }}{{ end }}
BarList = {{ range $index, $element := .BarList }}{{ if gt $index 0 }}, {{ end }}{{ $element }}{{ end }}
FooMap = {{ range $index, $key := (.FooMap | keys) }}{{ if gt $index 0 }}, {{ end }}{{ $key }}: {{ index $.FooMap $key }}{{ end }}
BarMap = {{ range $index, $key := (.BarMap | keys) }}{{ if gt $index 0 }}, {{ end }}{{ $key }}: {{ index $.BarMap $key }}{{ end }}
BarMap = {{ range $index, $key := (.BarMap | keys) }}{{ if gt $index 0 }}, {{ end }}{{ $key }}: {{ index $.BarMap $key }}{{ end }}
ListWithTemplates = {{ range $index, $element := .ListWithTemplates }}{{ if gt $index 0 }}, {{ end }}{{ $element }}{{ end }}
MapWithTemplates = {{ range $index, $key := (.MapWithTemplates | keys) }}{{ if gt $index 0 }}, {{ end }}{{ $key }}: {{ index $.MapWithTemplates $key }}{{ end }}
14 changes: 14 additions & 0 deletions examples/variables-recursive/boilerplate.yml
Expand Up @@ -29,6 +29,20 @@ variables:
type: map
reference: FooMap

- name: ListWithTemplates
type: list
default:
- "{{ .Foo }}"
- "{{ .Bar }}"
- "{{ .Baz }}"

- name: MapWithTemplates
type: map
default:
"{{ .Foo }}": "{{ .Foo }}"
"{{ .Bar }}": "{{ .Bar }}"
"{{ .Baz }}": "{{ .Baz }}"

dependencies:
- name: variables
template-folder: ../variables
Expand Down
9 changes: 7 additions & 2 deletions templates/template_processor.go
Expand Up @@ -104,11 +104,16 @@ func renderVariable(variable interface{}, variables map[string]interface{}, opti
case map[string]string:
values := map[string]string{}
for key, value := range variableType {
rendered, err := renderTemplateRecursively(options.TemplateFolder, value, variables, options)
renderedKey, err := renderTemplateRecursively(options.TemplateFolder, key, variables, options)
if err != nil {
return nil, err
}

renderedValue, err := renderTemplateRecursively(options.TemplateFolder, value, variables, options)
if err != nil {
return nil, err
}
values[key] = rendered
values[renderedKey] = renderedValue
}
return values, nil
default:
Expand Down
Expand Up @@ -8,4 +8,6 @@ Baz = foo-bar-baz
FooList = foo, bar, baz
BarList = foo, bar, baz
FooMap = bar: 2, baz: 3, foo: 1
BarMap = bar: 2, baz: 3, foo: 1
BarMap = bar: 2, baz: 3, foo: 1
ListWithTemplates = foo, foo-bar, foo-bar-baz
MapWithTemplates = foo: foo, foo-bar: foo-bar, foo-bar-baz: foo-bar-baz

0 comments on commit 67292d1

Please sign in to comment.