Skip to content

Commit

Permalink
Merge pull request #1344 from FernFerret/f-parseyaml
Browse files Browse the repository at this point in the history
Add parseYAML helper
  • Loading branch information
eikenb authored Apr 24, 2020
2 parents dbcb0c6 + 3900340 commit dc4dbf0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ this functionality might prove useful.
- [parseInt](#parseint)
- [parseJSON](#parsejson)
- [parseUint](#parseuint)
- [parseYAML](#parseyaml)
- [plugin](#plugin)
- [regexMatch](#regexmatch)
- [regexReplaceAll](#regexreplaceall)
Expand Down Expand Up @@ -1792,6 +1793,17 @@ Takes the given string and parses it as a base-10 int64:
{{ "1" | parseUint }}
```
##### `parseYAML`
Takes the given input (usually the value from a key) and parses the result as
YAML:
```liquid
{{ with $d := key "user/info" | parseYAML }}{{ $d.name }}{{ end }}
```
Note: The same caveats that apply to `parseJSON` apply to `parseYAML`.
##### `plugin`
Takes the name of a plugin and optional payload and executes a Consul Template
Expand Down
13 changes: 13 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,19 @@ func parseUint(s string) (uint64, error) {
return result, nil
}

// parseYAML returns a structure for valid YAML
func parseYAML(s string) (interface{}, error) {
if s == "" {
return map[string]interface{}{}, nil
}

var data interface{}
if err := yaml.Unmarshal([]byte(s), &data); err != nil {
return nil, err
}
return data, nil
}

// plugin executes a subprocess as the given command string. It is assumed the
// resulting command returns JSON which is then parsed and returned as the
// value for use in the template.
Expand Down
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func funcMap(i *funcMapInput) template.FuncMap {
"parseInt": parseInt,
"parseJSON": parseJSON,
"parseUint": parseUint,
"parseYAML": parseYAML,
"plugin": plugin,
"regexReplaceAll": regexReplaceAll,
"regexMatch": regexMatch,
Expand Down
33 changes: 33 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,39 @@ func TestTemplate_Execute(t *testing.T) {
"1",
false,
},
{
"helper_parseYAML",
&NewTemplateInput{
Contents: `{{ "foo: bar" | parseYAML }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"map[foo:bar]",
false,
},
{
"helper_parseYAMLv2",
&NewTemplateInput{
Contents: `{{ "foo: bar\nbaz: \"foo\"" | parseYAML }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"map[baz:foo foo:bar]",
false,
},
{
"helper_parseYAMLnested",
&NewTemplateInput{
Contents: `{{ "foo:\n bar: \"baz\"\n baz: 7" | parseYAML }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"map[foo:map[bar:baz baz:7]]",
false,
},
{
"helper_plugin",
&NewTemplateInput{
Expand Down

0 comments on commit dc4dbf0

Please sign in to comment.