Skip to content

Fix nested template panic #1480

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

Merged
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
5 changes: 5 additions & 0 deletions internal/config/configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ plugins:
cmd: echo
pipe: true

templates:
foo:
url: file:///tmp/foo.t

pluginTimeout: 2s
`
expected = &Config{
Expand All @@ -72,6 +76,7 @@ pluginTimeout: 2s
Plugins: map[string]PluginConfig{
"foo": {Cmd: "echo", Pipe: true},
},
Templates: Templates{"foo": DataSource{URL: mustURL("file:///tmp/foo.t")}},
PluginTimeout: 2 * time.Second,
}

Expand Down
4 changes: 3 additions & 1 deletion internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ type Templates map[string]DataSource
// UnmarshalYAML - satisfy the yaml.Umarshaler interface
func (t *Templates) UnmarshalYAML(value *yaml.Node) error {
// first attempt to unmarshal as a map[string]DataSource
err := value.Decode(map[string]DataSource(*t))
m := map[string]DataSource{}
err := value.Decode(m)
if err == nil {
*t = m
return nil
}

Expand Down
28 changes: 28 additions & 0 deletions internal/tests/integration/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,31 @@ suppressEmpty: true
_, err = os.Stat(tmpDir.Join("missing"))
assert.Equal(t, true, os.IsNotExist(err))
}

func TestConfig_ConfigTemplatesSupportsMap(t *testing.T) {
tmpDir := setupConfigTest(t)

writeConfig(t, tmpDir, `in: '{{ template "t1" (dict "testValue" "12345") }}'
templates:
t1:
url: t1.tmpl
`)
writeFile(t, tmpDir, "t1.tmpl", `{{ .testValue }}`)

o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "12345")
}

func TestConfig_ConfigTemplatesSupportsArray(t *testing.T) {
tmpDir := setupConfigTest(t)

// TODO: remove this test once the array format is no longer supported
writeConfig(t, tmpDir, `in: '{{ template "t1" (dict "testValue" "12345") }}'
templates:
- t1=t1.tmpl
`)
writeFile(t, tmpDir, "t1.tmpl", `{{ .testValue }}`)

o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "12345")
}