rules

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitSpecs

func InitSpecs(cfg config.Config, a *search.AST) error

InitSpecs loads the rule specs for function rules implemented by the github.com/frk/valid package and then initializes custom rules from the given config.

InitSpecs should be invoked only once and before starting the first rule-check.

Types

type Arg

type Arg struct {
	// The type of the argument.
	Type ArgType
	// The literal string representation of the value.
	Value string
}

Arg represents an argument for a rule.

func (*Arg) CanAssignTo

func (a *Arg) CanAssignTo(t *gotype.Type, keyMap map[string]*FieldNode) bool

CanAssignTo reports whether or not the Arg can be assigned to the Go type represented by t. The keyMap, if provided, is used to resolve ARG_FIELD args.

func (*Arg) IsEmpty

func (a *Arg) IsEmpty() bool

IsEmpty reports whether or not Arg value is empty.

func (*Arg) IsNumeric

func (a *Arg) IsNumeric() bool

IsNumeric reports whether or not Arg value is numeric.

func (*Arg) IsUInt

func (a *Arg) IsUInt() bool

IsUInt reports whether or not Arg value is a valid uint "candidate".

type ArgType

type ArgType uint

ArgType indicates the type of a rule argument value.

const (
	ARG_UNKNOWN ArgType = iota
	ARG_BOOL
	ARG_INT
	ARG_FLOAT
	ARG_STRING
	ARG_FIELD_ABS
	ARG_FIELD_REL
)

func (ArgType) String

func (t ArgType) String() string

type Checker

type Checker struct {
	*Info
	// contains filtered or unexported fields
}

Checker maintains the state of the rule checker.

func NewChecker

func NewChecker(ast *search.AST, pkg search.Pkg, fkCfg *config.FieldKeyConfig, info *Info) (c *Checker)

NewChecker returns a new Checker instance. The optional info argument, will be populated during rule-checking.

func (*Checker) Check

func (c *Checker) Check(match *search.Match) error

Check rule-checks the validator struct represented by the given *search.Match and returns the first error encountered.

type ErrSpec

type ErrSpec struct {
	// The text of the error message.
	Text string
	// If true the generated error message
	// will include the rule's arguments.
	WithArgs bool
	// The separator used to join the rule's
	// arguments for the error message.
	ArgSep string
	// The text to be appended after the list of arguments.
	ArgSuffix string
}

type Error

type Error struct {
	C ErrorCode
	// contains filtered or unexported fields
}

func (*Error) CfgArgBounds

func (e *Error) CfgArgBounds() string

func (*Error) CfgArgKey

func (e *Error) CfgArgKey() string

func (*Error) CfgArgNum

func (e *Error) CfgArgNum() string

func (*Error) CfgArgType

func (e *Error) CfgArgType() string

func (*Error) CfgArgValue

func (e *Error) CfgArgValue() string

func (*Error) CfgArgWord

func (e *Error) CfgArgWord() string

func (*Error) CfgErrYAML

func (e *Error) CfgErrYAML() string

func (*Error) CfgFuncParamType

func (e *Error) CfgFuncParamType() string

func (*Error) CfgJoinOp

func (e *Error) CfgJoinOp() string

func (*Error) CfgRuleName

func (e *Error) CfgRuleName() string

func (*Error) ConfigFile

func (e *Error) ConfigFile() string

func (*Error) CustomFuncIdent

func (e *Error) CustomFuncIdent() string

func (*Error) ErrType

func (e *Error) ErrType() string

func (*Error) Error

func (e *Error) Error() string

func (*Error) Field

func (e *Error) Field() string

func (*Error) FieldName

func (e *Error) FieldName() string

func (*Error) FieldPos

func (e *Error) FieldPos() string

func (*Error) FieldType

func (e *Error) FieldType() string

func (*Error) FieldTypeFamily

func (e *Error) FieldTypeFamily() string

func (*Error) FuncIdent

func (e *Error) FuncIdent() string

