Documentation
¶
Index ¶
- func InitSpecs(cfg config.Config, a *search.AST) error
- type Arg
- type ArgType
- type Checker
- type ErrSpec
- type Error
- func (e *Error) CfgArgBounds() string
- func (e *Error) CfgArgKey() string
- func (e *Error) CfgArgNum() string
- func (e *Error) CfgArgType() string
- func (e *Error) CfgArgValue() string
- func (e *Error) CfgArgWord() string
- func (e *Error) CfgErrYAML() string
- func (e *Error) CfgFuncParamType() string
- func (e *Error) CfgJoinOp() string
- func (e *Error) CfgRuleName() string
- func (e *Error) ConfigFile() string
- func (e *Error) CustomFuncIdent() string
- func (e *Error) ErrType() string
- func (e *Error) Error() string
- func (e *Error) Field() string
- func (e *Error) FieldName() string
- func (e *Error) FieldPos() string
- func (e *Error) FieldType() string
- func (e *Error) FieldTypeFamily() string
- func (e *Error) FuncIdent() string
- func (e *Error) FuncName() string
- func (e *Error) FuncParamIdent() string
- func (e *Error) FuncParamIndex() string
- func (e *Error) FuncParamNum() string
- func (e *Error) FuncParamWord() string
- func (e *Error) FuncPos() string
- func (e *Error) FuncType() string
- func (e *Error) HasConfigFile() bool
- func (e *Error) HasOriginalError() bool
- func (e *Error) OriginalError() string
- func (e *Error) Rule() string
- func (e *Error) Rule2Name() string
- func (e *Error) RuleArgFieldType() string
- func (e *Error) RuleArgIsField() bool
- func (e *Error) RuleArgNum() string
- func (e *Error) RuleArgType() string
- func (e *Error) RuleArgValue() string
- func (e *Error) RuleArgs() string
- func (e *Error) RuleFuncIdent() string
- func (e *Error) RuleFuncIn0Type() string
- func (e *Error) RuleFuncName() string
- func (e *Error) RuleFuncNameWithType() string
- func (e *Error) RuleFuncOut0Type() string
- func (e *Error) RuleFuncParamType() string
- func (e *Error) RuleFuncType() string
- func (e *Error) RuleName() string
- func (e *Error) RuleSpecArgMax() string
- func (e *Error) RuleSpecArgMin() string
- func (e *Error) RuleSpecKind() string
- func (e *Error) Tag() string
- func (e *Error) TagElem() string
- func (e *Error) TagKey() string
- func (e *Error) TagSTKey() string
- func (e *Error) Type() string
- type ErrorCode
- type FieldKeyFunc
- type FieldNode
- type Info
- type JoinOp
- type Node
- func (n *Node) Base() (m *Node)
- func (n *Node) HasRules() bool
- func (n *Node) HasSubRules() bool
- func (n *Node) IsDeep() bool
- func (n *Node) IsNoGuard() bool
- func (n *Node) IsOptional() bool
- func (n *Node) IsPtr() bool
- func (n *Node) IsRequired() bool
- func (n *Node) IsShallow() bool
- func (n *Node) IsStruct() bool
- func (n *Node) NeedsTempVar() bool
- func (n *Node) NumIsRules() int
- func (n *Node) PtrDepth() int
- func (n *Node) Root() (m *Node)
- type Rule
- type RuleList
- type Spec
- type SpecKind
- type Tag
- func (t *Tag) AddRule(r *Rule)
- func (t *Tag) GetElem() *Tag
- func (t *Tag) GetKey() *Tag
- func (t *Tag) GetRule(name string) (r *Rule, ok bool)
- func (t *Tag) GetRules() []*Rule
- func (t *Tag) HasElem() bool
- func (t *Tag) HasKey() bool
- func (t *Tag) HasRule(name string) bool
- func (t *Tag) IsEmpty() bool
- func (t *Tag) RmRule(name string)
- func (t *Tag) String() (out string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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.
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.
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 (*Error) CfgArgType ¶
func (*Error) CfgArgValue ¶
func (*Error) CfgArgWord ¶
func (*Error) CfgErrYAML ¶
func (*Error) CfgFuncParamType ¶
func (*Error) CfgRuleName ¶
func (*Error) ConfigFile ¶
func (*Error) CustomFuncIdent ¶
func (*Error) FieldTypeFamily ¶
func (*Error) FuncParamIdent ¶
func (*Error) FuncParamIndex ¶
func (*Error) FuncParamNum ¶
func (*Error) FuncParamWord ¶
func (*Error) HasConfigFile ¶
func (*Error) HasOriginalError ¶ added in v1.2.1
func (*Error) OriginalError ¶
func (*Error) RuleArgFieldType ¶
func (*Error) RuleArgIsField ¶
func (*Error) RuleArgNum ¶ added in v1.2.1
func (*Error) RuleArgType ¶
func (*Error) RuleArgValue ¶
func (*Error) RuleFuncIdent ¶
func (*Error) RuleFuncIn0Type ¶
func (*Error) RuleFuncName ¶ added in v1.1.0
func (*Error) RuleFuncNameWithType ¶ added in v1.1.0
func (*Error) RuleFuncOut0Type ¶
func (*Error) RuleFuncParamType ¶
func (*Error) RuleFuncType ¶
func (*Error) RuleSpecArgMax ¶ added in v1.2.1
func (*Error) RuleSpecArgMin ¶ added in v1.2.1
func (*Error) RuleSpecKind ¶ added in v1.2.1
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 ¶
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 ¶
HasRules reports whether or not n, or any of its child Nodes, have rules other than OPTIONAL and "noguard".
func (*Node) HasSubRules ¶
HasSubRules reports whether or not any of n's child Nodes have rules other than OPTIONAL and "noguard".
func (*Node) IsDeep ¶
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) IsOptional ¶
IsOptional reports whether or not n has an OPTIONAL is-rule.
func (*Node) IsRequired ¶
IsRequired reports whether or not n has a REQUIRED is-rule.
func (*Node) IsShallow ¶
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) NeedsTempVar ¶
func (*Node) NumIsRules ¶
NumIsRules returns the length of the IsRules slice.
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.
type RuleList ¶
type RuleList []*Rule
func (RuleList) Contains ¶
Contains reports whether or not the RuleList contains a rule with the given 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
}
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> )
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) GetRule ¶
GetRule looks up a rule in the Tag by the given name and, if present, returns it.