Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parseYAML helper #1344

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1781,6 +1782,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 @@ -1375,6 +1375,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