func (*Error) FuncName

func (e *Error) FuncName() string

func (*Error) FuncParamIdent

func (e *Error) FuncParamIdent() string

func (*Error) FuncParamIndex

func (e *Error) FuncParamIndex() string

func (*Error) FuncParamNum

func (e *Error) FuncParamNum() string

func (*Error) FuncParamWord

func (e *Error) FuncParamWord() string

func (*Error) FuncPos

func (e *Error) FuncPos() string

func (*Error) FuncType

func (e *Error) FuncType() string

func (*Error) HasConfigFile

func (e *Error) HasConfigFile() bool

func (*Error) HasOriginalError added in v1.2.1

func (e *Error) HasOriginalError() bool

func (*Error) OriginalError

func (e *Error) OriginalError() string

func (*Error) Rule

func (e *Error) Rule() string

func (*Error) Rule2Name

func (e *Error) Rule2Name() string

func (*Error) RuleArgFieldType

func (e *Error) RuleArgFieldType() string

func (*Error) RuleArgIsField

func (e *Error) RuleArgIsField() bool

func (*Error) RuleArgNum added in v1.2.1

func (e *Error) RuleArgNum() string

func (*Error) RuleArgType

func (e *Error) RuleArgType() string

func (*Error) RuleArgValue

func (e *Error) RuleArgValue() string

func (*Error) RuleArgs

func (e *Error) RuleArgs() string

func (*Error) RuleFuncIdent

func (e *Error) RuleFuncIdent() string

func (*Error) RuleFuncIn0Type

func (e *Error) RuleFuncIn0Type() string

func (*Error) RuleFuncName added in v1.1.0

func (e *Error) RuleFuncName() string

func (*Error) RuleFuncNameWithType added in v1.1.0

func (e *Error) RuleFuncNameWithType() string

func (*Error) RuleFuncOut0Type

func (e *Error) RuleFuncOut0Type() string

func (*Error) RuleFuncParamType

func (e *Error) RuleFuncParamType() string

func (*Error) RuleFuncType

func (e *Error) RuleFuncType() string

func (*Error) RuleName

func (e *Error) RuleName() string

func (*Error) RuleSpecArgMax added in v1.2.1

func (e *Error) RuleSpecArgMax() string

func (*Error) RuleSpecArgMin added in v1.2.1

func (e *Error) RuleSpecArgMin() string

func (*Error) RuleSpecKind added in v1.2.1

func (e *Error) RuleSpecKind() string

func (*Error) Tag

func (e *Error) Tag() string

func (*Error) TagElem

func (e *Error) TagElem() string

func (*Error) TagKey

func (e *Error) TagKey() string

func (*Error) TagSTKey

func (e *Error) TagSTKey() string

func (*Error) Type

func (e *Error) Type() string

type ErrorCode

