增加slice的公共逻辑处理
This commit is contained in:
108
run.go
108
run.go
@ -18,6 +18,7 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -155,8 +156,12 @@ func handleData(inputVal any, rule *define.FieldRule) (any, error) {
|
||||
return handleFloat(inputVal, rule)
|
||||
case consts.DataTypeInt: // int类型
|
||||
return handleInt(inputVal, rule)
|
||||
case consts.DataTypeUint:
|
||||
return handleUint(inputVal, rule)
|
||||
case consts.DataTypeString: // 字符串处理
|
||||
return handleString(inputVal, rule)
|
||||
case consts.DataTypeBool:
|
||||
return handleBool(inputVal, rule)
|
||||
case consts.DataTypeMapStrFloat, consts.DataTypeMapStrBool,
|
||||
consts.DataTypeMapStrInt, consts.DataTypeMapStrUint:
|
||||
// 一律按照 map[string]float64处理
|
||||
@ -167,6 +172,8 @@ func handleData(inputVal any, rule *define.FieldRule) (any, error) {
|
||||
return handleMapStringSlice(inputVal, rule)
|
||||
case consts.DataTypeMapAnyAny: // 任意类型map
|
||||
return handleMapAnyAny(inputVal, rule)
|
||||
case consts.DataTypeSliceUint, consts.DataTypeSliceUintWithChar: // uint数组处理
|
||||
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@ -223,6 +230,44 @@ func handleInt(inputVal any, rule *define.FieldRule) (int64, error) {
|
||||
return formatData, nil
|
||||
}
|
||||
|
||||
// handleUint ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:28 2024/4/30
|
||||
func handleUint(inputVal any, rule *define.FieldRule) (uint64, error) {
|
||||
var (
|
||||
err error
|
||||
formatData uint64
|
||||
)
|
||||
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
if _, err = handleFloat(formatData, rule); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return formatData, nil
|
||||
}
|
||||
|
||||
// handleBool...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:26 2024/4/30
|
||||
func handleBool(inputVal any, rule *define.FieldRule) (bool, error) {
|
||||
var (
|
||||
err error
|
||||
formatData bool
|
||||
)
|
||||
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
if _, err = handleFloat(formatData, rule); nil != err {
|
||||
return false, err
|
||||
}
|
||||
return formatData, nil
|
||||
}
|
||||
|
||||
// handleString ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -419,3 +464,66 @@ func validateMap(dataFieldTable map[string]string, rule *define.FieldRule) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleSlice 数组处理
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:30 2024/4/30
|
||||
func handleSlice(inputValue interface{}, rule *define.FieldRule) ([]any, error) {
|
||||
inputValType := reflect.TypeOf(inputValue).Kind()
|
||||
if inputValType != reflect.Slice && inputValType != reflect.String {
|
||||
return nil, fmt.Errorf("%v : data type is expect slice or string, but get %v", rule.Path, inputValType.String())
|
||||
}
|
||||
if inputValType == reflect.Slice {
|
||||
inputValue = serialize.JSON.MarshalForString(inputValue)
|
||||
// 重置配置
|
||||
if nil == rule.SliceConfig {
|
||||
rule.SliceConfig = &define.SliceConfig{
|
||||
Mode: consts.DataSliceModelMarshal,
|
||||
DisableAutoConvert: false,
|
||||
SplitChar: "",
|
||||
}
|
||||
} else {
|
||||
rule.SliceConfig.Mode = consts.DataSliceModelMarshal
|
||||
}
|
||||
}
|
||||
if inputStr, ok := inputValue.(string); ok {
|
||||
return handleSliceString(inputStr, rule)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v : data type is expect slice or string, but get %v", rule.Path, inputValType.String())
|
||||
}
|
||||
|
||||
// handleSliceString ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:06 2024/4/30
|
||||
func handleSliceString(inputStr string, rule *define.FieldRule) ([]any, error) {
|
||||
if nil == rule.SliceConfig {
|
||||
return nil, fmt.Errorf("%v : data type is slice, but get string", rule.Path)
|
||||
}
|
||||
if rule.SliceConfig.Mode == consts.DataSliceModelReal {
|
||||
return nil, fmt.Errorf("%v : data type expect real slice, but get string", rule.Path)
|
||||
}
|
||||
if rule.SliceConfig.Mode == consts.DataSliceModelMarshal { // json序列化之后的
|
||||
var (
|
||||
err error
|
||||
res []any
|
||||
)
|
||||
if err = serialize.JSON.UnmarshalWithNumber([]byte(inputStr), &res); nil != err {
|
||||
return nil, fmt.Errorf("%v : data type expect marshal slice, but can not convert", rule.Path)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
if rule.SliceConfig.Mode == consts.DataSliceModelWithSplitChar { // 指定字符串切割
|
||||
strArr := strings.Split(inputStr, rule.SliceConfig.SplitChar)
|
||||
anyArr := make([]any, 0)
|
||||
for _, item := range strArr {
|
||||
anyArr = append(anyArr, item)
|
||||
}
|
||||
return anyArr, nil
|
||||
}
|
||||
return nil, fmt.Errorf("%v : data type is slice, but rule mode [%v] is not supported", rule.Path, rule.SliceConfig.Mode)
|
||||
}
|
||||
|
Reference in New Issue
Block a user