gotype

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: 7 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 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

	K_UNION

	// 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 Term added in v1.4.0

type Term struct {
	// Indicates whether or not the term
	// was declared with a tilde.
	Tilde bool
	// The term's type.
	Type *Type
}

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

	// The method set of a named type or an interface type.
	Methods   []*Method
	Embeddeds []*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

	// If the Type is an instantiated named type then
	// Origin points to the original generic type.
	Origin *Type
	// If the Type is an instantiated named type then
	// TypeArgs is the list of type arguments.
	TypeArgs []*Type

	// If the Type is a generic named type or a generic function
	// signature then TypeParams is the list of type parameters.
	TypeParams []*TypeParam

	// If the Type is a union then Terms holds
	// the union's of terms.
	Terms []*Term
}

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) CanError added in v1.3.0

func (t *Type) CanError() bool

CanError reports that the type, if it *is* a K_FUNC type, has error as its last return value type.

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) 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) IsIncluded added in v1.2.1

func (t *Type) IsIncluded() bool

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

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) IsStructOrStructPointer added in v1.5.1

func (t Type) IsStructOrStructPointer() bool

IsStructOrStructPointer indicates that t is either a struct type or a pointer-to-struct type.

func (Type) IsStructPointer added in v1.5.1

func (t Type) IsStructPointer() bool

IsStructPointer indicates that t is a pointer-to-struct type.

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.

func (*Type) VisibleFields added in v1.4.0

func (t *Type) VisibleFields() []*StructField

NOTE: this implementation, and the corresponding tests, is a slightly modified and adapted version of https://pkg.go.dev/reflect#VisibleFields.

VisibleFields returns all the visible fields in t, which must be a struct type or a pointer to a struct type. A field is defined as visible if it's accessible directly from the type's instance.

The returned fields include fields inside anonymous struct members and unexported fields. They follow the same order found in the struct, with anonymous fields followed immediately by their promoted fields.

type TypeParam added in v1.4.0

type TypeParam struct {
	// The type param's package.
	Pkg Pkg
	// The type name of the type param.
	Name string
	// The constraint specified for the type param.
	Constraint *Type
}

type TypeParamer added in v1.4.0

type TypeParamer interface {
	TypeParams() *types.TypeParamList
}

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.

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