type ErrorCode uint
const (
	ERR_CONFIG_FUNCID       ErrorCode // bad function identifier format
	ERR_CONFIG_FUNCSEARCH             // function search failed
	ERR_CONFIG_INVALID                // failed to unmarshal function's config.RuleConfig
	ERR_CONFIG_MISSING                // missing function's config.RuleConfig
	ERR_CONFIG_NONAME                 // rule config with no rule name
	ERR_CONFIG_RESERVED               // illegal use of reserved rule name
	ERR_CONFIG_FUNCTYPE               // bad function signature for "is" rule
	ERR_CONFIG_PREFUNCTYPE            // bad function signature for "pre" rule
	ERR_CONFIG_PREPROCJOIN            // illegal use of cfg.JoinOp for "pre" rule
	ERR_CONFIG_PREPROCERROR           // illegal use of cfg.Err for "pre" rule
	ERR_CONFIG_ARGNUM                 // bad number of rule arguments
	ERR_CONFIG_ARGTYPE                // bad rule argument type
	ERR_CONFIG_ARGBOUNDS              // bad rule argument bounds

	ERR_RULE_UNDEFINED // illegal use of undefined rule
	ERR_RULE_KEY       // illegal rule key on non-map field
	ERR_RULE_ELEM      // illegal rule elem on non-map/non-slice/non-array field
	ERR_RULE_ARGMIN    // number of rule arguments is less than min
	ERR_RULE_ARGMAX    // number of rule arguments is more than max

	ERR_FIELD_UNKNOWN // unknown field referenced by rule argument

	ERR_NOTNIL_TYPE // illegal rule "notnil" on non-nilable field

	ERR_OPTIONAL_CONFLICT // an optional rule is in conflict with a required rule

	ERR_ENUM_NONAME  // illegal rule "enum" on field with unnamed type
	ERR_ENUM_KIND    // illegal rule "enum" on field with type of non-basic kind
	ERR_ENUM_NOCONST // "enum" rule on field with type that has no "known" constants declared

	ERR_LENGTH_NOLEN   // illegal rule "len" on field with lenght-less type
	ERR_LENGTH_NORUNE  // illegal rule "runecount" on field with non-stringy and non-[]byte type
	ERR_LENGTH_ARGTYPE // bad argument type in LENGTH rule
	ERR_LENGTH_NOARG   // no valid arguments in LENGTH rule
	ERR_LENGTH_BOUNDS  // invalid bounds arguments in LENGTH rule

	ERR_RANGE_TYPE    // illegal rule "rng" on non-numeric field
	ERR_RANGE_NOARG   // missing one or both values in arguments of RANGE rule
	ERR_RANGE_BOUNDS  // invalid bounds arguments in RANGE rule
	ERR_RANGE_ARGTYPE // bad argument type in RANGE rule

	ERR_ORDERED_TYPE    // illegal ORDERED rule on non-numeric/non-string field
	ERR_ORDERED_ARGTYPE // bad argument type in ORDERED rule

	ERR_PREPROC_INTYPE  // bad PREPROC rule function's input type, incompatible with node
	ERR_PREPROC_OUTTYPE // bad PREPROC rule function's output type, incompatible with node
	ERR_PREPROC_ARGTYPE // bad argument type in PREPROC rule
	ERR_PREPROC_INVALID // invalid PREPROC rule

	ERR_FUNCTION_INTYPE   // bad FUNCTION rule function's input type, incompatible with node
	ERR_FUNCTION_ARGTYPE  // bad argument type in FUNCTION rule
	ERR_FUNCTION_ARGVALUE // bad argument value in FUNCTION rule

	ERR_METHOD_TYPE // illegal METHOD rule on type that does not have the specified method

	// TODO rename
	ERR_ARG_BADCMP // argument's type incompatible with field's type (for comparison)

)

type FieldKeyFunc

type FieldKeyFunc func(fs gotype.FieldSelector, optuniq bool) (key string)

FieldKeyFunc is the type of the function called by the Checker for each field to generate a unique key from the FieldSelector.

If optuniq is true then the key uniqueness is optional.

type FieldNode

type FieldNode struct {
	Field *gotype.StructField
	// The Node representation of the field's type.
	Type *Node
	// The unique key of the field.
	Key string
	// The field's selector.
	Selector gotype.FieldSelector
}

type Info

type Info struct {
	// The Validator being rule-checked.
	Validator *gotype.Validator
	// The Node representation of the Validator
	RootNode *Node
	// KeyMap maps field keys to the corresponding type nodes.
	KeyMap map[string]*FieldNode
	// EnumMap maps types to a slice of
	// constants declared with that type.
	EnumMap map[*gotype.Type][]gotype.Const
}

Info holds the result type information for rule-checking a Validator.

type JoinOp

type JoinOp uint

JoinOp represents the boolean operator that can be used to join multiple instances of a rule into a single one.

NOTE(mkopriva): Because the generated code will be looking for **invalid values, as opposed to valid ones**, the actual expressions generated based on these operators will be the inverse of what their names indicate, see the comments next to the operators for an example.

