rule

package
v0.19.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package rule 提供 Argus 的标签规则、跨字段规则和时间表达式能力

Index

Constants

This section is empty.

Variables

View Source
var BuiltinRules = map[string]BuiltinRule{}/* 126 elements not displayed */

BuiltinRules 内置规则映射表,evalTable 在 init 中合并此表和 dispatch 表

View Source
var StringRuleMap = validate.StringRuleMap

StringRuleMap 字符串规则映射表,VarString 快速路径直接查表

Functions

func CmpOpForRule

func CmpOpForRule(name string) constants.CmpOp

CmpOpForRule maps validation rules to comparison operators.

func CompareFieldDerefed

func CompareFieldDerefed(current, parent reflect.Value, targetPath, op string) bool

CompareFieldDerefed 将已解引用的当前字段和目标字段按操作符比较 调用方已对 current 做 DerefReflect,避免重复解引用

func CompareLengthOrNumber

func CompareLengthOrNumber(field reflect.Value, expect float64, op constants.CmpOp) bool

CompareLengthOrNumber 对字段值进行长度或数值比较

func CompareTimeExpr

func CompareTimeExpr(field reflect.Value, expr, op string, now time.Time) bool

CompareTimeExpr 将字段时间与表达式时间比较

func CompareValue

func CompareValue(left, right reflect.Value, op string) bool

CompareValue 按操作符比较两个值,优先支持时间,其次支持数值和字符串

func CompareValueOp

func CompareValueOp(left, right reflect.Value, cmpOp constants.CmpOp) bool

CompareValueOp 按预解析操作符比较两个值

func FieldByPath

func FieldByPath(root reflect.Value, path string) (reflect.Value, bool)

FieldByPath 根据字段路径读取结构体字段

func FieldContains

func FieldContains(field, parent reflect.Value, param string) bool

FieldContains 判断当前字段字符串是否包含目标字段的值

func FieldIndexByPath

func FieldIndexByPath(root reflect.Type, path string) ([]int, bool)

FieldIndexByPath 预解析字段路径,供结构体计划复用

func IsControlRule

func IsControlRule(name string) bool

IsControlRule 是否为流程控制规则,不参与规则求值

func IsRequiredIf

func IsRequiredIf(parent reflect.Value, parts []string) bool

IsRequiredIf 判断 required_if 条件是否触发

func IsRequiredWith

func IsRequiredWith(parent reflect.Value, param string) bool

IsRequiredWith 判断 required_with 条件是否触发

func IsRequiredWithAll

func IsRequiredWithAll(parent reflect.Value, parts []string) bool

IsRequiredWithAll 判断 required_with_all 条件是否触发(所有指定字段均非空)

func IsRequiredWithout

func IsRequiredWithout(parent reflect.Value, parts []string) bool

IsRequiredWithout 判断 required_without 条件是否触发(任一指定字段为空)

func IsRequiredWithoutAll

func IsRequiredWithoutAll(parent reflect.Value, parts []string) bool

IsRequiredWithoutAll 判断 required_without_all 条件是否触发(所有指定字段均为空)

func IsUniqueMap

func IsUniqueMap(field reflect.Value) bool

IsUniqueMap 判断 Map 值是否唯一

func IsUniqueSlice

func IsUniqueSlice(field reflect.Value) bool

IsUniqueSlice 判断切片元素是否唯一

func OneOfCIFast

func OneOfCIFast(field reflect.Value, parts []string) bool

OneOfCIFast 判断字段值是否在候选列表中(忽略大小写)

func OneOfFast

func OneOfFast(field reflect.Value, parts []string) bool

OneOfFast 判断字段值是否在候选列表中(精确匹配)

func Range

func Range(parent reflect.Value, param string) bool

Range 判断起始字段值是否小于结束字段值

func ResolveFieldValue

func ResolveFieldValue(field reflect.Value) (float64, bool)

ResolveFieldValue 解析字段的数值或长度

func ResolveTimeExpr

func ResolveTimeExpr(expr string, now time.Time) (time.Time, bool)

ResolveTimeExpr 解析 now、now+5m、now-30d 这类时间表达式

func RuleBoolean

func RuleBoolean(field reflect.Value, _ string, _ bool) bool

func RuleDefault

func RuleDefault(field reflect.Value, _ string, requiredStructEnabled bool) bool

func RuleEq

func RuleEq(field reflect.Value, param string, _ bool) bool

func RuleEqIgnoreCase

func RuleEqIgnoreCase(field reflect.Value, param string, _ bool) bool

func RuleGt

func RuleGt(field reflect.Value, param string, _ bool) bool

func RuleImage

func RuleImage(field reflect.Value, param string, _ bool) bool

RuleImage 校验字段为有效且完整的图片 param 为空表示任意支持格式,非空为空格分隔的格式白名单(如 image=png jpg),出现未知格式名时校验失败

func RuleInteger

func RuleInteger(field reflect.Value, _ string, _ bool) bool

RuleInteger 判断字段是否为整数

与 RuleNumber 的区别:

  • RuleNumber 接受任何数值(含浮点)
  • RuleInteger 要求整数:int/uint 类型直接通过,float 类型必须为整数值,字符串必须为纯整数

性能:零分配

  • int/uint 类型:O(1) switch 命中
  • float 类型:单次 IsWholeNumber 比较
  • string 类型:手动字节扫描(StringInteger),无 strconv 分配

func RuleJSON

func RuleJSON(field reflect.Value, _ string, _ bool) bool

func RuleLatitude

func RuleLatitude(field reflect.Value, _ string, _ bool) bool

func RuleLen

func RuleLen(field reflect.Value, param string, _ bool) bool

func RuleLongitude

func RuleLongitude(field reflect.Value, _ string, _ bool) bool

func RuleLt

func RuleLt(field reflect.Value, param string, _ bool) bool

func RuleMax

func RuleMax(field reflect.Value, param string, _ bool) bool

func RuleMin

func RuleMin(field reflect.Value, param string, _ bool) bool

func RuleNe

func RuleNe(field reflect.Value, param string, _ bool) bool

func RuleNeIgnoreCase

func RuleNeIgnoreCase(field reflect.Value, param string, _ bool) bool

func RuleNumber

func RuleNumber(field reflect.Value, _ string, _ bool) bool

func RuleRequired

func RuleRequired(field reflect.Value, _ string, requiredStructEnabled bool) bool

func RuleUnique

func RuleUnique(field reflect.Value, _ string, _ bool) bool

func SplitRuleOr

func SplitRuleOr(s string) []string

SplitRuleOr 按 | 分隔或规则,支持转义符

func TimeValue

func TimeValue(value reflect.Value, layout string) (time.Time, bool)

TimeValue 将字段值识别为 time.Time,核心包不直接依赖 protobuf

Types

type BuiltinRule

type BuiltinRule func(field reflect.Value, param string, requiredStructEnabled bool) bool

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 Rule

type Rule struct {
	Name  string
	Param string
	Raw   string
}

Rule 表示一个从 validate 标签中解析出来的规则

func ParseTag

func ParseTag(tag string) []Rule

ParseTag 解析 validate 标签,支持逗号分隔和 name=value 参数

type RuleKind

type RuleKind uint8

RuleKind 规则类别位掩码,解析期一次性预分类,运行期以 int 位测试替代字符串比较与查表

const (
	// KindScalarCompare 标量比较规则(min/max/len/gt/gte/lt/lte),求值路径最优先短路
	KindScalarCompare RuleKind = 1 << iota
	// KindControl 流程控制规则(omitempty/dive/keys 等),不产生求值
	KindControl
	// KindHasStr StringRuleMap 命中,StrFn 为预解析函数
	KindHasStr
	// KindHasBuiltin BuiltinRules 命中,Builtin 为预解析函数
	KindHasBuiltin
)

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 ParseRulePlan

func ParseRulePlan(item Rule) RulePlan

ParseRulePlan 将单条规则解析为运行计划,支持或规则展开

func ParseRules

func ParseRules(tag string) []RulePlan

ParseRules 将 validate 标签解析为规则计划列表

func ParseSingleRulePlan

func ParseSingleRulePlan(raw string) RulePlan

ParseSingleRulePlan 解析单条规则字符串为运行计划

func PrepareRulePlan

func PrepareRulePlan(rp RulePlan) RulePlan

PrepareRulePlan 对规则计划做后处理,预拆分需要空白分词的规则参数 同时完成类别预分类与函数预解析,类别判定的优先级与求值路径保持一致(标量比较最优先短路)

type StringRuleFunc

type StringRuleFunc = validate.StringRuleFunc

StringRuleFunc 字符串规则函数签名

type StructPlan

type StructPlan struct {
	Name   string      // 结构体名称
	Fields []FieldPlan // 字段计划列表
}

StructPlan 表示结构体的编译计划

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL