-
Notifications
You must be signed in to change notification settings - Fork 3
templates: toJson doesn't work in some situations #1075
Description
Description
Imagine I'm authoring a template where I want the user of the template to pass in a map:
- name: build my thing
template:
name: build
vars:
build_args:
time: now
debug: trueI'd like to use the map build_args in my template like this:
steps:
- name: build something
image: builder
parameters:
build_args: {{ .build_args | mustToJson }}But this doesn't work. It fails with this error:
json: unsupported type: map[interface {}]interface {}
In JSON, a map is only allowed to have string-type keys (map[string]any), but the Vela code that passes template parameters uses the more general map[any]any.
Value
Because toJson doesn't work, I instead use toYaml, which is apparently more tolerant of input types. It must do some runtime reflection to make sure that all the keys are strings. However, YAML is worse than JSON for this purpose. I have to indent the generated YAML properly using a hardcoded indentation value, which is prone to break when making changes to the template.
steps:
- build something
image: builder
parameters:
build_args: {{ .build_args | toYaml | nindent 14 I think? }}JSON is better. All JSON is valid YAML, so serializing the map like this is totally fine, and much simpler.
steps:
- build something
image: builder
parameters:
build_args: {"time": "now", "debug": true}Definition of Done
Given the example above, it should be possible to do {{ .build_args | mustToJson }} without errors.