diff --git a/gjson_hack/precision.go b/gjson_hack/precision.go index 662fc35..2e808e5 100644 --- a/gjson_hack/precision.go +++ b/gjson_hack/precision.go @@ -330,6 +330,22 @@ func SliceSlice(gjsonResult gjson.Result) ([][]any, error) { return res, nil } +// MapStrAny 获取任意map类型的结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:26 2024/12/2 +func MapStrAny[T string | int64 | uint64 | bool | float64 | []any | map[string]any | any](gjsonResult gjson.Result) (map[string]T, error) { + if !gjsonResult.IsObject() { + return nil, ErrDataIsNotObject + } + var res map[string]T + if err := serialize.JSON.UnmarshalWithNumber([]byte(gjsonResult.String()), &res); nil != err { + return nil, err + } + return res, nil +} + // Value 获取指定的值 // // Author : go_developer@163.com<白茶清欢> @@ -383,55 +399,20 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any) case consts.DataTypeSliceMapStringAny: return SliceMapStringAny(sourceValue) case consts.DataTypeMapStrInt: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string]int64 - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[int64](sourceValue) case consts.DataTypeMapStrUint: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string]uint64 - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[uint64](sourceValue) case consts.DataTypeMapStrFloat: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string]float64 - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[float64](sourceValue) case consts.DataTypeMapStrBool: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string]bool - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[bool](sourceValue) case consts.DataTypeMapStrAny: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string]any - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[any](sourceValue) case consts.DataTypeMapStrStr: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string]string - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[string](sourceValue) case consts.DataTypeMapStrSlice: - if !sourceValue.IsObject() { - return nil, ErrDataIsNotObject - } - var res map[string][]any - err := strVal.ToStruct(&res) - return res, err + return MapStrAny[[]any](sourceValue) default: - return nil, errors.New("`" + dataType.String() + "` is not support!") + return nil, errors.New("data_type = `" + dataType.String() + "` is not support!") } }