typed

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Basic types currently used in the codebase
	Any    = MustTypeName("any")
	Bool   = MustTypeName("bool")
	String = MustTypeName("string")
)

Common built-in type definitions for convenience. These pre-defined TypeName instances help avoid repeated string literals and potential typos when referring to common Go built-in types.

Functions

func ExprImplements added in v1.5.0

func ExprImplements(resolver TypeResolver, expr dst.Expr, iface *types.Interface) bool

ExprImplements checks if the type of a dst.Expr, resolved using the provider, implements the given interface.

func FindMatchingTypeName added in v1.5.0

func FindMatchingTypeName(fields *dst.FieldList, typeNameStr string) (index int, found bool)

FindMatchingTypeName parses a type name string and searches a field list for the first field whose type matches. It returns the index of the matching field and whether a match was found. The index accounts for fields with multiple names.

func ResolveInterfaceTypeByName added in v1.5.0

func ResolveInterfaceTypeByName(name string) (*types.Interface, error)

ResolveInterfaceTypeByName takes an interface name as a string and resolves it to an interface type.

func SplitPackageAndName added in v1.5.0

func SplitPackageAndName(fullName string) (pkgPath string, localName string)

SplitPackageAndName splits a fully qualified type name like "io.Reader" or "example.com/pkg.Type" into its package path and local name. Returns ("", "error") for built-in "error". Returns ("", "MyType") for unqualified "MyType". For generic types like "iter.Seq[T]" or "iter.Seq[io.Reader]", the type parameters are included in the local name, so it returns ("iter", "Seq[T]") and ("iter", "Seq[io.Reader]").

Types

type ReferenceKind

type ReferenceKind bool

ReferenceKind denotes the style of a reference, which influences compilation and linking requirements.

const (
	// ImportStatement references must be made available to the compiler via the provided `importcfg`.
	ImportStatement ReferenceKind = true
	// RelocationTarget references must be made available to the linker, and must be referenced (directly or not) by the main package.
	RelocationTarget ReferenceKind = false
)

func (ReferenceKind) String

func (k ReferenceKind) String() string

type ReferenceMap

type ReferenceMap struct {
	// contains filtered or unexported fields
}

ReferenceMap associates import paths to ReferenceKind values.

func NewReferenceMap added in v1.0.3

func NewReferenceMap(nodeMap map[dst.Node]ast.Node, scopes map[ast.Node]*types.Scope) ReferenceMap

func (*ReferenceMap) AddImport

func (r *ReferenceMap) AddImport(file *dst.File, nodes []dst.Node, path string, localName string) bool

AddImport takes a package import path and the name in file and the result of a recursive parent lookup. It first determines if the import is already present and if it has not been shadowed by a local declaration. If both conditions are met, the import is added to the reference map and the function returns true. Otherwise, it returns false.

func (r *ReferenceMap) AddLink(file *dst.File, path string) bool

AddLink registers the provided path as a relocation target resolution source. If this path is already registered as an import, this method does nothing and returns false.

func (*ReferenceMap) AddSyntheticImports

func (r *ReferenceMap) AddSyntheticImports(file *dst.File) bool

AddSyntheticImports adds the registered imports to the provided *dst.File. This is not safe to call during an AST traversal by dstutil.Apply, as this may offset the declaration list by 1 in case a new import declaration needs to be added, which would result in re-traversing current declaration when the cursor moves forward. Instead, it is advise to call this method after dstutil.Apply has returned.

func (*ReferenceMap) Count

func (r *ReferenceMap) Count() int

func (*ReferenceMap) Map

func (r *ReferenceMap) Map() map[string]ReferenceKind

func (*ReferenceMap) Merge

func (r *ReferenceMap) Merge(other ReferenceMap)

type TypeName added in v1.5.0

type TypeName struct {
	// ImportPath is the import Path that provides the type, or an empty string if the
	// type is local or built-in (like "error" or "any").
	ImportPath string
	// Name is the leaf (un-qualified) name of the type.
	Name string
	// Pointer determines whether the specified type is a pointer or not.
	Pointer bool
}

TypeName represents a parsed Go type name, potentially including a package path and pointer indicator.

func MustTypeName added in v1.5.0

func MustTypeName(n string) (tn TypeName)

MustTypeName is the same as NewTypeName, except it panics in case of an error.

func NewTypeName added in v1.5.0

func NewTypeName(n string) (tn TypeName, err error)

NewTypeName parses a string representation of a type name into a TypeName struct. It returns an error if the syntax is invalid according to its limited regular expression.

func (*TypeName) AsNode added in v1.5.0

func (n *TypeName) AsNode() dst.Expr

AsNode converts the TypeName back into a dst.Expr AST node. Useful for generating code that refers to this type.

func (TypeName) Hash added in v1.5.0

func (n TypeName) Hash(h *fingerprint.Hasher) error

Hash contributes the TypeName's properties to a fingerprint hasher.

func (TypeName) Matches added in v1.5.0

func (n TypeName) Matches(node dst.Expr) bool

Matches determines whether the provided AST expression node represents the same type as this TypeName. This performs a structural comparison based on the limited types supported by the parsing regex (identifiers, selectors, pointers, empty interface).

func (TypeName) MatchesDefinition added in v1.5.0

func (n TypeName) MatchesDefinition(node dst.Expr, importPath string) bool

MatchesDefinition determines whether the provided node matches the definition of this TypeName. The `importPath` argument determines the context in which the assertion is made.

type TypeResolver added in v1.5.0

type TypeResolver interface {
	ResolveType(dst.Expr) types.Type
}

TypeResolver defines the capability to resolve a dst expression to its go/types type.

Jump to

Keyboard shortcuts

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