From 5fc2e0a6138d003a26ee9e2046a017980dee5a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 2 Dec 2024 14:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96map=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gjson_hack/precision.go | 67 +++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 43 deletions(-) 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!") } }