// Package validator ... // // Description : validator ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2024-05-01 22:33 package validator import ( "errors" "fmt" "git.zhangdeman.cn/gateway/validator/define" "git.zhangdeman.cn/zhangdeman/consts" "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 { 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 { 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 } // 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 { 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 } // 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 { 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.MarshalForStringIgnoreError(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 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:26 2024/4/29 func strOrMapConvert(inputVal any, mapConfig *define.MapConfig, receiver any) error { 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 { byteData := serialize.JSON.MarshalForByteIgnoreError(inputVal) if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err { return err } } 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 { byteData := serialize.JSON.MarshalForByteIgnoreError(inputVal) if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err { return err } return nil } return errors.New(mapConfig.Mode + " : map mode is not support") } // 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 }