fix: sprig dict funcs are failing on dicts nested under arrays (#646)
Fixes #643
This commit is contained in:
parent
65ee6a2124
commit
2d2b3e486f
|
|
@ -1522,6 +1522,8 @@ releases:
|
||||||
{stateExternal, `{{ if (keys .Environment.Values | has "foo") }}{{ .Environment.Values.foo }}{{ end }}`, `FOO`},
|
{stateExternal, `{{ if (keys .Environment.Values | has "foo") }}{{ .Environment.Values.foo }}{{ end }}`, `FOO`},
|
||||||
// See https://github.com/roboll/helmfile/issues/624
|
// See https://github.com/roboll/helmfile/issues/624
|
||||||
{stateExternal, `{{ if (keys .Environment.Values | has "bar") }}{{ if (keys .Environment.Values.bar | has "baz") }}{{ .Environment.Values.bar.baz }}{{ end }}{{ end }}`, `BAZ`},
|
{stateExternal, `{{ if (keys .Environment.Values | has "bar") }}{{ if (keys .Environment.Values.bar | has "baz") }}{{ .Environment.Values.bar.baz }}{{ end }}{{ end }}`, `BAZ`},
|
||||||
|
// See https://github.com/roboll/helmfile/issues/643
|
||||||
|
{stateExternal, `{{ range $service := .Environment.Values.services }}{{ $service.name }}{{ if hasKey $service "something" }}{{ $service.something }}{{ end }}{{ end }}`, `xyfalse`},
|
||||||
}
|
}
|
||||||
for i := range testcases {
|
for i := range testcases {
|
||||||
tc := testcases[i]
|
tc := testcases[i]
|
||||||
|
|
@ -1530,7 +1532,12 @@ releases:
|
||||||
testFs := state.NewTestFs(map[string]string{
|
testFs := state.NewTestFs(map[string]string{
|
||||||
statePath: stateContent,
|
statePath: stateContent,
|
||||||
"/path/to/1.yaml": `foo: FOO`,
|
"/path/to/1.yaml": `foo: FOO`,
|
||||||
"/path/to/2.yaml": `bar: { "baz": "BAZ" }`,
|
"/path/to/2.yaml": `bar: { "baz": "BAZ" }
|
||||||
|
services:
|
||||||
|
- name: "x"
|
||||||
|
- name: "y"
|
||||||
|
something: false
|
||||||
|
`,
|
||||||
})
|
})
|
||||||
app := &App{
|
app := &App{
|
||||||
readFile: testFs.ReadFile,
|
readFile: testFs.ReadFile,
|
||||||
|
|
|
||||||
|
|
@ -15,32 +15,18 @@ func CastKeysToStrings(s interface{}) (map[string]interface{}, error) {
|
||||||
return nil, fmt.Errorf("unexpected type of key in map: expected string, got %T: value=%v, map=%v", typed_k, typed_k, src)
|
return nil, fmt.Errorf("unexpected type of key in map: expected string, got %T: value=%v, map=%v", typed_k, typed_k, src)
|
||||||
}
|
}
|
||||||
|
|
||||||
var casted_v interface{}
|
casted_v, err := recursivelyStringifyMapKey(v)
|
||||||
switch typed_v := v.(type) {
|
if err != nil {
|
||||||
case map[interface{}]interface{}:
|
return nil, err
|
||||||
tmp, err := CastKeysToStrings(typed_v)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
casted_v = tmp
|
|
||||||
default:
|
|
||||||
casted_v = typed_v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new[str_k] = casted_v
|
new[str_k] = casted_v
|
||||||
}
|
}
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
for k, v := range src {
|
for k, v := range src {
|
||||||
var casted_v interface{}
|
casted_v, err := recursivelyStringifyMapKey(v)
|
||||||
switch typed_v := v.(type) {
|
if err != nil {
|
||||||
case map[interface{}]interface{}:
|
return nil, err
|
||||||
tmp, err := CastKeysToStrings(typed_v)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
casted_v = tmp
|
|
||||||
default:
|
|
||||||
casted_v = typed_v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new[k] = casted_v
|
new[k] = casted_v
|
||||||
|
|
@ -49,6 +35,31 @@ func CastKeysToStrings(s interface{}) (map[string]interface{}, error) {
|
||||||
return new, nil
|
return new, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func recursivelyStringifyMapKey(v interface{}) (interface{}, error) {
|
||||||
|
var casted_v interface{}
|
||||||
|
switch typed_v := v.(type) {
|
||||||
|
case map[interface{}]interface{}, map[string]interface{}:
|
||||||
|
tmp, err := CastKeysToStrings(typed_v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
casted_v = tmp
|
||||||
|
case []interface{}:
|
||||||
|
a := []interface{}{}
|
||||||
|
for i := range typed_v {
|
||||||
|
res, err := recursivelyStringifyMapKey(typed_v[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
a = append(a, res)
|
||||||
|
}
|
||||||
|
casted_v = a
|
||||||
|
default:
|
||||||
|
casted_v = typed_v
|
||||||
|
}
|
||||||
|
return casted_v, nil
|
||||||
|
}
|
||||||
|
|
||||||
func Set(m map[string]interface{}, key []string, value string) map[string]interface{} {
|
func Set(m map[string]interface{}, key []string, value string) map[string]interface{} {
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
panic(fmt.Errorf("bug: unexpected length of key: %d", len(key)))
|
panic(fmt.Errorf("bug: unexpected length of key: %d", len(key)))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue