Documentation ¶
Overview ¶
Package bast implements a simple intermediate object model of top level Go declarations from AST of go source files and is designed for use in text based code-generation with Go's text/template templating engine.
Bast's structure can be easily traversed from a template and provides a number of functions to help with data retrieval and other utils.
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 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 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 holds lists of top level declarations found in a set of parsed packages.
It is a reduced model of go source which parses out only top level declarations and provides a simple model and interface for their easy retrieval, enumeration and inspection.
It is returned the by Load function.
func Load ¶
Load loads packages specified by pattern and returns a *Bast of it or an error.
An optional config configures the underlying go build system and other details. See Config.
func (*Bast) AllInterfaces ¶
Funcs returns all interfaces in self, across all packages.
func (*Bast) AllMethods ¶
Funcs returns all methods in self, across all packages.
func (*Bast) AllPackages ¶
AllPackages returns all parsed packages.
func (*Bast) AllStructs ¶
Funcs returns all structs in self, across all packages.
func (*Bast) AnyInterface ¶
Interface returns a interface whose Name==declName from from any package.
func (*Bast) ConstsOfType ¶
ConstsOfType returns all top level constant declarations from a package named pkgPath whose type name equals typeName.
func (*Bast) FieldNames ¶
FieldNames returns a slice of names of fields of a struct named by structName residing in some file in package named pkgPath.
func (*Bast) MethodSet ¶
MethodSet returns all methods from a package named pkgPath whose receiver type matches typeName (star prefixed or not).
func (*Bast) PackageByPath ¶
PackageByPath returns a package by its import path or nil if not found.
func (*Bast) PackageImportPaths ¶
PackageImportPaths returns package paths of all loaded packages.
func (*Bast) PackageNames ¶
PackageNames returns names of all parsed packages.
func (*Bast) PkgInterface ¶
PkgInterface returns an interface whose Name==declName from a package with pkgPath.
func (*Bast) PkgInterfaces ¶
PkgInterfaces returns all interfaces in pkgPath.
func (*Bast) PkgMethod ¶
PkgMethod returns a method whose Name==declName from a package with pkgPath.
func (*Bast) PkgMethods ¶
PkgMethods returns all methods in pkgPath.
func (*Bast) PkgStruct ¶
PkgStruct returns a struct whose Name==declName from a package with pkgPath.
func (*Bast) PkgStructs ¶
PkgStructs returns all structs in pkgPath.
func (*Bast) ResolveBasicType ¶
ResolveBasicType returns the basic type name of a derived type typeName by searching the type hierarchy of parsed packages.
If typeName is already a name of a basic type it is returned as is. If basic type was not found resolved returns an empty string.
Requires [Config.TypeChecking].
func (*Bast) TypesOfType ¶
TypesOfType returns all top level type declarations from a package named pkgPath whose type name equals typeName.
func (*Bast) VarsOfType ¶
VarsOfType returns all top level variable declarations from a package named pkgPath whose type name equals typeName.
type Config ¶
type Config struct { // Dir is the directory in which to run the build system's query tool // that provides information about the packages. // If Dir is empty, the tool is run in the current directory. // // Package patterns given to [ParsePackages] are relative to this directory. // // Default is "." which sets the build dir to current directory. Dir string `json:"dir,omitempty"` // BuildFlags is a list of command-line flags to be passed through to // the build system's query tool. BuildFlags []string `json:"buildFlags,omitempty"` // Env is the environment to use when invoking the build system's query tool. // If Env is nil, the current environment is used. // As in os/exec's Cmd, only the last value in the slice for // each environment key is used. To specify the setting of only // a few variables, append to the current environment, as in: // // opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386") // Env []string `json:"env,omitempty"` // If Tests is set, the loader includes not just the packages // matching a particular pattern but also any related test packages, // including test-only variants of the package and the test executable. // // For example, when using the go command, loading "fmt" with Tests=true // returns four packages, with IDs "fmt" (the standard package), // "fmt [fmt.test]" (the package as compiled for the test), // "fmt_test" (the test functions from source files in package fmt_test), // and "fmt.test" (the test binary). // // In build systems with explicit names for tests, // setting Tests may have no effect. Tests bool `json:"tests,omitempty"` // TypeChecking enables type checking to enable few type resolution // related utilities like [Bast.ResolveBasicType]. // // Default: true TypeChecking bool `json:"typeChecking,omitempty"` // TypeCheckingErrors if enabled will return errors during [Load] if // typechecking or parsed packages failed. // // Default: true TypeCheckingErrors bool `json:"typeCheckingErrors,omitempty"` }
Config configures Load.
type Const ¶
type Const struct { // Model is the declaration base. Model // Type is the const type, empty if undpecified. Type string // Value is the const value, empty if unspecified. Value string }
Const contains info about a constant.
If a variable was declared with implicit type, Type will be empty.
type Declaration ¶
type Declaration interface { // GetFile returns the declarations parent file. GetFile() *File // GetPackage returns the declarations parent package. GetPackage() *Package }
Declaration represents a top level declaration in a Go file.
type DeclarationMap ¶
type DeclarationMap = maps.OrderedMap[string, Declaration]
DeclarationMap maps declarations by their name in parse order.
type Field ¶
type Field struct { // Model is the declaration base. Model // Type is the field type. // // For [Struct] receivers, type will be the bare type name without star or // type parameters. If it is a pointer receiver [Field.Pointer] will be // true. Type parameters are ommited from the name and can be inspected in // parent [Struct] definition. Type string // Tag is the field raw tag string. Tag string // Unnamed is true if field is unnamed and specifies the type only. Unnamed bool // Pointer is true if this field is a pointer method receiver. Pointer bool }
Field contains info about a struct field, method receiver, or method or func type params, params or results.
type FieldMap ¶
type FieldMap = maps.OrderedMap[string, *Field]
FieldMap maps fields by their 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 contians info about a Go source file.
func (*File) HasDecl ¶
HasDecl returns true if a var, const, func or method declaration with name or type, struct or interface with type name was found in this file.
func (*File) ImportSpecFromSelector ¶
func (self *File) ImportSpecFromSelector(selectorExpr string) *ImportSpec
ImportSpecFromSelector returns an import spec from a selectorExpr. If import was not found or selectorExpr is invalid result is nil.
func (*File) Interface ¶
Var returns a Interface declaration from File under name or nil if not found.
type FileMap ¶
type FileMap = maps.OrderedMap[string, *File]
FileMap maps files by their FileName in parse order.
type Func ¶
type Func struct { // Model is the declaration base. Model // TypeParams are type parameters. TypeParams *FieldMap // Params is a list of func arguments. Params *FieldMap // Results is a list of func returns. Results *FieldMap }
Func contains info about a function.
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 contains info about an Package or File import.
func (*ImportSpec) Base ¶
func (self *ImportSpec) Base() string
Base returns the base name from the package path.
type ImportSpecMap ¶
type ImportSpecMap = maps.OrderedMap[string, *ImportSpec]
ImportSpecMap maps imports by their path in parse order.
type Interface ¶
type Interface struct { // Model is the declaration base. Model // Methods is a list of methods defined by the interface. Methods *MethodMap // Interface is a list of inherited interfaces. // // Map is keyed by the embeded interface type name. Interfaces *FieldMap }
Interface contains info about an interface.
func NewInterface ¶
NewInterface returns a new *Interface.
type Method ¶
type Method struct { // Func embeds all Func properties. Func // Receiver is the method receiver. // // Receiver is nil if this is an interface method without a receiver. Receiver *Field }
Method contains info about a method.
type MethodMap ¶
type MethodMap = maps.OrderedMap[string, *Method]
MethodMap maps methods by their 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 bast model base with fields shared by all declarations.
Model implements Declaration interface].
func (*Model) GetPackage ¶
GetPackage returns the declarations parent package.
func (*Model) ImportSpecBySelectorExpr ¶
func (self *Model) ImportSpecBySelectorExpr(selectorExpr string) *ImportSpec
ImportSpecBySelectorExpr returns an ImportSpec whose path is the path of a package from which a type qualified by selectorExpr is imported into go file being parsed. I.e.: "package.TypeName".
It returns nil if not found or selectorExpr is invalid.
func (*Model) ResolveBasicType ¶
ResolveBasicType returns the basic type name of a derived type typeName by searching the type hierarchy of parsed packages.
If typeName is already a name of a basic type it is returned as is. If basic type was not found resolved returns an empty string.
Requires [Config.TypeChecking].
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 contains info about a Go package.
func NewPackage ¶
NewPackage returns a new *Package.
func (*Package) DeclFile ¶
DeclFile returns the full filename of a file that contains the declaration whose type equals typeName. If not found result is an empty string.
func (*Package) HasDecl ¶
HasDecl returns true if declaration with typeName was found in this package.
func (*Package) Interface ¶
Var returns a Interface declaration from File under name or nil if not found.
type PackageMap ¶
type PackageMap = maps.OrderedMap[string, *Package]
PackageMap maps packages by their import path.
type Printer ¶
type Printer struct { PrintDoc bool PrintComments bool PrintConsts bool PrintVars bool PrintTypes bool PrintFuncs bool PrintMethods bool PrintStructs bool PrintInterfaces bool }
func DefaultPrinter ¶
func DefaultPrinter() *Printer
DefaultPrinter returns the default print configuration.
type Struct ¶
type Struct struct { // Model is the declaration base. Model // Fields is a list of struct fields. Fields *FieldMap // TypeParams are type parameters. TypeParams *FieldMap }
Struct contains info about a struct.
type Type ¶
type Type struct { // Model is the declaration base. Model // Type is Type's underlying type. // // The name can be a selector qualifying the package it originates in. Type string // IsAlias is true if type is an alias of the type it derives from. IsAlias bool // TypeParams are type parameters. TypeParams *FieldMap }
Type contains info about a type.
type Var ¶
type Var struct { // Model is the declaration base. Model // Type is the const type, empty if undpecified. Type string // Value is the const value, empty if undpecified. Value string }
Var contains info about a variable.
If a variable was declared with implicit type, Type will be empty. If a variable was declared without an initial value, Value will be empty.