const (
	JOIN_NOT JoinOp // x || x || x....
	JOIN_AND        // !x || !x || !x....
	JOIN_OR         // !x && !x && !x....
)

type Node

type Node struct {
	// The type associated with the node.
	Type *gotype.Type
	// List of preprocessor rules associated with the node.
	PreRules RuleList
	// List of validation rules associated with the node.
	IsRules RuleList
	// If the Node represents a map, then Key
	// will hold information about the map's key.
	Key *Node
	// If the Node represents a map, array, or slice, then
	// Elem will hold information about the element's type.
	//
	// If the Node represents a pointer, then Elem will
	// hold information about the pointer's base type.
	Elem *Node
	// If the Node represents a struct, then Fields
	// will hold information about the struct's fields.
	Fields []*FieldNode
	// Ptr holds the Node whose type is the pointer to
	// this Node's type. If this Node's type is not pointed
	// to by any pointer then this field will be nil.
	Ptr *Node
}

func (*Node) Base

func (n *Node) Base() (m *Node)

Base returns the pointer base Node of n. If n is not a pointer then the returned Node will be n itself.

func (*Node) HasRules

func (n *Node) HasRules() bool

HasRules reports whether or not n, or any of its child Nodes, have rules other than OPTIONAL and "noguard".

func (*Node) HasSubRules

func (n *Node) HasSubRules() bool

HasSubRules reports whether or not any of n's child Nodes have rules other than OPTIONAL and "noguard".

func (*Node) IsDeep

func (n *Node) IsDeep() bool

IsDeep reports whether the n's pointer depth is "deep" or not. A "deep" pointer depth is 2 and above, i.e. a depth that requires explicit indirection to access the base type's fields and/or methods.

func (*Node) IsNoGuard

func (n *Node) IsNoGuard() bool

IsNoGuard reports whether or not n has a NOGUARD is-rule.

func (*Node) IsOptional

func (n *Node) IsOptional() bool

IsOptional reports whether or not n has an OPTIONAL is-rule.

func (*Node) IsPtr

func (n *Node) IsPtr() bool

IsPtr reports whether or not n's type is a pointer type.

func (*Node) IsRequired

func (n *Node) IsRequired() bool

IsRequired reports whether or not n has a REQUIRED is-rule.

func (*Node) IsShallow

func (n *Node) IsShallow() bool

IsShallow reports whether the n's pointer depth is "shallow" or not. A "shallow" pointer depth is 0 or 1, i.e. a depth that allows the base type's fields and/or methods to be accessed without explicit indirection.

func (*Node) IsStruct

func (n *Node) IsStruct() bool

IsStruct reports whether or not n's type is a struct type.

func (*Node) NeedsTempVar

func (n *Node) NeedsTempVar() bool

func (*Node) NumIsRules

func (n *Node) NumIsRules() int

NumIsRules returns the length of the IsRules slice.

func (*Node) PtrDepth

func (n *Node) PtrDepth() int

PtrDepth reports the "pointer depth" of Node n, i.e. the number of pointer Nodes from root to n.

func (*Node) Root

func (n *Node) Root() (m *Node)

Root returns the pointer root Node of n. If n is not a pointer base then the returned Node will be n itself.

type Rule

type Rule struct {
	// The name of the rule.
	Name string
	// The arguments of the rule.
	Args []*Arg
	// The spec associated with the rule. Note that this is
	// not populated by the parser but instead by the Checker.
	Spec *Spec
}

Rule represents the rule as parsed from a struct tag.

func (Rule) IsBasic added in v1.3.0

func (r Rule) IsBasic() bool

IsBasic reports whether or not the rule is a basic rule. What "basic" means at this point is that the rule is NOT a function that returns an error as its second return value.

func (Rule) String

func (r Rule) String() (out string)

type RuleList

type RuleList []*Rule

func (*RuleList) Add

func (ls *RuleList) Add(r *Rule)

