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

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 数组处理