增加map[string][]any和map[any]any数据处理

This commit is contained in:
白茶清欢 2024-04-29 17:48:23 +08:00
parent 6f5aad9607
commit c4b8209839

93
run.go
View File

@ -26,13 +26,13 @@ import (
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:18 2024/4/29 // Date : 14:18 2024/4/29
func RunForStruct(sourceData interface{}, ruleList []*define.FieldRule) (map[string]interface{}, error) { func RunForStruct(sourceData any, ruleList []*define.FieldRule) (map[string]any, error) {
if nil == sourceData { if nil == sourceData {
return map[string]interface{}{}, nil return map[string]any{}, nil
} }
byteData, _ := json.Marshal(sourceData) byteData, _ := json.Marshal(sourceData)
var ( var (
sourceMapData map[string]interface{} sourceMapData map[string]any
err error err error
) )
@ -52,7 +52,7 @@ func RunForStruct(sourceData interface{}, ruleList []*define.FieldRule) (map[str
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:13 2024/4/29 // Date : 14:13 2024/4/29
func Run(sourceData map[string]interface{}, ruleList []*define.FieldRule) error { func Run(sourceData map[string]any, ruleList []*define.FieldRule) error {
if nil == sourceData || len(sourceData) == 0 { if nil == sourceData || len(sourceData) == 0 {
return nil return nil
} }
@ -72,7 +72,7 @@ func Run(sourceData map[string]interface{}, ruleList []*define.FieldRule) error
} }
} }
} }
sourceData = make(map[string]interface{}) sourceData = make(map[string]any)
d := json.NewDecoder(bytes.NewReader(byteData)) d := json.NewDecoder(bytes.NewReader(byteData))
d.UseNumber() d.UseNumber()
if err := d.Decode(&sourceData); nil != err { if err := d.Decode(&sourceData); nil != err {
@ -122,7 +122,7 @@ func getDataStatus(val gjson.Result, dataType string) string {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:32 2024/4/29 // Date : 14:32 2024/4/29
func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (interface{}, error) { func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, error) {
inputVal := val.Value() inputVal := val.Value()
if !val.Exists() { if !val.Exists() {
if rule.IsRequired { if rule.IsRequired {
@ -147,7 +147,7 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (inte
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:43 2024/4/29 // Date : 14:43 2024/4/29
func handleData(inputVal interface{}, rule *define.FieldRule) (interface{}, error) { func handleData(inputVal any, rule *define.FieldRule) (any, error) {
switch rule.Type { switch rule.Type {
case consts.DataTypeAny: // 任意类型 case consts.DataTypeAny: // 任意类型
return inputVal, nil return inputVal, nil
@ -164,9 +164,9 @@ func handleData(inputVal interface{}, rule *define.FieldRule) (interface{}, erro
case consts.DataTypeMapStrAny: // 对象结构 case consts.DataTypeMapStrAny: // 对象结构
return handleMapStringAny(inputVal, rule) return handleMapStringAny(inputVal, rule)
case consts.DataTypeMapStrSlice: // map列表 case consts.DataTypeMapStrSlice: // map列表
return handleMapStringSlice(inputVal, rule)
case consts.DataTypeMapAnyAny: // 任意类型map case consts.DataTypeMapAnyAny: // 任意类型map
return handleMapAnyAny(inputVal, rule)
} }
return nil, nil return nil, nil
} }
@ -176,7 +176,7 @@ func handleData(inputVal interface{}, rule *define.FieldRule) (interface{}, erro
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:29 2024/4/29 // Date : 15:29 2024/4/29
func handleFloat(inputVal interface{}, rule *define.FieldRule) (float64, error) { func handleFloat(inputVal any, rule *define.FieldRule) (float64, error) {
var ( var (
err error err error
formatData float64 formatData float64
@ -209,7 +209,7 @@ func handleFloat(inputVal interface{}, rule *define.FieldRule) (float64, error)
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:36 2024/4/29 // Date : 15:36 2024/4/29
func handleInt(inputVal interface{}, rule *define.FieldRule) (int64, error) { func handleInt(inputVal any, rule *define.FieldRule) (int64, error) {
var ( var (
err error err error
formatData int64 formatData int64
@ -228,7 +228,7 @@ func handleInt(inputVal interface{}, rule *define.FieldRule) (int64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:53 2024/4/29 // Date : 15:53 2024/4/29
func handleString(inputVal interface{}, rule *define.FieldRule) (string, error) { func handleString(inputVal any, rule *define.FieldRule) (string, error) {
var ( var (
err error err error
formatData string formatData string
@ -262,7 +262,7 @@ func handleString(inputVal interface{}, rule *define.FieldRule) (string, error)
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:38 2024/4/29 // Date : 16:38 2024/4/29
func handleMapStringFloat(inputVal interface{}, rule *define.FieldRule) (map[string]float64, error) { func handleMapStringFloat(inputVal any, rule *define.FieldRule) (map[string]float64, error) {
var ( var (
err error err error
res map[string]float64 res map[string]float64
@ -289,10 +289,10 @@ func handleMapStringFloat(inputVal interface{}, rule *define.FieldRule) (map[str
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:19 2024/4/29 // Date : 17:19 2024/4/29
func handleMapStringAny(inputVal interface{}, rule *define.FieldRule) (map[string]interface{}, error) { func handleMapStringAny(inputVal any, rule *define.FieldRule) (map[string]any, error) {
var ( var (
err error err error
res map[string]interface{} res map[string]any
) )
if err = strOrMapConvert(inputVal, &res); nil != err { if err = strOrMapConvert(inputVal, &res); nil != err {
@ -311,12 +311,73 @@ func handleMapStringAny(inputVal interface{}, rule *define.FieldRule) (map[strin
return res, nil return res, nil
} }
// handleMapStringSlice...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:42 2024/4/29
func handleMapStringSlice(inputVal any, rule *define.FieldRule) (map[string][]any, error) {
var (
err error
res map[string][]any
)
if err = strOrMapConvert(inputVal, &res); nil != err {
return nil, err
}
dataFieldTable := make(map[string]string)
for k, _ := range res {
dataFieldTable[k] = k
}
if err = validateMap(dataFieldTable, rule); nil != err {
return nil, err
}
return res, nil
}
// handleMapAnyAny map[any]any处理
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:33 2024/4/29
func handleMapAnyAny(inputVal any, rule *define.FieldRule) (map[any]any, error) {
var (
err error
res = map[any]any{}
jsonRes gjson.Result
)
if inputValStr, ok := inputVal.(string); ok {
jsonRes = gjson.Parse(inputValStr)
} else {
jsonRes = gjson.Parse(serialize.JSON.MarshalForString(inputVal))
}
if !jsonRes.IsObject() {
return nil, fmt.Errorf("%v : is not a map", rule.Path)
}
fieldTable := make(map[string]string)
jsonRes.ForEach(func(key, value gjson.Result) bool {
fieldTable[key.String()] = key.String()
res[key.Value()] = value.Value()
return true
})
if err = validateMap(fieldTable, rule); nil != err {
return nil, err
}
return res, nil
}
// strOrMapConvert 字符串或map转map // strOrMapConvert 字符串或map转map
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:26 2024/4/29 // Date : 17:26 2024/4/29
func strOrMapConvert(inputVal interface{}, receiver interface{}) error { func strOrMapConvert(inputVal any, receiver any) error {
var ( var (
err error err error
) )