v1版本数据验证 + 转换

This commit is contained in:
白茶清欢 2024-05-01 21:44:59 +08:00
parent 2de1fb0c0c
commit 9c7886dec3
3 changed files with 93 additions and 12 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module git.zhangdeman.cn/gateway/validator
go 1.22.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240430135109-0be82b4a6434 // indirect
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501132050-6c507d6b9c99 // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect

2
go.sum
View File

@ -6,6 +6,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240429082641-eeef7e967d00 h1:bOPZXY
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240429082641-eeef7e967d00/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240430135109-0be82b4a6434 h1:BgpbeE3Vuy1iS4xrMzJP6bDGbrrhlv8uSDh8n/Sj+fg=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240430135109-0be82b4a6434/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501132050-6c507d6b9c99 h1:rskqw1MOJ7b2qvGZLJ8S9POZFqvcaeaCnBp4FStIiyI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501132050-6c507d6b9c99/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/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=

101
run.go
View File

@ -172,10 +172,20 @@ func handleData(inputVal any, rule *define.FieldRule) (any, error) {
return handleMapStringSlice(inputVal, rule)
case consts.DataTypeMapAnyAny: // 任意类型map
return handleMapAnyAny(inputVal, rule)
case consts.DataTypeSliceInt, consts.DataTypeSliceIntWithChar: // int数组处理
return handleSliceInt(inputVal, rule)
case consts.DataTypeSliceUint, consts.DataTypeSliceUintWithChar: // uint数组处理
return handleSliceUint(inputVal, rule)
case consts.DataTypeSliceFloat, consts.DataTypeSliceFloatWithChar: // float数组处理
return handleSliceFloat(inputVal, rule)
case consts.DataTypeSliceBool, consts.DataTypeSliceBoolWithChar: // bool数组
return handleSliceBool(inputVal, rule)
case consts.DataTypeSliceMapAnyAny: // map 列表
return handleSliceMapAny(inputVal, rule)
case consts.DataTypeSliceMapStringAny:
return handleSliceMapString(inputVal, rule)
}
return nil, nil
return nil, fmt.Errorf("%v : data type [%v] is not support", rule.Path, rule.Type)
}
// handleFloat 处理float数据
@ -260,7 +270,7 @@ func handleBool(inputVal any, rule *define.FieldRule) (bool, error) {
formatData bool
)
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
return 0, err
return false, err
}
if _, err = handleFloat(formatData, rule); nil != err {
return false, err
@ -478,8 +488,15 @@ func handleSliceInt(inputValue interface{}, rule *define.FieldRule) ([]int64, er
if anySlice, err = handleSlice(inputValue, rule); nil != err {
return nil, err
}
return nil, nil
intSlice := make([]int64, 0)
for _, item := range anySlice {
var itemInt int64
if err = util.ConvertAssign(&itemInt, item); nil != err {
return nil, fmt.Errorf("%v : data type expect int, but convert fail : %v", rule.Path, err.Error())
}
intSlice = append(intSlice, itemInt)
}
return intSlice, nil
}
// handleSliceUint ...
@ -495,7 +512,15 @@ func handleSliceUint(inputValue interface{}, rule *define.FieldRule) ([]uint64,
if anySlice, err = handleSlice(inputValue, rule); nil != err {
return nil, err
}
return nil, nil
uintSlice := make([]uint64, 0)
for _, item := range anySlice {
var itemUint uint64
if err = util.ConvertAssign(&itemUint, item); nil != err {
return nil, fmt.Errorf("%v : data type expect uint, but convert fail : %v", rule.Path, err.Error())
}
uintSlice = append(uintSlice, itemUint)
}
return uintSlice, nil
}
// handleSliceBool ...
@ -511,7 +536,15 @@ func handleSliceBool(inputValue interface{}, rule *define.FieldRule) ([]bool, er
if anySlice, err = handleSlice(inputValue, rule); nil != err {
return nil, err
}
return nil, nil
boolSlice := make([]bool, 0)
for _, item := range anySlice {
var itemBool bool
if err = util.ConvertAssign(&itemBool, item); nil != err {
return nil, fmt.Errorf("%v : data type expect bool, but convert fail : %v", rule.Path, err.Error())
}
boolSlice = append(boolSlice, itemBool)
}
return boolSlice, nil
}
// handleSliceFloat ...
@ -527,7 +560,15 @@ func handleSliceFloat(inputValue interface{}, rule *define.FieldRule) ([]float64
if anySlice, err = handleSlice(inputValue, rule); nil != err {
return nil, err
}
return nil, nil
floatSlice := make([]float64, 0)
for _, item := range anySlice {
var itemFloat float64
if err = util.ConvertAssign(&itemFloat, item); nil != err {
return nil, fmt.Errorf("%v : data type expect float, but convert fail : %v", rule.Path, err.Error())
}
floatSlice = append(floatSlice, itemFloat)
}
return floatSlice, nil
}
// handleSliceSlice ...
@ -546,12 +587,12 @@ func handleSliceSlice(inputValue interface{}, rule *define.FieldRule) ([][]any,
return nil, nil
}
// handleSliceMap ...
// handleSliceMapAny ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:30 2024/4/30
func handleSliceMap(inputValue interface{}, rule *define.FieldRule) ([]map[any]any, error) {
func handleSliceMapAny(inputValue interface{}, rule *define.FieldRule) ([]map[any]any, error) {
var (
anySlice []any
err error
@ -559,7 +600,45 @@ func handleSliceMap(inputValue interface{}, rule *define.FieldRule) ([]map[any]a
if anySlice, err = handleSlice(inputValue, rule); nil != err {
return nil, err
}
return nil, nil
mapSlice := make([]map[any]any, 0)
for _, item := range anySlice {
byteData := serialize.JSON.MarshalForByte(item)
jsonRes := gjson.ParseBytes(byteData)
res := make(map[any]any)
jsonRes.ForEach(func(key, value gjson.Result) bool {
res[key.Value()] = value.Value()
return true
})
mapSlice = append(mapSlice, res)
}
return mapSlice, nil
}
// handleSliceMapString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:33 2024/5/1
func handleSliceMapString(inputValue interface{}, rule *define.FieldRule) ([]map[string]any, error) {
var (
anySlice []any
err error
)
if anySlice, err = handleSlice(inputValue, rule); nil != err {
return nil, err
}
mapSlice := make([]map[string]any, 0)
for _, item := range anySlice {
byteData := serialize.JSON.MarshalForByte(item)
jsonRes := gjson.ParseBytes(byteData)
res := make(map[string]any)
jsonRes.ForEach(func(key, value gjson.Result) bool {
res[key.String()] = value.Value()
return true
})
mapSlice = append(mapSlice, res)
}
return mapSlice, nil
}
// handleSlice 数组处理