You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to use the really useful dig function within a range, but because the dig function only accepts "varargs" for path, I'm not able to use it.
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
}
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 😇
The text was updated successfully, but these errors were encountered: