增加 map[string]string 处理

This commit is contained in:
白茶清欢 2024-05-17 14:08:52 +08:00
parent 67abdaeb7d
commit fad858143a
5 changed files with 42 additions and 4 deletions

View File

@ -21,7 +21,7 @@ type FieldRule struct {
AllowEmpty bool `json:"allow_empty"` // 必传时空字符串是否合法 AllowEmpty bool `json:"allow_empty"` // 必传时空字符串是否合法
AllowZero bool `json:"allow_zero"` // 必传数字, 0 是否合法 AllowZero bool `json:"allow_zero"` // 必传数字, 0 是否合法
AllowNil bool `json:"allow_nil"` // 必传时, nil值是否合法 AllowNil bool `json:"allow_nil"` // 必传时, nil值是否合法
AutoTrimSpace bool `json:"auto_trim_space"` // 自动去除前后空格 DisableAutoTrimSpace bool `json:"disable_auto_trim_space"` // 自动去除前后空格
DisableAutoConvert bool `json:"disable_auto_convert"` // 禁用自动格式转换, 不禁用情况下, ["1","2","3"] 可以转换成 [1,2,3] , "12.34" 可以转换成 12.34 DisableAutoConvert bool `json:"disable_auto_convert"` // 禁用自动格式转换, 不禁用情况下, ["1","2","3"] 可以转换成 [1,2,3] , "12.34" 可以转换成 12.34
RequiredConditionGroup [][]RequiredCondition `json:"required_condition_group"` // 满足何种条件,字段必传,不配置则为无差别必传, 组之间是或条件, 满足一组即命中, 组之内为与条件 RequiredConditionGroup [][]RequiredCondition `json:"required_condition_group"` // 满足何种条件,字段必传,不配置则为无差别必传, 组之间是或条件, 满足一组即命中, 组之内为与条件
ValueLimit *ValueLimit `json:"value_limit"` // 数据值的限制 ValueLimit *ValueLimit `json:"value_limit"` // 数据值的限制

5
go.mod
View File

@ -3,10 +3,10 @@ module git.zhangdeman.cn/gateway/validator
go 1.22.2 go 1.22.2
require ( require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95 git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1 github.com/tidwall/gjson v1.17.1
github.com/tidwall/sjson v1.2.5 github.com/tidwall/sjson v1.2.5
@ -14,6 +14,7 @@ require (
require ( require (
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect github.com/BurntSushi/toml v1.3.2 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect

6
go.sum
View File

@ -1,13 +1,19 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc h1:kPz9xiUVruM8kwbUUVpxyCTX8pGgyKt60K5zX77oyC4= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc h1:kPz9xiUVruM8kwbUUVpxyCTX8pGgyKt60K5zX77oyC4=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec h1:ENemx9RGAU9nqpPYC4S6C/Dnw9gwcx96+sTISG/6rsY=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec/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 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= 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 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-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4= 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-20231227095334-7eb5cdbf9253/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 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-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=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= 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.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=

View File

@ -68,6 +68,35 @@ func handleMapStringAny(inputVal any, rule *define.FieldRule) (map[string]any, e
return res, nil return res, nil
} }
// handleMapStringString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:07 2024/5/17
func handleMapStringString(inputVal any, rule *define.FieldRule) (map[string]any, error) {
var (
err error
res map[string]string
)
if err = strOrMapConvert(inputVal, &res); nil != err {
return nil, err
}
formatData := make(map[string]any)
dataFieldTable := make(map[string]string)
for k, v := range res {
dataFieldTable[k] = k
formatData[k] = v
}
if err = validateMap(dataFieldTable, rule); nil != err {
return nil, err
}
return formatData, nil
}
// handleMapStringSlice... // handleMapStringSlice...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

4
run.go
View File

@ -160,7 +160,7 @@ func formatInputVal(val gjson.Result, rule *define.FieldRule) (any, error) {
} }
} else { } else {
if inputValStr, ok := inputVal.(string); ok { if inputValStr, ok := inputVal.(string); ok {
if rule.AutoTrimSpace { if !rule.DisableAutoTrimSpace {
// 自动去空格 // 自动去空格
inputVal = strings.TrimSpace(inputValStr) inputVal = strings.TrimSpace(inputValStr)
} else { } else {
@ -234,6 +234,8 @@ func handleData(inputVal any, rule *define.FieldRule) (any, error) {
return handleMapStringFloat(inputVal, rule) return handleMapStringFloat(inputVal, rule)
case consts.DataTypeMapStrAny: // 对象结构 case consts.DataTypeMapStrAny: // 对象结构
return handleMapStringAny(inputVal, rule) return handleMapStringAny(inputVal, rule)
case consts.DataTypeMapStrStr: // 对象结构
return handleMapStringString(inputVal, rule)
case consts.DataTypeMapStrSlice: // map列表 case consts.DataTypeMapStrSlice: // map列表
return handleMapStringSlice(inputVal, rule) return handleMapStringSlice(inputVal, rule)
case consts.DataTypeMapAnyAny: // 任意类型map case consts.DataTypeMapAnyAny: // 任意类型map