map RecursiveSetStrVal

This commit is contained in:
xing 2023-06-28 22:49:45 +08:00
parent a00bfff91c
commit 879238cda1

View File

@ -106,3 +106,30 @@ func GetAnyAnyValWithDefaults[T any](m map[any]any, defaults T, key ...any) (r T
r = v r = v
return return
} }
func RecursiveSetStrVal[T any](m map[string]any, k string, v T) {
kk := strings.Split(k, ".")
if len(kk) < 1 {
return
} else if len(kk) < 2 {
m[k] = v
return
}
for i, _ := range kk[0 : len(kk)-1] {
key := strings.Join(kk[0:i+1], ".")
mm, ok := GetStrAnyVal[map[string]any](m, key)
if !ok {
mm = map[string]any{}
preKey := strings.Join(kk[0:i], ".")
if preKey == "" {
RecursiveSetStrVal(m, key, mm)
} else {
m, _ := GetStrAnyVal[map[string]any](m, preKey)
RecursiveSetStrVal(m, kk[i], mm)
}
}
}
key := strings.Join(kk[0:len(kk)-1], ".")
mm, _ := GetStrAnyVal[map[string]any](m, key)
mm[kk[len(kk)-1]] = v
}