From 5ae18410238bebed855f4bbc2a08f3875ea19054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 1 Dec 2024 15:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96[]map[string]any=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gjson_hack/precision.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/gjson_hack/precision.go b/gjson_hack/precision.go index 2baec52..188300e 100644 --- a/gjson_hack/precision.go +++ b/gjson_hack/precision.go @@ -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