Thanks to @mdaniel for his answer, which helped/allowed me to fix my problem! His (quick, I get it) solution has problem though if you have move values with same prefix. To carry on with his example:
{{ $fred := dict
"alpha" (dict "a0" "a0ch0")
"beta" (dict "beta0" (dict "beta00" 1234))
"charlie" (list "ch0" "ch1" "ch2")
"problem" (dict "beta0" (dict "1" "1" "2" "2" )) }}
data:
theData: |
{{ toJson $fred | indent 4 }}
toml: |
{{ include "bob" $fred | indent 4 }}
will produce:
data:
theData: |
{"alpha":{"a0":"a0ch0"},"beta":{"beta0":{"beta00":1234}},"charlie":["ch0","ch1","ch2"],"problem":{"beta0":{"1":"1","2":"2"}}}
toml: |
problem.beta0.1="1"
2="2"
alpha.a0="a0ch0"
beta.beta0.beta00=1234
charlie=["ch0","ch1","ch2"]
which is not correct. Also there is another eye-candy problem of lost order, no idea why or how to fix it, but that's not such biggie.
My extra requirement was, that I needed to add common prefix to all lines. Without it you can define another template simplifying usage, but that's trivial to do.
disclaimer: I'm totally new to helm/go-templating, so following isn't probably optimal, but it should fix these issues.
To carry on with example again:
{{ $fred := dict
"alpha" (dict "a0" "a0ch0")
"beta" (dict "beta0" (dict "beta00" 1234))
"charlie" (list "ch0" "ch1" "ch2")
"problem" (dict "beta0" (dict "1" "1" "2" "2" )) }}
data:
theData: |
{{ toJson $fred | indent 4 }}
toml: |
{{ include "flattenYaml" (dict "prefix" "added_prefix" "data" $fred) | indent 4 }}
will produce:
data:
theData: |
{"alpha":{"a0":"a0ch0"},"beta":{"beta0":{"beta00":1234}},"charlie":["ch0","ch1","ch2"],"problem":{"beta0":{"1":"1","2":"2"}}}
toml: |
added_prefix.alpha.a0="a0ch0"
added_prefix.beta.beta0.beta00=1234
added_prefix.charlie=["ch0","ch1","ch2"]
added_prefix.problem.beta0.1="1"
added_prefix.problem.beta0.2="2"
and template source for flattenYaml looks like this:
{{- define "flattenYaml" -}}
{{- $dict := . -}}
{{- $prefix := $dict.prefix -}}
{{- $data := $dict.data -}}
{{- $knd := kindOf $data -}}
{{- if eq $knd "map" }}
{{- range (keys $data) }}
{{- $key := . }}
{{- $prefixedKey := (printf "%s.%s" $prefix $key) }}
{{- $value := get $data $key }}
{{- $valueKind := kindOf $value }}
{{- if eq $valueKind "map" }}
{{- include "flattenYaml" (dict "prefix" ($prefixedKey) "data" $value) }}
{{- else }}
{{- printf "%s=%s\n" $prefixedKey (toJson $value) }}
{{- end }}
{{- end }}
{{- else }}
{{ toJson . }}#k({{ $knd }})
{{- end }}
{{- end -}}
disclaimer 2: I have no idea what #k ... in {{ toJson . }}#k({{ $knd }}) from original solution does, do not be surprised if it explodes into your face ;)
edit:
the
{{ toJson . }}#k({{ $knd }})
actually blew into my face, still don't know what #k should serve, but following replacement fixed my issue:
{{- if ne $data nil }}
{{- toJson $data }}
{{- end }}