Add adds the given rule to the RuleList.

func (RuleList) Contains

func (ls RuleList) Contains(name string) bool

Contains reports whether or not the RuleList contains a rule with the given name.

func (RuleList) Empty

func (ls RuleList) Empty() bool

Empty reports whether or not the slice is empty.

func (RuleList) Many

func (ls RuleList) Many() bool

Many reports whether or not there are multiple rules in the slice.

func (RuleList) One

func (ls RuleList) One() bool

One reports whether or not there is exactly one rule in the slice.

func (*RuleList) Remove

func (ls *RuleList) Remove(name string)

Remove removes a rule from the RuleList by name.

type Spec

type Spec struct {
	// The unique name of the rule.
	Name string
	// The kind of the rule.
	Kind SpecKind
	// Kind=FUNCTION only, the function's identifier.
	FName string
	// Kind=FUNCTION only, the function's type.
	FType *gotype.Type
	// ArgMin and ArgMax define bounds of allowed
	// number of arguments for the rule.
	ArgMin, ArgMax int
	// Kind=FUNCTION only, the rule's pre-declared argument options.
	ArgOpts []map[string]Arg
	// The join operator that should be used for joining
	// multiple instances of the rule into a single one.
	JoinOp JoinOp
	// The spec for the error message that the
	// generator should produce for the rule.
	Err ErrSpec
	// The error options for specific argument combinations
	ErrOpts map[string]ErrSpec
	// Indicates that the generated code should use raw
	// strings for any string arguments of the rule.
	UseRawString bool
}

func GetSpec

func GetSpec(name string) *Spec

type SpecKind

type SpecKind uint
const (
	REQUIRED   SpecKind // required, notnil
	COMPARABLE          // =, !=
	ORDERED             // >, >=, <, <=, min, max
	LENGTH              // len, runecount
	RANGE               // rng
	ENUM                // enum
	FUNCTION            // <custom/builtin/included func rules>
	METHOD              // isvalid (implicit), ...

	// "modifiers"
	OPTIONAL // omitnil [is the default rule for pointers] (ptr only), optional (ptr & base)
	NOGUARD  // nonilguard
	REMOVE   // -isvalid

	// "preprocessors"
	PREPROC // <custom/builtin/included func rules>
)

func (SpecKind) String added in v1.2.1

func (k SpecKind) String() string

type Tag

type Tag struct {
	// The list of rules present at this level of the tree.
	Rules []*Rule
	// Key and Elem are child nodes of this Tag.
	Key, Elem *Tag
	// contains filtered or unexported fields
}

A Tag is a tree-like representation of a parsed "rule" struct tag.

func (*Tag) AddRule

func (t *Tag) AddRule(r *Rule)

AddRule adds the given rule to the Tag.

func (*Tag) GetElem

func (t *Tag) GetElem() *Tag

func (*Tag) GetKey

func (t *Tag) GetKey() *Tag

func (*Tag) GetRule

func (t *Tag) GetRule(name string) (r *Rule, ok bool)

GetRule looks up a rule in the Tag by the given name and, if present, returns it.

func (*Tag) GetRules

func (t *Tag) GetRules() []*Rule

func (*Tag) HasElem

func (t *Tag) HasElem() bool

HasElem reports whether or not the Tag has an Elem child.

func (*Tag) HasKey

func (t *Tag) HasKey() bool

HasElem reports whether or not the Tag has an Key child.

func (*Tag) HasRule

func (t *Tag) HasRule(name string) bool

HasRule reports whether or not the Tag contains a rule with the given name.

func (*Tag) IsEmpty

func (t *Tag) IsEmpty() bool

IsEmpty reports whether or not the Tag, including its sub-nodes, are empty.

func (*Tag) RmRule

func (t *Tag) RmRule(name string)

RmRule removes a rule from the Tag by name.

func (*Tag) String

func (t *Tag) String() (out string)

Jump to

Keyboard shortcuts

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