typeinfo

package
v0.0.0-...-f21976e Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BothXY

type BothXY struct{}

BothXY is the shape with both input X and output Y.

type Func

type Func interface {
	Object() types.Object
	Name() string          // for declared function
	FuncLit() *ast.FuncLit // for anonymous function literal

	// Signature information
	X() Type
	Y() Type
	HasErr() bool
	HasOut() bool

	// Position information
	Pos() token.Pos
	WithPos(token.Pos) Func
}

Func describes a function information. It holds function signature information of types.Object that is necessary from the Convgen's perspective.

func FuncLitOf

func FuncLitOf[S Shape](pkg *packages.Package, lit *ast.FuncLit) (Func, error)

FuncLitOf inspects the given function literal and returns a new Func. It returns an error if the function signature does not match with the given shape.

func FuncOf

func FuncOf[S Shape](obj types.Object) (Func, error)

FuncOf inspects the given function and returns a new Func. It returns an error if the function signature does not match with the given shape.

func NewFunc

func NewFunc(pkg *types.Package, name string, x, y Type, hasErr, hasOut bool) Func

NewFunc creates a new Func with the given attributes.

This is for creating a new function programmatically. It focuses on the signature of the function only, not the body.

To create from an existing function object, use FuncOf or related functions. To create from a function literal without a named object, use [InspectFuncLit] or related functions.

type Lookup

type Lookup[T Func] struct {
	// contains filtered or unexported fields
}

Lookup indexes converter functions by their input and output types.

func NewLookup

func NewLookup[T Func]() *Lookup[T]

NewLookup creates a new Lookup.

func (*Lookup[T]) Del

func (l *Lookup[T]) Del(x, y Type) bool

Del removes a function which converts X to Y. It returns whether such a function existed.

func (*Lookup[T]) Get

func (l *Lookup[T]) Get(x, y Type) (T, bool)

Get finds a function which converts X to Y.

func (*Lookup[T]) Put

func (l *Lookup[T]) Put(fn T) (T, bool)

Put adds a converter function to the registry.

func (*Lookup[T]) Range

func (l *Lookup[T]) Range() iter.Seq[T]

Range iterates all registered functions.

type NoXY

type NoXY struct{}

NoXY is the shape with no input and no output.

type OnlyX

type OnlyX struct{}

OnlyX is the shape with only input X.

type OnlyY

type OnlyY struct{}

OnlyY is the shape with only output Y.

type Shape

type Shape interface {
	NoXY | OnlyX | OnlyY | BothXY
	// contains filtered or unexported methods
}

Shape is a type constraint for function shapes. It is used in FuncOf and FuncLitOf to specify which kind of function signature is expected.

Shape   | Basic      | HasErr              | HasOut       | HasErr & HasOut
--------+------------+---------------------+--------------+-------------------
NoXY    | func()     | func() error        | -            | -
OnlyX   | func(x)    | func(x) error       | -            | -
OnlyY   | func() y   | func() (y, error)   | func(*y),    | func(*y) error
BothXY  | func(x) y  | func(x) (y, error)  | func(x, *y)  | func(x, *y) error

type Type

type Type struct {
	T types.Type

	Basic     *types.Basic
	Array     *types.Array
	Slice     *types.Slice
	Map       *types.Map
	Struct    *types.Struct
	Interface *types.Interface
	Pointer   *types.Pointer
	Named     *types.Named

	Elem *Type
	Key  *Type
	Len  int64
}

Type describes a type information. It holds information of types.Type that is necessary from the Convgen's perspective.

func TypeOf

func TypeOf(t types.Type) Type

TypeOf inspects the given type and returns a new Type.

func (Type) Deref

func (t Type) Deref() Type

Deref returns the element type if the type is a pointer. For type of *X, it returns type of X. If the type is not a pointer, it returns the type itself.

func (Type) Identical

func (t Type) Identical(u Type) bool

func (Type) IsArray

func (t Type) IsArray() bool

func (Type) IsBasic

func (t Type) IsBasic() bool

func (Type) IsError

func (t Type) IsError() bool

func (Type) IsGeneric

func (t Type) IsGeneric() bool

IsGeneric reports whether the type is generic or has any generic type parameters. Even though the type has type parameters, if all type arguments are concrete types, it returns false.

func (Type) IsInterface

func (t Type) IsInterface() bool

func (Type) IsMap

func (t Type) IsMap() bool

func (Type) IsNamed

func (t Type) IsNamed() bool

func (Type) IsNil

func (t Type) IsNil() bool

func (Type) IsPointer

func (t Type) IsPointer() bool

func (Type) IsSlice

func (t Type) IsSlice() bool

func (Type) IsStruct

func (t Type) IsStruct() bool

func (Type) Method

func (t Type) Method(name string) (*types.Func, bool)

Method returns the method with the given name. If the type is not a named type or the method does not exist, it returns nil and false.

func (Type) Pkg

func (t Type) Pkg() *types.Package

Pkg returns the package where the type is defined. It returns nil if the type is not a named type.

func (Type) PointerDepth

func (t Type) PointerDepth() int

PointerDepth returns the number of pointer indirections. For example, for type of ***X, it returns 3. For type of X, it returns 0.

func (Type) Pos

func (t Type) Pos() token.Pos

Pos returns the position where the type is defined. It returns token.NoPos if the type is not a named type.

func (Type) Ref

func (t Type) Ref() Type

Ref returns the pointer type of the type. For type of X, it returns type of *X.

func (Type) String

func (t Type) String() string

func (Type) StructField

func (t Type) StructField(name string) (*types.Var, bool)

StructField returns the struct field with the given name. If the type is not a struct or the field does not exist, it returns nil and false.

func (Type) Type

func (t Type) Type() types.Type

Jump to

Keyboard shortcuts

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