Open davinkevin opened 1 year ago
I do the following, not perfect, but works when you can control the context:
func FuncMap() ttemplate.FuncMap {
f := sprig.TxtFuncMap()
f["getNested"] = getNested
return f
}
func getNested(d map[string]interface{}, key string) interface{} {
t := strings.Split(key, ".")
for i := 0; i < len(t); i++ {
if val, ok := d[t[i]]; ok {
if i+1 == len(t) {
return val
} else {
d = castAndGetMapStringInterface(val)
}
}
}
panic(fmt.Errorf("the map does not have a match for key %s", key))
}
func castAndGetMapStringInterface(i interface{}) map[string]interface{} {
m, ok := i.(map[string]interface{})
if ok {
return m
}
return nil
}
then in your template you can do:
{{getNested .ImageMap "ingress-controller.registry"}}
for struct:
type InterpolateContext struct {
ImageMap map[string]interface{}
}
Hello 👋 ,
I would like to use the really useful
dig
function within a range, but because thedig
function only accepts "varargs" for path, I'm not able to use it.With
$someStruct
equals to:First,
The result of this one is
nil
becausedig
looks for{ "a.b.c": "value" }
🙂. So the result is twice~
instead ofvalue
andvalue2
.Second, with array:
Here, it's a type cast problem because the first parameter is not a string like expected by the function. I have the following error:
It could be cool, especially in dynamic env like a
range
orwith
to have the ability to provide "path" coming from another variable dynamicallyThank you 😇