优化[]map[string]any处理

This commit is contained in:
白茶清欢 2024-12-01 15:46:16 +08:00
parent d466b7526d
commit 5ae1841023

View File

@ -10,6 +10,7 @@ package gjson_hack
import (
"errors"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/tidwall/gjson"
@ -249,6 +250,28 @@ func SliceString(gjsonResult gjson.Result) ([]string, error) {
return sliceVal.Value, sliceVal.Err
}
// SliceMapStringAny ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:41 2024/12/1
func SliceMapStringAny(gjsonResult gjson.Result) ([]map[string]any, error) {
if gjsonResult.Value() == nil {
return nil, nil
}
strVal := strings.TrimSpace(gjsonResult.String())
// 任意类型的list
if !strings.HasPrefix(strVal, "[") || !strings.HasSuffix(strVal, "]") {
return nil, ErrDataIsNotArray
}
var res []map[string]any
if err := serialize.JSON.UnmarshalWithNumber([]byte(strVal), &res); nil != err {
return nil, err
}
return res, nil
}
// Value 获取指定的值
//
// Author : go_developer@163.com<白茶清欢>
@ -296,12 +319,7 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any)
case consts.DataTypeSliceSlice, consts.DataTypeMapAnyAny:
return nil, errors.New(consts.DataTypeSliceSlice.String() + " : data type is not support")
case consts.DataTypeSliceMapStringAny:
if !sourceValue.IsArray() {
return nil, ErrDataIsNotArray
}
var res []map[string]any
err := strVal.ToStruct(&res)
return res, err
return SliceMapStringAny(sourceValue)
case consts.DataTypeMapStrInt:
if !sourceValue.IsObject() {
return nil, ErrDataIsNotObject