数据类型枚举值适配
This commit is contained in:
parent
4367435ab7
commit
0ece3237d7
@ -7,6 +7,8 @@
|
||||
// Date : 2024-04-29 10:51
|
||||
package define
|
||||
|
||||
import "git.zhangdeman.cn/zhangdeman/consts/enums"
|
||||
|
||||
// FieldRule 字段验证规则
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -14,7 +16,7 @@ package define
|
||||
// Date : 10:52 2024/4/29
|
||||
type FieldRule struct {
|
||||
Path string `json:"path"` // 字段路径
|
||||
Type string `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts
|
||||
Type enums.DataType `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts
|
||||
DisableRewrite bool `json:"disable_rewrite"` // 验证完相关数据类型之后, 不要重新给字段赋值
|
||||
DefaultValue any `json:"default_value"` // 默认值, 统一以字符串传入, 会转为最终设置的类型
|
||||
IsRequired bool `json:"is_required"` // 是否必传
|
||||
@ -36,7 +38,7 @@ type FieldRule struct {
|
||||
// Date : 10:58 2024/4/29
|
||||
type RequiredCondition struct {
|
||||
DependOnField string `json:"depend_on_field"` // 依赖数据源中的那一个字段
|
||||
DependOnFieldType string `json:"depend_on_field_type"` // 依赖数据源数据类型
|
||||
DependOnFieldType enums.DataType `json:"depend_on_field_type"` // 依赖数据源数据类型
|
||||
DependOnFieldStatus []string `json:"depend_on_field_status"` // 依赖数据状态 : NOT_FOUND / IS_NIL / IS_ZERO / IS_EMPTY / IS_FALSE
|
||||
}
|
||||
|
||||
|
8
go.mod
8
go.mod
@ -3,17 +3,17 @@ module git.zhangdeman.cn/gateway/validator
|
||||
go 1.22.2
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
github.com/tidwall/sjson v1.2.5
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba // indirect
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 // indirect
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
||||
|
8
go.sum
8
go.sum
@ -1,15 +1,23 @@
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba h1:hgajrPZGoDY9P+x6iqcS06pnu5t+N7DOfpmRwb+TZ4s=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558 h1:ZVJyyDKfYeA3TsN8UOi4IprkouK4wIIfCKe+F9byLWA=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558/go.mod h1:/9eicss/Dt9tp2jwZ/4cXDqDKo/Dez+HuT5/NGdqW+s=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254 h1:20NfKC/5EwTpSLodvXCPM2CmKWuDgggratYB2r9ihLM=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254/go.mod h1:7D7XZqEMCHSTnmDM4t3xWcrpJn1TC2EyDLd9ScP5gN4=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0 h1:aGQADmQKTF7c8+s3acnn569sTJwjlLhhhZfQ6f4nAH0=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0/go.mod h1:lvVOIrYDvCQHUzBdaOwKSrxT9ubcXQJBsafWMWu+I14=
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
|
17
run.go
17
run.go
@ -13,6 +13,7 @@ import (
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/gateway/validator/define"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/consts/enums"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
"strings"
|
||||
@ -126,7 +127,7 @@ func checkRuleConditionRequiredRule(sourceData []byte, rule *define.FieldRule) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:59 2024/4/29
|
||||
func getDataStatus(val gjson.Result, dataType string) string {
|
||||
func getDataStatus(val gjson.Result, dataType enums.DataType) string {
|
||||
if !val.Exists() {
|
||||
return consts.DataStatusNotFound
|
||||
}
|
||||
@ -142,17 +143,17 @@ func getDataStatus(val gjson.Result, dataType string) string {
|
||||
return consts.DataStatusIsZero
|
||||
}
|
||||
default:
|
||||
if strings.HasPrefix(dataType, "[]") {
|
||||
if strings.HasPrefix(dataType.String(), "[]") {
|
||||
// 数组
|
||||
if len(val.Array()) == 0 {
|
||||
return consts.DataStatusIsEmpty
|
||||
}
|
||||
} else if strings.HasPrefix(dataType, "map") {
|
||||
} else if strings.HasPrefix(dataType.String(), "map") {
|
||||
// 对象
|
||||
if len(val.Map()) == 0 {
|
||||
return consts.DataStatusIsEmpty
|
||||
}
|
||||
} else if strings.HasPrefix(dataType, "*") {
|
||||
} else if strings.HasPrefix(dataType.String(), "*") {
|
||||
// 指针类型
|
||||
if nil == val.Value() {
|
||||
return consts.DataStatusIsNil
|
||||
@ -227,7 +228,7 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
|
||||
if rule.IsRequired {
|
||||
return nil, fmt.Errorf("%v : field is required, but not found", rule.Path)
|
||||
}
|
||||
if strings.HasSuffix(rule.Type, "_ptr") {
|
||||
if strings.HasSuffix(rule.Type.String(), "_ptr") {
|
||||
// 指针类型数据, 无需验证
|
||||
return nil, nil
|
||||
}
|
||||
@ -246,10 +247,10 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
|
||||
//
|
||||
// Date : 14:43 2024/4/29
|
||||
func handleData(inputVal any, rule *define.FieldRule) (any, error) {
|
||||
rule.Type = strings.ToLower(rule.Type)
|
||||
rule.Type = enums.DataType(strings.ToLower(rule.Type.String()))
|
||||
// 处理真实的map和序列化之后的map
|
||||
if strings.HasPrefix(rule.Type, "map") {
|
||||
if strings.HasSuffix(rule.Type, "_marshal") {
|
||||
if strings.HasPrefix(rule.Type.String(), "map") {
|
||||
if strings.HasSuffix(rule.Type.String(), "_marshal") {
|
||||
rule.MapConfig = &define.MapConfig{Mode: consts.DataMapModelMarshal}
|
||||
} else {
|
||||
rule.MapConfig = &define.MapConfig{Mode: consts.DataMapModelReal}
|
||||
|
Loading…
Reference in New Issue
Block a user