wp-go/helper/maps/function.go

47 lines
763 B
Go
Raw Normal View History

2023-01-21 11:31:23 +00:00
package maps
2023-01-11 05:56:56 +00:00
import "strings"
2023-02-10 13:23:30 +00:00
func GetStrMapAnyVal[T any](m map[string]any, key string) (r T, o bool) {
2023-01-11 05:56:56 +00:00
k := strings.Split(key, ".")
if len(k) > 1 {
2023-02-10 13:23:30 +00:00
val, ok := m[k[0]]
2023-01-11 05:56:56 +00:00
if ok {
vx, ok := val.(map[string]any)
if ok {
2023-02-10 13:23:30 +00:00
r, o = GetStrMapAnyVal[T](vx, strings.Join(k[1:], "."))
2023-01-11 05:56:56 +00:00
}
}
} else {
2023-02-10 13:23:30 +00:00
x, ok := m[k[0]]
2023-01-11 05:56:56 +00:00
if ok {
vv, ok := x.(T)
if ok {
o = true
r = vv
}
}
}
return
}
2023-01-12 04:54:14 +00:00
2023-01-21 11:31:23 +00:00
func GetStrMapAnyValWithAny(key string, v map[string]any) (r any, o bool) {
2023-01-12 04:54:14 +00:00
k := strings.Split(key, ".")
if len(k) > 1 {
val, ok := v[k[0]]
if ok {
vx, ok := val.(map[string]any)
if ok {
2023-01-21 11:31:23 +00:00
r, o = GetStrMapAnyValWithAny(strings.Join(k[1:], "."), vx)
2023-01-12 04:54:14 +00:00
}
}
} else {
x, ok := v[k[0]]
if ok {
o = true
r = x
}
}
return
}