gotype

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsErrorAggregator

func IsErrorAggregator(t *Type) bool

IsErrorAggregator reports whether or not the given type implements the "ErrorAggregator" interface.

func IsErrorConstructor

func IsErrorConstructor(t *Type) bool

IsErrorConstructor reports whether or not the given type implements the "ErrorConstructor" interface.

func IsErrorConstructorFunc

func IsErrorConstructorFunc(t *Type) bool

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

func NewAnalyzer(p *types.Package) (a *Analyzer)

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

func (a *Analyzer) Analyze(t types.Type) (u *Type)

Analyze runs the analysis of the given types.Type t and returns the resulting gotype.Type representation.

func (*Analyzer) Consts

func (a *Analyzer) Consts(t *Type, ast *search.AST) (consts []Const)

Consts is a helper method that finds and returns all declared constants for a given type.

func (*Analyzer) Object

func (a *Analyzer) Object(obj types.Object) (u *Type)

Object is used to analyze the type of a *types.Func object.

func (*Analyzer) Validator

func (an *Analyzer) Validator(named *types.Named) *Validator

Validator analyzes the named type and returns its corresponding gotype.Validator representation.

The method will panic if the named type is not a struct.

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 Const

type Const struct {
	// The constant's package.
	Pkg Pkg
	// Name of the constant.
	Name string
}

Const represents the identifier of a declared constant.

type ContextField

type ContextField struct {
	// Name of the field (case preserved).
	Name string
}

ContextField is the result of analyzing a validator struct's field whose name is equal to "context" (case insensitive).

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 Func

type Func struct {
	Name string
	Type *Type
}

Func is used to represent a function.

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

func (k Kind) BasicString() string

BasicString returns a string representation of the basic kind k.

func (Kind) IsBasic

func (k Kind) IsBasic() bool

Reports whether or not k is of a basic kind.

func (Kind) IsFloat

func (k Kind) IsFloat() bool

Reports whether or not k is one of the float types.

func (Kind) IsInteger

func (k Kind) IsInteger() bool

Reports whether or not k is one of the int types. (does not include unsigned integers)

func (Kind) IsNumeric

func (k Kind) IsNumeric() bool

Reports whether or not k is of the numeric kind, note that this does not include the complex64 and complex128 kinds.

func (Kind) IsUnsigned

func (k Kind) IsUnsigned() bool

Reports whether or not k is one of the uint types.

func (Kind) String

func (k Kind) String() string

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

type Methoder interface {
	NumMethods() int
	Method(i int) *types.Func
}

Methoder represents a type with methods. It is implicitly implemented by *types.Interface and *types.Named.

type Pkg

type Pkg struct {
	// The package import path.
	Path string
	// The package's name.
	Name string
}

Pkg describes a type's package.

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 MustGetType added in v1.1.0

func MustGetType(pkgpath, name string, a *search.AST) *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

func (t *Type) CanConvert(u *Type) bool

CanConvert reports whether or not a value of type u can be converted to a value of type t.

func (*Type) HasIsValid

func (t *Type) HasIsValid() bool

IsValid reports whether or not the "IsValid() bool" method belongs to the method set of the type t.

func (*Type) HasLength

func (t *Type) HasLength() bool

HasLength reports whether or not the Go type represented by t has a length.

func (*Type) Implements added in v1.2.0

func (t *Type) Implements(u *Type) bool

func (Type) Is

func (t Type) Is(kinds ...Kind) bool

Reports whether or not the type's kind is is one of the provided kinds.

func (*Type) IsBuiltin

func (t *Type) IsBuiltin() bool

IsBuiltin reports whether or not the Type was declared in the github.com/frk/valid package.

func (*Type) IsComparable added in v1.1.0

func (t *Type) IsComparable() bool

IsComparable reports wether or not a value of the Go type represented by t is comparable.

func (*Type) IsEmptyInterface

func (t *Type) IsEmptyInterface() bool

Indicates whether or not the type is an empty interface type.

func (*Type) IsGoAny

func (t *Type) IsGoAny() bool

IsGoAny reports whether or not t is the Go builtin any/interface{} type.

func (*Type) IsGoAnySlice

func (t *Type) IsGoAnySlice() bool

IsGoAnySlice reports whether or not t is the Go builtin []any/[]interface{} type.

func (*Type) IsGoError

func (t *Type) IsGoError() bool

IsGoError reports whether or not t is the Go builtin error type.

func (*Type) IsGoString

func (t *Type) IsGoString() bool

IsGoString reports whether or not t is the Go builtin string type.

func (*Type) IsIdentical added in v1.2.0

func (t *Type) IsIdentical(u *Type) bool

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) IsNilable

func (t *Type) IsNilable() bool

IsNilable reports wether or not a value of the Go type represented by t can be set to nil.

func (*Type) NeedsConversion

func (t *Type) NeedsConversion(u *Type) bool

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) PtrOf

func (t *Type) PtrOf(u *Type) bool

Reports whether or not the type t represents a pointer type of u.

func (Type) TypeString added in v1.1.0

func (t Type) TypeString(pkg *Pkg) string

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 field named "context" (case insensitive), or nil.
	ContextField *ContextField
	// 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.

type Var

type Var struct {
	Name string
	Type *Type
}

Var is used to represent a function's parameters and results.

func (*Var) ShallowCopy

func (v *Var) ShallowCopy() *Var

Jump to

Keyboard shortcuts

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