|
|
@@ -10,6 +10,12 @@ const (
|
|
|
pathSeparator = "."
|
|
|
)
|
|
|
|
|
|
+// GetValueByPath 通过路径查询map的值,常用查找unmarshal到map的json或者yaml中的值
|
|
|
+// 参数:
|
|
|
+// m: key是string类型的map,值可以是任意类型,如果map下的值还是map,则map的key必须为string类型
|
|
|
+// path: 路径,如foo1.foo1_sub.[0],key为foo1的值是一个map[string]any,foo1_sub对应的值是一个slice,如果需要取slice的某一个值,可以用[n]获取
|
|
|
+// 返回值:
|
|
|
+// 对应的值和错误
|
|
|
func GetValueByPath(m map[string]any, path string) (any, error) {
|
|
|
if m == nil || strings.TrimSpace(path) == "" {
|
|
|
return nil, errors.New("没有传递需要查找的数据或路径")
|
|
|
@@ -51,7 +57,7 @@ func GetValueByPath(m map[string]any, path string) (any, error) {
|
|
|
} else {
|
|
|
findMap, ok := currentFind.(map[string]any)
|
|
|
if !ok {
|
|
|
- return nil, errors.New("对应map路径的值不是map: " + pathPart)
|
|
|
+ return nil, errors.New("对应map路径的值不是map[string]any: " + pathPart)
|
|
|
}
|
|
|
|
|
|
value, ok := findMap[pathPart]
|
|
|
@@ -72,6 +78,14 @@ func GetValueByPath(m map[string]any, path string) (any, error) {
|
|
|
return retValue, nil
|
|
|
}
|
|
|
|
|
|
+// GetValueByKey 通过key查询map的值
|
|
|
+// 模板参数:
|
|
|
+// K是map的key的类型,V是map值的类型
|
|
|
+// 参数:
|
|
|
+// m: 从这个map获取值
|
|
|
+// key: map的key
|
|
|
+// 返回值:
|
|
|
+// 对应的值和获取是否成功,失败可能有两个原因:对应的key不存在或者V类型转换失败
|
|
|
func GetValueByKey[K comparable, V any](m map[K]any, key K) (V, bool) {
|
|
|
var zeroValue V
|
|
|
|