Documentation ¶
Index ¶
- func IsErrorAggregator(t *Type) bool
- func IsErrorConstructor(t *Type) bool
- func IsErrorConstructorFunc(t *Type) bool
- type Analyzer
- type AssignmentStatus
- type Const
- type ErrorHandlerField
- type FieldSelector
- type Func
- type Kind
- type Method
- type MethodInfo
- type Methoder
- type Pkg
- type StructField
- type Type
- func (t *Type) CanAssign(u *Type) AssignmentStatus
- func (t *Type) CanConvert(u *Type) bool
- func (t *Type) HasIsValid() bool
- func (t *Type) HasLength() bool
- func (t *Type) Implements(u *Type) bool
- func (t Type) Is(kinds ...Kind) bool
- func (t *Type) IsComparable() bool
- func (t *Type) IsEmptyInterface() bool
- func (t *Type) IsGoAny() bool
- func (t *Type) IsGoAnySlice() bool
- func (t *Type) IsGoError() bool
- func (t *Type) IsGoString() bool
- func (t *Type) IsIdentical(u *Type) bool
- func (t *Type) IsIncluded() bool
- func (t *Type) IsNilable() bool
- func (t *Type) NeedsConversion(u *Type) bool
- func (t *Type) PtrOf(u *Type) bool
- func (t Type) TypeString(pkg *Pkg) string
- type Validator
- type Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsErrorAggregator ¶
IsErrorAggregator reports whether or not the given type implements the "ErrorAggregator" interface.
func IsErrorConstructor ¶
IsErrorConstructor reports whether or not the given type implements the "ErrorConstructor" interface.
func IsErrorConstructorFunc ¶
IsErrorConstructorFunc reports whether or not the given type matches the "ErrorConstructor" function signature.
func(key string, val any, rule string, args ...any) error
Types ¶
type Analyzer ¶
type Analyzer struct {
// contains filtered or unexported fields
}
Analyzer maintains the state of the analysis.
func NewAnalyzer ¶
NewAnalyzer returns a new Analyzer instance with the p argument as the "root" package. The "root" package is primarily used to resolve wether the types encountered during analysis are imported or not.
func (*Analyzer) Analyze ¶
Analyze runs the analysis of the given types.Type t and returns the resulting gotype.Type representation.
func (*Analyzer) Consts ¶
Consts is a helper method that finds and returns all declared constants for a given type.
type AssignmentStatus ¶
type AssignmentStatus uint
const ( ASSIGNMENT_INVALID AssignmentStatus = iota // cannot assign ASSIGNMENT_CONVERT // can assign but needs explicit converstion ASSIGNMENT_OK // can assign as is )
type ErrorHandlerField ¶
type ErrorHandlerField struct { // Name of the field (case preserved). Name string // Indicates whether or not the field's type implements // the valid.ErrorAggregator interface. IsAggregator bool }
ErrorHandlerField is the result of analyzing a validator struct's field whose type implements the valid.ErrorConstructor or valid.ErrorAggregator interface.
type FieldSelector ¶
type FieldSelector []*StructField
FieldSelector is a list of fields that represents a chain of selectors where the 0th field is the "root" field and the len-1 field is the "leaf" field.
func (FieldSelector) CopyWith ¶
func (s FieldSelector) CopyWith(f *StructField) FieldSelector
CopyWith returns a copy of the receiver with f appended to the end.
func (FieldSelector) Last ¶
func (s FieldSelector) Last() *StructField
Last returns the last field in the selector.
type Kind ¶
type Kind uint
Kind indicates the specific kind of a Go type.
const ( // basic K_INVALID Kind = iota K_BOOL K_INT K_INT8 K_INT16 K_INT32 K_INT64 K_UINT K_UINT8 K_UINT16 K_UINT32 K_UINT64 K_UINTPTR K_FLOAT32 K_FLOAT64 K_COMPLEX64 K_COMPLEX128 K_STRING K_UNSAFEPOINTER // non-basic K_ARRAY // try to validate individual elements K_INTERFACE // try to validate ... ??? K_MAP // try to validate individual elements K_PTR // try to validate the element K_SLICE // try to validate the individual elements K_STRUCT // try to validate the individual fields K_CHAN // don't validate K_FUNC // don't validate // alisases (basic) K_BYTE = K_UINT8 K_RUNE = K_INT32 )
func (Kind) BasicString ¶
BasicString returns a string representation of the basic kind k.
func (Kind) IsInteger ¶
Reports whether or not k is one of the int types. (does not include unsigned integers)
func (Kind) IsNumeric ¶
Reports whether or not k is of the numeric kind, note that this does not include the complex64 and complex128 kinds.
func (Kind) IsUnsigned ¶
Reports whether or not k is one of the uint types.
type Method ¶
type Method struct { // The package to which the method belongs. Pkg Pkg // The name of the method. Name string // The method's type. Type *Type // Indicates whether or not the method is exported. IsExported bool }
Method describes a single method in the method set of a named type or interface.
type MethodInfo ¶
type MethodInfo struct { // The name of the method (case preserved). Name string }
MethodInfo represents the result of analysing a type's method.
type Methoder ¶
Methoder represents a type with methods. It is implicitly implemented by *types.Interface and *types.Named.
type StructField ¶
type StructField struct { // The package to which the field belongs. Pkg Pkg // Name of the field. Name string // The field's type. Type *Type // The field's raw, uparsed struct tag. Tag string // Indicates that the tag `is:"-"` was used. CanIgnore bool // Indicates whether or not the field is embedded. IsEmbedded bool // Indicates whether or not the field is exported. IsExported bool // Var holds a reference to the *types.Var // representation of the field. Var *types.Var `cmp:"+"` }
StructField describes a single struct field in a struct.
func (*StructField) CanAccess ¶ added in v1.1.0
func (f *StructField) CanAccess(from Pkg) bool
type Type ¶
type Type struct { // The type's package. Pkg Pkg // The name of a named type or empty string for unnamed types Name string // The kind of the go type. Kind Kind // Indicates whether or not the field is exported. IsExported bool // If the base type's an array type, this field will hold the array's length. ArrayLen int64 // If kind is func, indicates whether or not the function is variadic. IsVariadic bool // Indicates whether or not the type is the "byte" alias type. IsByte bool // Indicates whether or not the type is the "rune" alias type. IsRune bool // If kind is map, Key will hold the info on the map's key type. Key *Type // If kind is map, Elem will hold the info on the map's value type. // If kind is ptr, Elem will hold the info on pointed-to type. // If kind is slice/array, Elem will hold the info on slice/array element type. Elem *Type // If kind is func, In & Out will hold the function's parameter and result types. In, Out []*Var // If kind is struct, Fields will hold the list of the struct's fields. Fields []*StructField // The method set of a named type or an interface type. Methods []*Method }
Type is the representation of a Go type.
func (*Type) CanAssign ¶
func (t *Type) CanAssign(u *Type) AssignmentStatus
CanAssign reports whether or not a value of type u can be assigned to a variable of type t. Note that this does not handle unnamed struct, interface, func, and channel types.
func (*Type) CanConvert ¶
CanConvert reports whether or not a value of type u can be converted to a value of type t.
func (*Type) HasIsValid ¶
IsValid reports whether or not the "IsValid() bool" method belongs to the method set of the type t.
func (*Type) HasLength ¶
HasLength reports whether or not the Go type represented by t has a length.
func (*Type) Implements ¶ added in v1.2.0
func (*Type) IsComparable ¶ added in v1.1.0
IsComparable reports wether or not a value of the Go type represented by t is comparable.
func (*Type) IsEmptyInterface ¶
Indicates whether or not the type is an empty interface type.
func (*Type) IsGoAnySlice ¶
IsGoAnySlice reports whether or not t is the Go builtin []any/[]interface{} type.
func (*Type) IsGoString ¶
IsGoString reports whether or not t is the Go builtin string type.
func (*Type) IsIdentical ¶ added in v1.2.0
Reports whether the types represented by t and u are equal. Note that this does not handle unnamed struct, interface (non-empty), func, and channel types.
func (*Type) IsIncluded ¶ added in v1.2.1
IsIncluded reports whether or not the Type was declared in the github.com/frk/valid package.
func (*Type) IsNilable ¶
IsNilable reports wether or not a value of the Go type represented by t can be set to nil.
func (*Type) NeedsConversion ¶
Reports whether or not a value of type t needs to be converted before it can be assigned to a variable of type u.
func (Type) TypeString ¶ added in v1.1.0
String retruns a string representation of the t Type.
type Validator ¶
type Validator struct { // The struct type info. Type *Type // Info on the valid.ErrorConstructor or valid.ErrorAggregator // field of the validator struct type, or nil. ErrorHandlerField *ErrorHandlerField // Info on the validator type's method named "beforevalidate" (case insensitive), or nil. BeforeValidateMethod *MethodInfo // Info on the validator type's method named "aftervalidate" (case insensitive), or nil. AfterValidateMethod *MethodInfo }
Validator represents a validator struct type.