validator/handle_map.go

232 lines
5.4 KiB
Go
Raw Normal View History

2024-05-01 22:35:07 +08:00
// Package validator ...
//
// Description : validator ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-01 22:33
package validator
import (
"errors"
2024-05-01 22:35:07 +08:00
"fmt"
"git.zhangdeman.cn/gateway/validator/define"
"git.zhangdeman.cn/zhangdeman/consts"
2024-05-01 22:35:07 +08:00
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/tidwall/gjson"
)
// handleMapStringFloat ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:38 2024/4/29
func handleMapStringFloat(inputVal any, rule *define.FieldRule) (map[string]float64, error) {
var (
err error
res map[string]float64
)
if err = strOrMapConvert(inputVal, rule.MapConfig, &res); nil != err {
2024-05-01 22:35:07 +08:00
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
}
// handleMapStringAny ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:19 2024/4/29
func handleMapStringAny(inputVal any, rule *define.FieldRule) (map[string]any, error) {
var (
err error
res map[string]any
)
if err = strOrMapConvert(inputVal, rule.MapConfig, &res); nil != err {
2024-05-01 22:35:07 +08:00
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
}
2024-05-17 14:08:52 +08:00
// 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, rule.MapConfig, &res); nil != err {
2024-05-17 14:08:52 +08:00
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
}
2024-05-01 22:35:07 +08:00
// 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, rule.MapConfig, &res); nil != err {
2024-05-01 22:35:07 +08:00
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 {
2024-10-24 21:57:29 +08:00
jsonRes = gjson.Parse(serialize.JSON.MarshalForStringIgnoreError(inputVal))
2024-05-01 22:35:07 +08:00
}
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
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:26 2024/4/29
func strOrMapConvert(inputVal any, mapConfig *define.MapConfig, receiver any) error {
2024-05-01 22:35:07 +08:00
var (
err error
)
if nil == mapConfig {
if inputValStr, ok := inputVal.(string); ok {
if err = serialize.JSON.UnmarshalWithNumber([]byte(inputValStr), receiver); nil != err {
return err
}
} else {
2024-10-24 21:57:29 +08:00
byteData := serialize.JSON.MarshalForByteIgnoreError(inputVal)
if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err {
return err
}
2024-05-01 22:35:07 +08:00
}
return nil
}
if mapConfig.Mode == consts.DataMapModelMarshal {
if inputValStr, ok := inputVal.(string); ok {
if err = serialize.JSON.UnmarshalWithNumber([]byte(inputValStr), receiver); nil != err {
return err
}
return nil
}
return errors.New("input value is not marshal string")
}
if mapConfig.Mode == consts.DataMapModelReal {
2024-10-24 21:57:29 +08:00
byteData := serialize.JSON.MarshalForByteIgnoreError(inputVal)
2024-05-01 22:35:07 +08:00
if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err {
return err
}
return nil
2024-05-01 22:35:07 +08:00
}
return errors.New(mapConfig.Mode + " : map mode is not support")
2024-05-01 22:35:07 +08:00
}
// validateMap 验证map数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:21 2024/4/29
func validateMap(dataFieldTable map[string]string, rule *define.FieldRule) error {
if nil == rule.ValueLimit {
return nil
}
if nil != rule.ValueLimit.Min && float64(len(dataFieldTable)) < *rule.ValueLimit.Min {
return fmt.Errorf("%v : data type is map, min item cnt is %v, real item cnt is %v", rule.Path, *rule.ValueLimit.Min, len(dataFieldTable))
}
if nil != rule.ValueLimit.Max && float64(len(dataFieldTable)) >= *rule.ValueLimit.Max {
return fmt.Errorf("%v : data type is map, max item cnt is %v, real item max is %v", rule.Path, *rule.ValueLimit.Max, len(dataFieldTable))
}
if nil != rule.ValueLimit.Map {
for _, itemField := range rule.ValueLimit.Map.IncludeFieldList {
if _, exist := dataFieldTable[itemField]; !exist {
return fmt.Errorf("%v : data type is map, %v field is required, but not found", rule.Path, itemField)
}
}
}
return nil
}