From 505a41edb58f638638e967747bd06b43d8c543e6 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, 23 Jun 2024 11:38:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E9=AA=8C=E8=AF=81=E5=BA=93,?= =?UTF-8?q?=20=E6=94=AF=E6=8C=81=E7=BB=86=E5=8C=96real=5Fmap=20/=20marshal?= =?UTF-8?q?=5Fmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/rule.go | 12 +++++++++++- go.mod | 8 ++++---- go.sum | 8 ++++++++ handle_map.go | 44 ++++++++++++++++++++++++++++++++++---------- run.go | 10 ++++++++++ 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/define/rule.go b/define/rule.go index 29e1f38..443987d 100644 --- a/define/rule.go +++ b/define/rule.go @@ -26,6 +26,7 @@ type FieldRule struct { RequiredConditionGroup [][]RequiredCondition `json:"required_condition_group"` // 满足何种条件,字段必传,不配置则为无差别必传, 组之间是或条件, 满足一组即命中, 组之内为与条件 ValueLimit *ValueLimit `json:"value_limit"` // 数据值的限制 SliceConfig *SliceConfig `json:"slice_config"` // 数组转化的配置 + MapConfig *MapConfig `json:"map_config"` // 转换对象的配置 } // RequiredCondition 定义必传条件 @@ -77,7 +78,16 @@ type MapValueLimit struct { // // Date : 21:34 2024/4/30 type SliceConfig struct { - Mode string `json:"slice_mode"` // REAL - 输入直接是slice MARSHAL - json序列化之后的字符串 WITH_SPLIT_CHAR - 使用指定字符串分隔 + Mode string `json:"mode"` // REAL - 输入直接是slice MARSHAL - json序列化之后的字符串 WITH_SPLIT_CHAR - 使用指定字符串分隔 DisableIgnoreEmpty bool `json:"disable_ignore_empty"` // 指定字符分割的时候, 忽略空字符串 SplitChar string `json:"split_char"` // 数组转换的分隔符, 当输入模式是 WITH_SPLIT_CHAR 时生效 } + +// MapConfig map配置 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:18 2024/6/23 +type MapConfig struct { + Mode string `json:"mode"` // REAL - 输入直接是map MARSHAL - json序列化之后的字符串 +} diff --git a/go.mod b/go.mod index 854553b..d76b055 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,10 @@ module git.zhangdeman.cn/gateway/validator go 1.22.2 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608104035-feddc6a70f9d - git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 - git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 - git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240520124754-3faebb9145e3 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240623031247-2fea122027a7 + git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd + git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.17.1 github.com/tidwall/sjson v1.2.5 diff --git a/go.sum b/go.sum index ce6e1f4..0f3d516 100644 --- a/go.sum +++ b/go.sum @@ -4,20 +4,28 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec h1:ENemx9 git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608104035-feddc6a70f9d h1:qKou2RVGqRd7ojAHq2H8xTxfwayjYvfn35Rk+Fz0+zU= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608104035-feddc6a70f9d/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240623031247-2fea122027a7 h1:f04tDgg1+cWuoH+vkMeKtJ+glq2WhLG20MKFjogz8gQ= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240623031247-2fea122027a7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE= git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4= git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95 h1:3lO4ap9p7kEA+4yL5ojG9mAWsz5sY28Nu2tSzAZEehw= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95/go.mod h1:Fo4XOiZPua4E4/Qzy3ZYS5zyd15bS/lsb3t6S6PQFGY= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306 h1:Iy36ouA7TecVwBY1QTJfWIYNU2d39+APEfOhBC4gvic= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240520124754-3faebb9145e3 h1:fDDWQV3Xu/ntjrtKjwK30URPHr4kJJpqZocw52x+DKw= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240520124754-3faebb9145e3/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e h1:+PeWa2QdYBWnL32CfAAgy0dlaRCVNmYZDH4q+9w7Gfg= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e/go.mod h1:US/pcq2vstE3iyxIHf53w8IeXKkZys7bj/ozLWkRYeE= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= diff --git a/handle_map.go b/handle_map.go index 728afdc..2b38b85 100644 --- a/handle_map.go +++ b/handle_map.go @@ -8,8 +8,10 @@ package validator import ( + "errors" "fmt" "git.zhangdeman.cn/gateway/validator/define" + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/serialize" "github.com/tidwall/gjson" ) @@ -25,7 +27,7 @@ func handleMapStringFloat(inputVal any, rule *define.FieldRule) (map[string]floa res map[string]float64 ) - if err = strOrMapConvert(inputVal, &res); nil != err { + if err = strOrMapConvert(inputVal, rule.MapConfig, &res); nil != err { return nil, err } @@ -52,7 +54,7 @@ func handleMapStringAny(inputVal any, rule *define.FieldRule) (map[string]any, e res map[string]any ) - if err = strOrMapConvert(inputVal, &res); nil != err { + if err = strOrMapConvert(inputVal, rule.MapConfig, &res); nil != err { return nil, err } @@ -79,7 +81,7 @@ func handleMapStringString(inputVal any, rule *define.FieldRule) (map[string]any res map[string]string ) - if err = strOrMapConvert(inputVal, &res); nil != err { + if err = strOrMapConvert(inputVal, rule.MapConfig, &res); nil != err { return nil, err } @@ -108,7 +110,7 @@ func handleMapStringSlice(inputVal any, rule *define.FieldRule) (map[string][]an res map[string][]any ) - if err = strOrMapConvert(inputVal, &res); nil != err { + if err = strOrMapConvert(inputVal, rule.MapConfig, &res); nil != err { return nil, err } @@ -163,22 +165,44 @@ func handleMapAnyAny(inputVal any, rule *define.FieldRule) (map[any]any, error) // Author : go_developer@163.com<白茶清欢> // // Date : 17:26 2024/4/29 -func strOrMapConvert(inputVal any, receiver any) error { +func strOrMapConvert(inputVal any, mapConfig *define.MapConfig, receiver any) error { var ( err error ) - if inputValStr, ok := inputVal.(string); ok { - if err = serialize.JSON.UnmarshalWithNumber([]byte(inputValStr), receiver); nil != err { - return err + if nil == mapConfig { + if inputValStr, ok := inputVal.(string); ok { + if err = serialize.JSON.UnmarshalWithNumber([]byte(inputValStr), receiver); nil != err { + return err + } + } else { + byteData := serialize.JSON.MarshalForByte(inputVal) + if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err { + return err + } } - } else { + return nil + } + + if mapConfig.Mode == consts.DataMapModelMarshal { + if inputValStr, ok := inputVal.(string); ok { + if err = serialize.JSON.UnmarshalWithNumber([]byte(inputValStr), receiver); nil != err { + return err + } + return nil + } + return errors.New("input value is not marshal string") + } + + if mapConfig.Mode == consts.DataMapModelReal { byteData := serialize.JSON.MarshalForByte(inputVal) if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err { return err } + return nil } - return nil + + return errors.New(mapConfig.Mode + " : map mode is not support") } // validateMap 验证map数据 diff --git a/run.go b/run.go index 2780051..ba2e6d7 100644 --- a/run.go +++ b/run.go @@ -228,6 +228,16 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, // // Date : 14:43 2024/4/29 func handleData(inputVal any, rule *define.FieldRule) (any, error) { + rule.Type = strings.ToLower(rule.Type) + // 处理真实的map和序列化之后的map + if strings.HasPrefix(rule.Type, "map") { + if strings.HasSuffix(rule.Type, "_marshal") { + rule.MapConfig = &define.MapConfig{Mode: consts.DataMapModelMarshal} + } else { + rule.MapConfig = &define.MapConfig{Mode: consts.DataMapModelReal} + } + } + switch rule.Type { case consts.DataTypeAny: // 任意类型 return inputVal, nil