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

Handle multi-document YAML streams #534

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ func YAMLArray(in string) ([]interface{}, error) {
return obj, nil
}

// YAMLStream - parses a (potentially) multi-document YAML stream
func YAMLStream(in string) ([]interface{}, error) {
obj := []interface{}{}
s := strings.NewReader(in)
d := yaml.NewDecoder(s)
for {
var o interface{}
err := d.Decode(&o)
if err == io.EOF {
break
}
obj = append(obj, o)
}
return obj, nil
}

// TOML - Unmarshal a TOML Object
func TOML(in string) (interface{}, error) {
obj := make(map[string]interface{})
Expand Down
21 changes: 20 additions & 1 deletion data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestUnmarshalArray(t *testing.T) {
assert.EqualValues(t, expected, actual)
}
test(JSONArray(`["foo","bar",{"baz":{"qux": true},"quux":{"42":18},"corge":{"false":"blah"}}]`))
test(YAMLArray(`
test(YAMLArray(`---
- foo
- bar
- baz:
Expand Down Expand Up @@ -523,3 +523,22 @@ QUX='single quotes ignore $variables'
assert.NoError(t, err)
assert.EqualValues(t, expected, out)
}

func TestMultiDocYAML(t *testing.T) {
in := `foo: bar
---
baz: qux
---
# empty document with a comment
`
expected := []interface{}{
map[string]interface{}{"foo": "bar"},
map[string]interface{}{"baz": "qux"},
nil,
}
ch, err := YAMLArray(in)
for i, out := range ch {
assert.NoError(t, err)
assert.EqualValues(t, expected[i], out)
}
}
5 changes: 5 additions & 0 deletions funcs/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (f *DataFuncs) YAMLArray(in interface{}) ([]interface{}, error) {
return data.YAMLArray(conv.ToString(in))
}

// YAMLStream -
func (f *DataFuncs) YAMLStream(in interface{}) ([]interface{}, error) {
return data.YAMLStream(conv.ToString(in))
}

// TOML -
func (f *DataFuncs) TOML(in interface{}) (interface{}, error) {
return data.TOML(conv.ToString(in))
Expand Down
16 changes: 16 additions & 0 deletions internal/tests/integration/datasources_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ FOO.BAR = "values can be double-quoted, and shell\nescapes are supported"

BAZ = "variable expansion: ${FOO}"
QUX='single quotes ignore $variables'
`,
"multidoc.yaml": `---
# empty document
---
foo: bar
---
foo: baz
...
`,
}),
fs.WithDir("sortorder", fs.WithFiles(map[string]string{
Expand Down Expand Up @@ -191,4 +199,12 @@ bar`})
"FOO.BAR": "values can be double-quoted, and shell\nescapes are supported",
"QUX": "single quotes ignore $variables"
}`})

result = icmd.RunCmd(icmd.Command(GomplateBin,
"-d=multidoc.yaml",
"-i", `{{ range $d := (include "multidoc" | data.YAMLStream) }}---{{"\n"}}{{ range $k, $v := $d }}{{ $k }}: {{ $v }}{{"\n"}}{{ end }}{{ end }}`,
), func(c *icmd.Cmd) {
c.Dir = s.tmpDir.Path()
})
result.Assert(c, icmd.Expected{ExitCode: 0, Out: "---\n---\nfoo: bar\n---\nfoo: baz\n"})
}