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

function "has" will panic when used on nested map #134

Closed
cheungpat opened this issue May 5, 2017 · 4 comments · Fixed by #135
Closed

function "has" will panic when used on nested map #134

cheungpat opened this issue May 5, 2017 · 4 comments · Fixed by #135
Assignees
Labels

Comments

@cheungpat
Copy link

When using the has function to check if a key exists on a nested map, gomplate will panic:

$ cat template.yaml
hello: {{- if has (datasource "default").data "word" -}}
{{- (datasource "default").data.word -}}
{{- else -}}
bob
{{- end -}}

$ cat config.yaml
data:
  word: alice

$ gomplate -f template.yaml --datasource default=config.yaml
hello:panic: template: template:1:30: executing "template" at <"default">: wrong type for value; expected map[string]interface {}; got map[interface {}]interface {}

goroutine 1 [running]:
main.(*Gomplate).RunTemplate(0xc42022a060, 0xc420090f80, 0x80, 0x1a7ba60, 0xc42000e018)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/gomplate.go:33 +0x271
main.renderTemplate(0xc42022a060, 0xc420090f80, 0x80, 0x16c5313, 0x1, 0x0, 0x0)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/gomplate.go:99 +0xc1
main.processInputFiles(0x0, 0x0, 0xc420213050, 0x1, 0x1, 0xc420212fe0, 0x1, 0x1, 0xc42022a060, 0x16c5c1e, ...)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/process.go:23 +0x12f
main.runTemplate(0x1aae800, 0x0, 0x0)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/gomplate.go:88 +0x18f
main.newGomplateCmd.func1(0xc420019200, 0xc4201b9100, 0x0, 0x4, 0x0, 0x0)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/main.go:68 +0x7e
github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra.(*Command).execute(0xc420019200, 0xc4200101a0, 0x4, 0x4, 0xc420019200, 0xc4200101a0)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra/command.go:620 +0x3f8
github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc420019200, 0x2, 0x16c54c5, 0x2)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra/command.go:699 +0x339
github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra.(*Command).Execute(0xc420019200, 0xc4200f7f78, 0x0)
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra/command.go:658 +0x2b
main.main()
	/Users/dee/gocode/src/github.com/hairyhenderson/gomplate/main.go:96 +0x46

Checking for key in top-level map works as expected:

$ cat template.yaml
hello: {{- if has (datasource "default") "word" -}}
{{- (datasource "default").word -}}
{{- else -}}
bob
{{- end -}}

$ cat config.yaml
word: alice

$ gomplate -f template.yaml --datasource default=config.yaml
hello:alice
@hairyhenderson
Copy link
Owner

Hi @cheungpat - thanks for the bug report, and sorry that you're experiencing this!

I've reproduced this, and I think I know what's going on. A possible workaround would be to re-convert it to a map[string]interface{} like this:

{{- if has ((datasource "default").data | toYAML | yaml) "word" -}}

It's a hack, but it should work for now. I'm going to see if I can figure out a proper fix for you though 😉

@hairyhenderson
Copy link
Owner

@cheungpat I've fixed this bug, and I've added numerous tests to make sure this sort of thing doesn't happen again! This'll be in the 1.7.0 release (next couple days, hopefully?), but if you want to try it out right away you can build gomplate from master after I merge the fix.

@cheungpat
Copy link
Author

Seems fixed! Thank you for fixing it so quick! 👍

@hairyhenderson
Copy link
Owner

You're welcome 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants