Documentation
¶
Index ¶
- func Print(w io.Writer, bast *Bast)
- type Bast
- func (self *Bast) AllConsts() (out []*Const)
- func (self *Bast) AllFuncs() (out []*Func)
- func (self *Bast) AllInterfaces() (out []*Interface)
- func (self *Bast) AllMethods() (out []*Method)
- func (self *Bast) AllPackages() (out []*Package)
- func (self *Bast) AllStructs() (out []*Struct)
- func (self *Bast) AllTypes() (out []*Type)
- func (self *Bast) AllVars() (out []*Var)
- func (self *Bast) AnyConst(declName string) (out *Const)
- func (self *Bast) AnyFunc(declName string) (out *Func)
- func (self *Bast) AnyInterface(declName string) (out *Interface)
- func (self *Bast) AnyMethod(declName string) (out *Method)
- func (self *Bast) AnyStruct(declName string) (out *Struct)
- func (self *Bast) AnyType(declName string) (out *Type)
- func (self *Bast) AnyVar(declName string) (out *Var)
- func (self *Bast) ConstsOfType(pkgPath, typeName string) (out []*Const)
- func (self *Bast) FieldNames(pkgPath, structName string) (out []string)
- func (self *Bast) MethodSet(pkgPath, typeName string) (out []*Method)
- func (self *Bast) PackageByPath(pkgPath string) (p *Package)
- func (self *Bast) PackageImportPaths() []string
- func (self *Bast) PackageNames() (out []string)
- func (self *Bast) Packages() []*Package
- func (self *Bast) PkgConst(pkgPath, declName string) (out *Const)
- func (self *Bast) PkgConsts(pkgPath string) (out []*Const)
- func (self *Bast) PkgFunc(pkgPath, declName string) (out *Func)
- func (self *Bast) PkgFuncs(pkgPath string) (out []*Func)
- func (self *Bast) PkgInterface(pkgPath, declName string) (out *Interface)
- func (self *Bast) PkgInterfaces(pkgPath string) (out []*Interface)
- func (self *Bast) PkgMethod(pkgPath, declName string) (out *Method)
- func (self *Bast) PkgMethods(pkgPath string) (out []*Method)
- func (self *Bast) PkgStruct(pkgPath, declName string) (out *Struct)
- func (self *Bast) PkgStructs(pkgPath string) (out []*Struct)
- func (self *Bast) PkgType(pkgPath, declName string) (out *Type)
- func (self *Bast) PkgTypes(pkgPath string) (out []*Type)
- func (self *Bast) PkgVar(pkgPath, declName string) (out *Var)
- func (self *Bast) PkgVars(pkgPath string) (out []*Var)
- func (self *Bast) ResolveBasicType(typeName string) string
- func (self *Bast) TypesOfType(pkgPath, typeName string) (out []*Type)
- func (self *Bast) VarsOfType(pkgPath, typeName string) (out []*Var)
- type Config
- type Const
- type Declaration
- type DeclarationMap
- type Field
- type FieldMap
- type File
- func (self *File) Const(name string) (out *Const)
- func (self *File) Func(name string) (out *Func)
- func (self *File) HasDecl(name string) (b bool)
- func (self *File) ImportSpecFromSelector(selectorExpr string) *ImportSpec
- func (self *File) Interface(name string) (out *Interface)
- func (self *File) Method(name string) (out *Method)
- func (self *File) Struct(name string) (out *Struct)
- func (self *File) Type(name string) (out *Type)
- func (self *File) Var(name string) (out *Var)
- type FileMap
- type Func
- type ImportSpec
- type ImportSpecMap
- type Interface
- type InterfaceMap
- type Method
- type MethodMap
- type Model
- type Package
- func (self *Package) Const(name string) (out *Const)
- func (self *Package) DeclFile(typeName string) string
- func (self *Package) Func(name string) (out *Func)
- func (self *Package) HasDecl(typeName string) bool
- func (self *Package) Interface(name string) (out *Interface)
- func (self *Package) Method(name string) (out *Method)
- func (self *Package) Struct(name string) (out *Struct)
- func (self *Package) Type(name string) (out *Type)
- func (self *Package) Var(name string) (out *Var)
- type PackageMap
- type Parser
- type Printer
- type Struct
- type Type
- type Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bast ¶
type Bast struct {
// contains filtered or unexported fields
}
Bast is the top-level type that holds parsed packages and their declarations.
It provides methods for querying and retrieving declarations across all packages.
func Load ¶
Load loads the Go packages matching the given patterns and returns a Bast representation.
cfg configures the loading process; if nil, DefaultConfig is used.
Patterns follow the same syntax as go list or go build (e.g., "./...", "github.com/user/repo").
func (*Bast) AllInterfaces ¶
AllInterfaces returns all top-level interfaces across all parsed packages.
func (*Bast) AllMethods ¶
AllMethods returns all top-level methods across all parsed packages.
func (*Bast) AllPackages ¶
AllPackages returns all parsed packages.
func (*Bast) AllStructs ¶
AllStructs returns all top-level structs across all parsed packages.
func (*Bast) AnyConst ¶
AnyConst returns the constant named declName from any parsed package, or nil if not found.
func (*Bast) AnyFunc ¶
AnyFunc returns the function named declName from any parsed package, or nil if not found.
func (*Bast) AnyInterface ¶
AnyInterface returns the interface named declName from any parsed package, or nil if not found.
func (*Bast) AnyMethod ¶
AnyMethod returns the method named declName from any parsed package, or nil if not found.
func (*Bast) AnyStruct ¶
AnyStruct returns the struct named declName from any parsed package, or nil if not found.
func (*Bast) AnyType ¶
AnyType returns the type named declName from any parsed package, or nil if not found.
func (*Bast) AnyVar ¶
AnyVar returns the variable named declName from any parsed package, or nil if not found.
func (*Bast) ConstsOfType ¶
ConstsOfType returns all top-level constant declarations from the package with path pkgPath whose type matches typeName.
func (*Bast) FieldNames ¶
FieldNames returns the names of the fields of the struct named structName in the package with path pkgPath.
func (*Bast) MethodSet ¶
MethodSet returns all methods from the package with path pkgPath whose receiver type matches typeName (with or without a pointer prefix).
func (*Bast) PackageByPath ¶
PackageByPath returns the package with the given import path, or nil if not found.
func (*Bast) PackageImportPaths ¶
PackageImportPaths returns the import paths of all loaded packages.
func (*Bast) PackageNames ¶
PackageNames returns the names of all parsed packages.
func (*Bast) PkgConst ¶
PkgConst returns the constant named declName from the package with path pkgPath, or nil if not found.
func (*Bast) PkgConsts ¶
PkgConsts returns all top-level constants in the package with path pkgPath.
func (*Bast) PkgFunc ¶
PkgFunc returns the function named declName from the package with path pkgPath, or nil if not found.
func (*Bast) PkgInterface ¶
PkgInterface returns the interface named declName from the package with path pkgPath, or nil if not found.
func (*Bast) PkgInterfaces ¶
PkgInterfaces returns all top-level interfaces in the package with path pkgPath.
func (*Bast) PkgMethod ¶
PkgMethod returns the method named declName from the package with path pkgPath, or nil if not found.
func (*Bast) PkgMethods ¶
PkgMethods returns all top-level methods in the package with path pkgPath.
func (*Bast) PkgStruct ¶
PkgStruct returns the struct named declName from the package with path pkgPath, or nil if not found.
func (*Bast) PkgStructs ¶
PkgStructs returns all top-level structs in the package with path pkgPath.
func (*Bast) PkgType ¶
PkgType returns the type named declName from the package with path pkgPath, or nil if not found.
func (*Bast) PkgVar ¶
PkgVar returns the variable named declName from the package with path pkgPath, or nil if not found.
func (*Bast) ResolveBasicType ¶
ResolveBasicType resolves the underlying basic type name for the given typeName by searching the type hierarchy of the parsed packages.
If typeName is already a basic type name, it returns typeName as is. If no basic type is found, it returns an empty string.
This method requires Config.TypeChecking to be enabled.
func (*Bast) TypesOfType ¶
TypesOfType returns all top-level type declarations from the package with path pkgPath whose underlying type matches typeName.
func (*Bast) VarsOfType ¶
VarsOfType returns all top-level variable declarations from the package with path pkgPath whose type matches typeName.
type Config ¶
type Config struct {
// Dir is the base directory for the build system's query tool.
// If empty, the current directory is used.
//
// Package patterns in Load are relative to this directory.
// Default is ".".
Dir string `json:"dir,omitempty"`
// BuildFlags are additional command-line flags passed to the build system's query tool.
BuildFlags []string `json:"buildFlags,omitempty"`
// Env is the environment variables used when invoking the build system's query tool.
// If nil, the current environment is used.
// Only the last value for each key is used.
//
// To set specific variables, append to os.Environ():
// opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386")
Env []string `json:"env,omitempty"`
// Tests, if true, includes related test packages when loading.
// This includes test variants of the package and the test executable.
Tests bool `json:"tests,omitempty"`
// TypeChecking enables type checking during loading, required for type resolution utilities
// like Bast.ResolveBasicType.
// Default is true.
TypeChecking bool `json:"typeChecking,omitempty"`
// TypeCheckingErrors, if true, causes Load to return an error if type checking fails
// or if any loaded package has errors.
// Default is true.
TypeCheckingErrors bool `json:"typeCheckingErrors,omitempty"`
}
Config configures the loading and parsing behavior of Load.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with default values.
type Const ¶
type Const struct {
Model
// Type is the constant's type, empty if inferred.
Type string
// Value is the constant's value.
Value string
}
Const represents a top-level constant declaration.
type Declaration ¶
type Declaration interface {
// GetFile returns the declarations parent file.
GetFile() *File
// GetPackage returns the declarations parent package.
GetPackage() *Package
}
Declaration is the interface implemented by all top-level declarations.
type DeclarationMap ¶
type DeclarationMap = maps.OrderedMap[string, Declaration]
DeclarationMap is an ordered map of declarations keyed by their name in parse order.
type Field ¶
type Field struct {
Model
// Type is the field's type.
//
// For method receivers, this is the bare type name without "*" or type parameters.
// Use Pointer to check for pointer receivers, and inspect the parent for type parameters.
Type string
// Tag is the field's raw struct tag string.
Tag string
// Unnamed is true if the field is unnamed (embedded field).
Unnamed bool
// Pointer is true if this is a pointer receiver for a method.
Pointer bool
}
Field represents a field in a struct, a parameter or result in a function, or a receiver in a method.
type FieldMap ¶
type FieldMap = maps.OrderedMap[string, *Field]
FieldMap is an ordered map of fields keyed by name in parse order.
type File ¶
type File struct {
// Comments are the file comments, grouped by separation, including docs.
Comments [][]string
// Doc is the file doc comment.
Doc []string
// Name is the File name, a full file path.
Name string
// Imports is a list of file imports.
Imports *ImportSpecMap
// Declarations is a list of top level declarations in the file.
Declarations *DeclarationMap
// contains filtered or unexported fields
}
File represents a parsed Go source file.
It contains comments, imports, and top-level declarations.
func (*File) ImportSpecFromSelector ¶
func (self *File) ImportSpecFromSelector(selectorExpr string) *ImportSpec
ImportSpecFromSelector returns the ImportSpec for the given selector expression (e.g., "pkg.Type"). It returns nil if the import is not found or the selector is invalid.
func (*File) Interface ¶
Interface returns the interface named name from this file, or nil if not found.
type FileMap ¶
type FileMap = maps.OrderedMap[string, *File]
FileMap is an ordered map of files keyed by their filename in parse order.
type Func ¶
type Func struct {
Model
// TypeParams are the function's type parameters.
TypeParams *FieldMap
// Params are the function's parameters.
Params *FieldMap
// Results are the function's return values.
Results *FieldMap
}
Func represents a top-level function declaration.
type ImportSpec ¶
type ImportSpec struct {
// Doc is the import doc comment.
Doc []string
// Name is the import name, possibly empty, "." or some custom name.
Name string
// Path is the import path.
Path string
}
ImportSpec represents an import specification for a package.
func NewImport ¶
func NewImport(name, path string) *ImportSpec
NewImport creates a new ImportSpec with the given name and path.
func (*ImportSpec) Base ¶
func (self *ImportSpec) Base() string
Base returns the base name of the imported package path.
type ImportSpecMap ¶
type ImportSpecMap = maps.OrderedMap[string, *ImportSpec]
ImportSpecMap is an ordered map of import specs keyed by their path in parse order.
type Interface ¶
type Interface struct {
Model
// Methods are the methods declared by this interface.
Methods *MethodMap
// Interfaces are the embedded interfaces.
//
// Keyed by the embedded interface type name.
Interfaces *InterfaceMap
// TypeParams are the interface's type parameters.
TypeParams *FieldMap
}
Interface represents a top-level interface type declaration.
func NewInterface ¶
NewInterface creates a new Interface for the given file and name.
type InterfaceMap ¶
type InterfaceMap = maps.OrderedMap[string, *Interface]
InterfaceMap is an ordered map of interfaces keyed by name in parse order.
type Method ¶
type Method struct {
Func
// Receiver is the method's receiver, or nil for interface methods.
Receiver *Field
}
Method represents a top-level method declaration.
type MethodMap ¶
type MethodMap = maps.OrderedMap[string, *Method]
MethodMap is an ordered map of methods keyed by name in parse order.
type Model ¶
type Model struct {
// Doc is the declaration doc comment.
Doc []string
// Name is the declaration name.
//
// For [Struct], this will be the bare name of the struct type without type
// parameters. Type parameters are stored separately in a [Struct]
// definition.
//
// If struct field is unnamed Name will be equal to Type.
// [Field.Unnamed] will be set to true as well.
Name string
// contains filtered or unexported fields
}
Model is the base struct embedded by all declarations.
It provides common fields like documentation and name, and implements the Declaration interface.
func (*Model) GetPackage ¶
GetPackage returns the parent package of the declaration.
func (*Model) ImportSpecBySelectorExpr ¶
func (self *Model) ImportSpecBySelectorExpr(selectorExpr string) *ImportSpec
ImportSpecBySelectorExpr returns the ImportSpec for the package from which the type qualified by selectorExpr (e.g., "pkg.TypeName") is imported.
It returns nil if not found or selectorExpr is invalid.
func (*Model) ResolveBasicType ¶
ResolveBasicType resolves the underlying basic type name for the given typeName by searching the type hierarchy of the parsed packages.
If typeName is already a basic type name, it returns typeName as is. If no basic type is found, it returns an empty string.
This method requires Config.TypeChecking to be enabled.
type Package ¶
type Package struct {
// Name is the package name, without path, as it appears in source code.
Name string
// Path is the package import path as used by go compiler.
Path string
// Files maps definitions of parsed go files by their full path.
Files *FileMap
// contains filtered or unexported fields
}
Package represents a parsed Go package.
It contains the package name, import path, files, and top-level declarations.
func NewPackage ¶
NewPackage creates a new Package with the given name, path, and underlying packages.Package.
func (*Package) Const ¶
Const returns the constant named name from this package, or nil if not found.
func (*Package) DeclFile ¶
DeclFile returns the full filename of the file containing the declaration named typeName in this package. It returns an empty string if not found.
func (*Package) HasDecl ¶
HasDecl returns true if a declaration named typeName exists in this package.
func (*Package) Interface ¶
Interface returns the interface named name from this package, or nil if not found.
func (*Package) Method ¶
Method returns the method named name from this package, or nil if not found.
func (*Package) Struct ¶
Struct returns the struct named name from this package, or nil if not found.
type PackageMap ¶
type PackageMap = maps.OrderedMap[string, *Package]
PackageMap is an ordered map of packages keyed by their import path.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser transforms loaded go/packages into the bast model.
type Printer ¶
type Printer struct {
// PrintDoc, if true, includes documentation comments.
PrintDoc bool
// PrintComments, if true, includes non-doc comments.
PrintComments bool
// PrintConsts, if true, includes constants.
PrintConsts bool
// PrintVars, if true, includes variables.
PrintVars bool
// PrintTypes, if true, includes type declarations.
PrintTypes bool
// PrintFuncs, if true, includes functions.
PrintFuncs bool
// PrintMethods, if true, includes methods.
PrintMethods bool
// PrintStructs, if true, includes structs.
PrintStructs bool
// PrintInterfaces, if true, includes interfaces.
PrintInterfaces bool
// Indentation is the string used for indenting output (default "\t").
Indentation string
}
Printer configures the formatting options for printing a Bast model.
func DefaultPrinter ¶
func DefaultPrinter() *Printer
DefaultPrinter returns a Printer with all printing options enabled and tab indentation.
type Struct ¶
type Struct struct {
Model
// Fields are the struct's fields.
Fields *FieldMap
// TypeParams are the struct's type parameters.
TypeParams *FieldMap
}
Struct represents a top-level struct type declaration.
type Type ¶
type Type struct {
Model
// Type is the underlying type of this type declaration.
//
// This may be a qualified selector like "pkg.Type".
Type string
// IsAlias is true if this is a type alias (using := instead of =).
IsAlias bool
// TypeParams are the type's type parameters.
TypeParams *FieldMap
}
Type represents a top-level type declaration (not struct or interface).