Documentation
¶
Overview ¶
Package rule 提供 Argus 的标签规则、跨字段规则和时间表达式能力
Index ¶
- Variables
- func CmpOpForRule(name string) constants.CmpOp
- func CompareFieldDerefed(current, parent reflect.Value, targetPath, op string) bool
- func CompareLengthOrNumber(field reflect.Value, expect float64, op constants.CmpOp) bool
- func CompareTimeExpr(field reflect.Value, expr, op string, now time.Time) bool
- func CompareValue(left, right reflect.Value, op string) bool
- func CompareValueOp(left, right reflect.Value, cmpOp constants.CmpOp) bool
- func FieldByPath(root reflect.Value, path string) (reflect.Value, bool)
- func FieldContains(field, parent reflect.Value, param string) bool
- func FieldIndexByPath(root reflect.Type, path string) ([]int, bool)
- func IsControlRule(name string) bool
- func IsRequiredIf(parent reflect.Value, parts []string) bool
- func IsRequiredWith(parent reflect.Value, param string) bool
- func IsRequiredWithAll(parent reflect.Value, parts []string) bool
- func IsRequiredWithout(parent reflect.Value, parts []string) bool
- func IsRequiredWithoutAll(parent reflect.Value, parts []string) bool
- func IsUniqueMap(field reflect.Value) bool
- func IsUniqueSlice(field reflect.Value) bool
- func OneOfCIFast(field reflect.Value, parts []string) bool
- func OneOfFast(field reflect.Value, parts []string) bool
- func Range(parent reflect.Value, param string) bool
- func ResolveFieldValue(field reflect.Value) (float64, bool)
- func ResolveTimeExpr(expr string, now time.Time) (time.Time, bool)
- func RuleBoolean(field reflect.Value, _ string, _ bool) bool
- func RuleDefault(field reflect.Value, _ string, requiredStructEnabled bool) bool
- func RuleEq(field reflect.Value, param string, _ bool) bool
- func RuleEqIgnoreCase(field reflect.Value, param string, _ bool) bool
- func RuleGt(field reflect.Value, param string, _ bool) bool
- func RuleImage(field reflect.Value, param string, _ bool) bool
- func RuleInteger(field reflect.Value, _ string, _ bool) bool
- func RuleJSON(field reflect.Value, _ string, _ bool) bool
- func RuleLatitude(field reflect.Value, _ string, _ bool) bool
- func RuleLen(field reflect.Value, param string, _ bool) bool
- func RuleLongitude(field reflect.Value, _ string, _ bool) bool
- func RuleLt(field reflect.Value, param string, _ bool) bool
- func RuleMax(field reflect.Value, param string, _ bool) bool
- func RuleMin(field reflect.Value, param string, _ bool) bool
- func RuleNe(field reflect.Value, param string, _ bool) bool
- func RuleNeIgnoreCase(field reflect.Value, param string, _ bool) bool
- func RuleNumber(field reflect.Value, _ string, _ bool) bool
- func RuleRequired(field reflect.Value, _ string, requiredStructEnabled bool) bool
- func RuleUnique(field reflect.Value, _ string, _ bool) bool
- func SplitRuleOr(s string) []string
- func TimeValue(value reflect.Value, layout string) (time.Time, bool)
- type BuiltinRule
- type FieldNames
- type FieldPlan
- type Rule
- type RuleKind
- type RulePlan
- type StringRuleFunc
- type StructPlan
Constants ¶
This section is empty.
Variables ¶
var BuiltinRules = map[string]BuiltinRule{}/* 126 elements not displayed */
BuiltinRules 内置规则映射表,evalTable 在 init 中合并此表和 dispatch 表
var StringRuleMap = validate.StringRuleMap
StringRuleMap 字符串规则映射表,VarString 快速路径直接查表
Functions ¶
func CmpOpForRule ¶
CmpOpForRule maps validation rules to comparison operators.
func CompareFieldDerefed ¶
CompareFieldDerefed 将已解引用的当前字段和目标字段按操作符比较 调用方已对 current 做 DerefReflect,避免重复解引用
func CompareLengthOrNumber ¶
CompareLengthOrNumber 对字段值进行长度或数值比较
func CompareTimeExpr ¶
CompareTimeExpr 将字段时间与表达式时间比较
func CompareValue ¶
CompareValue 按操作符比较两个值,优先支持时间,其次支持数值和字符串
func CompareValueOp ¶
CompareValueOp 按预解析操作符比较两个值
func FieldByPath ¶
FieldByPath 根据字段路径读取结构体字段
func FieldContains ¶
FieldContains 判断当前字段字符串是否包含目标字段的值
func FieldIndexByPath ¶
FieldIndexByPath 预解析字段路径,供结构体计划复用
func IsRequiredIf ¶
IsRequiredIf 判断 required_if 条件是否触发
func IsRequiredWith ¶
IsRequiredWith 判断 required_with 条件是否触发
func IsRequiredWithAll ¶
IsRequiredWithAll 判断 required_with_all 条件是否触发(所有指定字段均非空)
func IsRequiredWithout ¶
IsRequiredWithout 判断 required_without 条件是否触发(任一指定字段为空)
func IsRequiredWithoutAll ¶
IsRequiredWithoutAll 判断 required_without_all 条件是否触发(所有指定字段均为空)
func OneOfCIFast ¶
OneOfCIFast 判断字段值是否在候选列表中(忽略大小写)
func ResolveFieldValue ¶
ResolveFieldValue 解析字段的数值或长度
func ResolveTimeExpr ¶
ResolveTimeExpr 解析 now、now+5m、now-30d 这类时间表达式
func RuleImage ¶
RuleImage 校验字段为有效且完整的图片 param 为空表示任意支持格式,非空为空格分隔的格式白名单(如 image=png jpg),出现未知格式名时校验失败
func RuleInteger ¶
RuleInteger 判断字段是否为整数
与 RuleNumber 的区别:
- RuleNumber 接受任何数值(含浮点)
- RuleInteger 要求整数:int/uint 类型直接通过,float 类型必须为整数值,字符串必须为纯整数
性能:零分配
- int/uint 类型:O(1) switch 命中
- float 类型:单次 IsWholeNumber 比较
- string 类型:手动字节扫描(StringInteger),无 strconv 分配
func RuleRequired ¶
Types ¶
type BuiltinRule ¶
BuiltinRule 内置规则函数签名,接收反射值、参数和 requiredStructEnabled 标志
type FieldNames ¶
type FieldNames struct {
NS string // 展示命名空间,形如 Type.Field
StructNS string // 结构体命名空间,形如 Type.GoField
Field string // 展示名(json tag 或自定义)
StructField string // Go 字段名
}
FieldNames 字段的命名空间与名称四元组,编译期一次填好,求值路径按指针直取
原先这四个串是摊开传的,applyRules 的入参恰好 24 字;arm64 ABIInternal 只有 16 个整数入参寄存器,超出部分每次调用都要落栈再取回,而「全部通过」的热路径 根本不读它们——只有错误构造、dive 子路径拼接和自定义规则上下文才需要。 收进本结构体后 applyRules 降到 13 字、evalRule 降到 7 字,帧也相应变小
注:dive 子元素的命名空间仍由 applyRules/applyDive 以 ns/structNs 形参单独传, 不另建一个 FieldNames 局部变量——applyRules 与 applyDive 互递归, 局部变量取址后会流进这个强连通分量里的形参,逃逸分析收敛不了, 会把它按出错元素逐个搬上堆(实测 dive 失败路径 6→7 次分配)
type FieldPlan ¶
type FieldPlan struct {
Index []int // 字段索引路径
Name string // Go 字段名
AltName string // 展示名(json tag 或自定义)
Typ reflect.Type
Rules []RulePlan // 字段上的规则列表
HasValidate bool // 是否包含校验标签
MayDiveStruct bool // 是否可能递归进入结构体字段
NsPrefix string // 命名空间前缀
StructNsPrefix string // 结构体命名空间前缀
// Names 编译期预填的四元组,供求值路径按指针直取;
// 与上面四个字段同源,仅为省掉每字段一次装配而冗余存一份
Names FieldNames
}
FieldPlan 表示结构体字段的校验计划
type RulePlan ¶
type RulePlan struct {
Name string // 规则名称
Param string // 规则参数原始值
ParamParts []string // 规则参数按空白拆分
OrRules []RulePlan // 或规则列表(对应 | 分隔符)
CmpOp constants.CmpOp
HasCmpOp bool
FieldIndex []int
Number float64
HasNumber bool
Kind RuleKind // 类别位掩码
StrFn StringRuleFunc // StringRuleMap 预解析函数
Builtin BuiltinRule // BuiltinRules 预解析函数
}
RulePlan 表示一条规则的运行计划
func ParseSingleRulePlan ¶
ParseSingleRulePlan 解析单条规则字符串为运行计划
func PrepareRulePlan ¶
PrepareRulePlan 对规则计划做后处理,预拆分需要空白分词的规则参数 同时完成类别预分类与函数预解析,类别判定的优先级与求值路径保持一致(标量比较最优先短路)
type StructPlan ¶
StructPlan 表示结构体的编译计划