2025-02-13 16:03:31 +08:00
|
|
|
// Package api_doc ...
|
|
|
|
//
|
|
|
|
// Description : api_doc ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2025-02-13 15:26
|
|
|
|
package api_doc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
|
|
"reflect"
|
2025-02-13 21:11:27 +08:00
|
|
|
"strconv"
|
2025-02-13 16:03:31 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ValidateRule = validateRule{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type validateRule struct{}
|
|
|
|
|
|
|
|
// IsRequired 判断是否必传
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:32 2025/2/13
|
|
|
|
func (r validateRule) IsRequired(structField reflect.StructField) bool {
|
|
|
|
ruleTable := r.getValidateRuleTable(structField)
|
|
|
|
_, exist := ruleTable[consts.ValidatorRuleCommonRequired.String()]
|
|
|
|
// 存在即为必传
|
|
|
|
return exist
|
|
|
|
}
|
|
|
|
|
2025-02-13 18:27:31 +08:00
|
|
|
// Enum 获取枚举值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:23 2025/2/13
|
2025-02-13 21:11:27 +08:00
|
|
|
func (r validateRule) Enum(structField reflect.StructField) []any {
|
2025-02-13 18:27:31 +08:00
|
|
|
ruleTable := r.getValidateRuleTable(structField)
|
|
|
|
oneOfValue, _ := ruleTable[consts.ValidatorRuleCommonOneOf.String()]
|
|
|
|
if len(oneOfValue) == 0 {
|
2025-02-13 21:11:27 +08:00
|
|
|
return []any{}
|
2025-02-13 18:27:31 +08:00
|
|
|
}
|
2025-02-13 21:11:27 +08:00
|
|
|
fieldType := structField.Type.Kind().String()
|
|
|
|
valStrArr := strings.Split(oneOfValue, " ")
|
|
|
|
anySlice := make([]any, 0)
|
|
|
|
for _, val := range valStrArr {
|
|
|
|
if strings.HasPrefix(fieldType, "int") {
|
|
|
|
i, _ := strconv.Atoi(val)
|
|
|
|
anySlice = append(anySlice, i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "uint") {
|
|
|
|
uintVal, _ := strconv.ParseUint(val, 10, 64)
|
|
|
|
anySlice = append(anySlice, uintVal)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "float") {
|
|
|
|
floatVal, _ := strconv.ParseFloat(val, 64)
|
|
|
|
anySlice = append(anySlice, floatVal)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "string") {
|
|
|
|
anySlice = append(anySlice, val)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fieldType, "bool") {
|
|
|
|
if val == "true" {
|
|
|
|
anySlice = append(anySlice, true)
|
|
|
|
} else {
|
|
|
|
anySlice = append(anySlice, false)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return anySlice
|
2025-02-13 18:27:31 +08:00
|
|
|
}
|
|
|
|
|
2025-02-18 22:21:53 +08:00
|
|
|
// Minimum 最小值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:51 2025/2/18
|
|
|
|
func (r validateRule) Minimum(structField reflect.StructField) *int64 {
|
|
|
|
ruleTable := r.getValidateRuleTable(structField)
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
minVal, gteVal int64
|
|
|
|
minStr, gteStr string
|
|
|
|
minStrExist, gteStrExist bool
|
|
|
|
)
|
|
|
|
if minStr, minStrExist = ruleTable[consts.ValidatorRuleCommonMin.String()]; minStrExist && len(minStr) > 0 {
|
|
|
|
if minVal, err = strconv.ParseInt(minStr, 10, 64); nil != err {
|
|
|
|
panic("validate rule min val = " + minStr + " : " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gteStr, gteStrExist = ruleTable[consts.ValidateRuleGte.String()]; gteStrExist && len(gteStr) > 0 {
|
|
|
|
if gteVal, err = strconv.ParseInt(gteStr, 10, 64); nil != err {
|
|
|
|
panic("validate rule gte val = " + gteStr + " : " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !minStrExist && !gteStrExist {
|
|
|
|
// 未配置
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(minStr) > 0 {
|
|
|
|
if len(gteStr) > 0 {
|
|
|
|
if gteVal > minVal {
|
|
|
|
return &minVal
|
|
|
|
}
|
|
|
|
return >eVal
|
|
|
|
}
|
|
|
|
return &minVal
|
|
|
|
}
|
|
|
|
return >eVal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum 最大值
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:00 2025/2/18
|
|
|
|
func (r validateRule) Maximum(structField reflect.StructField) *int64 {
|
|
|
|
ruleTable := r.getValidateRuleTable(structField)
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
maxVal, gleVal int64
|
|
|
|
maxStr, gleStr string
|
|
|
|
maxStrExist, gleStrExist bool
|
|
|
|
)
|
|
|
|
if maxStr, maxStrExist = ruleTable[consts.ValidatorRuleCommonMax.String()]; maxStrExist && len(maxStr) > 0 {
|
|
|
|
if maxVal, err = strconv.ParseInt(maxStr, 10, 64); nil != err {
|
|
|
|
panic("validate rule min val = " + maxStr + " : " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gleStr, gleStrExist = ruleTable[consts.ValidateRuleLte.String()]; gleStrExist && len(gleStr) > 0 {
|
|
|
|
if gleVal, err = strconv.ParseInt(gleStr, 10, 64); nil != err {
|
|
|
|
panic("validate rule gte val = " + gleStr + " : " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !maxStrExist && !gleStrExist {
|
|
|
|
// 未配置
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(maxStr) > 0 {
|
|
|
|
if len(gleStr) > 0 {
|
|
|
|
if gleVal > maxVal {
|
|
|
|
return &gleVal
|
|
|
|
}
|
|
|
|
return &maxVal
|
|
|
|
}
|
|
|
|
return &maxVal
|
|
|
|
}
|
|
|
|
return &gleVal
|
|
|
|
}
|
|
|
|
|
2025-02-13 16:03:31 +08:00
|
|
|
// getValidateRuleTable 解析验证规则表
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:29 2025/2/13
|
|
|
|
func (r validateRule) getValidateRuleTable(structField reflect.StructField) map[string]string {
|
|
|
|
res := map[string]string{}
|
2025-02-18 10:08:45 +08:00
|
|
|
ruleStr := ParseStructFieldTag.GetValidateRule(structField)
|
2025-02-13 16:03:31 +08:00
|
|
|
if len(ruleStr) == 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
expressList := strings.Split(ruleStr, ",")
|
|
|
|
for _, item := range expressList {
|
|
|
|
if strings.Contains(item, "=") {
|
|
|
|
arr := strings.Split(item, "=")
|
|
|
|
res[strings.TrimSpace(arr[0])] = strings.Join(arr[1:], "=")
|
|
|
|
} else {
|
|
|
|
res[strings.TrimSpace(item)] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|