Documentation ¶
Overview ¶
Package ast provides utilities for working with Go's Abstract Syntax Tree
Index ¶
- Constants
- Variables
- func Break() *dst.BranchStmt
- func CallDecs(before, after dst.SpaceType) dst.CallExprDecorations
- func Continue() *dst.BranchStmt
- func Ellipsis(elt dst.Expr) *dst.Ellipsis
- func FieldList(fields ...*dst.Field) *dst.FieldList
- func Id(name string) *dst.Ident
- func IdDecs(i *dst.Ident, decs dst.IdentDecorations) *dst.Ident
- func IdPath(name, path string) *dst.Ident
- func IdentDecs(before, after dst.SpaceType) dst.IdentDecorations
- func Idf(format string, a ...interface{}) *dst.Ident
- func IncStmt(x dst.Expr) *dst.IncDecStmt
- func LitBool(value bool) *dst.Ident
- func LitInt(value int) *dst.BasicLit
- func LitString(value string) *dst.BasicLit
- func LitStringf(format string, a ...interface{}) *dst.BasicLit
- func NodeDecsf(format string, a ...interface{}) dst.NodeDecs
- func Paren(x dst.Expr) *dst.ParenExpr
- func Return(results ...dst.Expr) *dst.ReturnStmt
- func SelDecs(before, after dst.SpaceType) dst.SelectorExprDecorations
- func SliceType(elt dst.Expr) *dst.ArrayType
- func StarDecs(before, after dst.SpaceType) dst.StarExprDecorations
- func Struct(fields ...*dst.Field) *dst.StructType
- func StructFromList(fieldList *dst.FieldList) *dst.StructType
- func Un(op token.Token, x dst.Expr) *dst.UnaryExpr
- func Var(specs ...dst.Spec) *dst.DeclStmt
- type AssignDSL
- type AssignDecsDSL
- type BinDSL
- type BlockDSL
- type Cache
- func (c *Cache) FindPackage(dir string) (string, error)
- func (c *Cache) IsComparable(expr dst.Expr, parentType TypeInfo) (bool, error)
- func (c *Cache) IsDefaultComparable(expr dst.Expr, parentType TypeInfo) (bool, error)
- func (c *Cache) LoadPackage(pkgPattern string) error
- func (c *Cache) MockableTypes(onlyExported bool) []dst.Ident
- func (c *Cache) Type(id dst.Ident, contextPkg string, testImport bool) (TypeInfo, error)
- type CallDSL
- type CompDSL
- type ExprDSL
- type ExprDecsDSL
- type FieldDSL
- type FieldDecsDSL
- type FnDSL
- func (d FnDSL) Body(list ...dst.Stmt) FnDSL
- func (d FnDSL) Decs(decs dst.FuncDeclDecorations) FnDSL
- func (d FnDSL) ParamList(fieldList *dst.FieldList) FnDSL
- func (d FnDSL) Params(fields ...*dst.Field) FnDSL
- func (d FnDSL) Recv(fields ...*dst.Field) FnDSL
- func (d FnDSL) ResultList(fieldList *dst.FieldList) FnDSL
- func (d FnDSL) Results(fields ...*dst.Field) FnDSL
- func (d FnDSL) TypeParams(fieldList *dst.FieldList) FnDSL
- type FnLitDSL
- type FnTypeDSL
- type ForDSL
- type IfDSL
- type IfDecsDSL
- type IndexDSL
- type KeyValueDSL
- type KeyValueDecsDSL
- type LoadFn
- type MapTypeDSL
- type RangeDSL
- type ReadFileFn
- type SelDSL
- type SliceExprDSL
- type StarDSL
- type StatFn
- type TypeDeclDSL
- type TypeInfo
- type TypeSpecDSL
- type ValueDSL
- type VarDeclDSL
Constants ¶
const (
GenTypeSuffix = "_genType"
)
Variables ¶
var ( // ErrTypeNotFound is returned when a type isn't in the cache and can't be // loaded ErrTypeNotFound = errors.New("type not found") // ErrInvalidType is returned when the type doesn't have the expected // structure ErrInvalidType = errors.New("type did not have expected format") // ErrMissingModuleDirective is returned when the go.mod file is missing // its module directive ErrMissingModuleDirective = errors.New("missing module directive") // ErrMixedRecvTypes is returned when fabricating an interface for a type // with multiple exported methods that differ by receiver type (i.e: some // pass-by-value receivers and some pass-by-reference receivers). If all // exported methods have a consistent receiver type and only the // non-exported methods are different, non-exported methods are dropped // and the cache emits a warning log. ErrMixedRecvTypes = errors.New("single type is used with exported and differing receiver types") )
Functions ¶
func CallDecs ¶ added in v0.28.0
func CallDecs(before, after dst.SpaceType) dst.CallExprDecorations
CallDecs creates a dst.CallExprDecorations
func IdentDecs ¶ added in v0.28.0
func IdentDecs(before, after dst.SpaceType) dst.IdentDecorations
IdentDecs returns a dst.IdentDecorations
func IncStmt ¶
func IncStmt(x dst.Expr) *dst.IncDecStmt
IncStmt creates a dst.IncDecStmt for incrementing an expression
func LitStringf ¶
LitStringf returns a formatted dst.BasicLit with a literal string value
func NodeDecsf ¶
NodeDecsf formats a string into a standard node decoration, chopping down the line as necessary. The format param should be a single long line starting with a line comment (//). The format line is broken into multiple lines each with at least one word.
func SelDecs ¶ added in v0.28.0
func SelDecs(before, after dst.SpaceType) dst.SelectorExprDecorations
SelDecs returns a dst.SelectorExprDecorations
func StarDecs ¶ added in v0.28.0
func StarDecs(before, after dst.SpaceType) dst.StarExprDecorations
StarDecs returns a dst.StarExprDecorations
func StructFromList ¶
func StructFromList(fieldList *dst.FieldList) *dst.StructType
StructFromList returns a dst.StructType given a dst.FieldList
Types ¶
type AssignDSL ¶
type AssignDSL struct{ Obj *dst.AssignStmt }
AssignDSL translates to a dst.AssignStmt
func (AssignDSL) Decs ¶
func (d AssignDSL) Decs(decs dst.AssignStmtDecorations) AssignDSL
Decs adds decorations to an AssignDSL
type AssignDecsDSL ¶
type AssignDecsDSL struct{ Obj dst.AssignStmtDecorations }
AssignDecsDSL translates to a dst.AssignStmtDecorations
func AssignDecs ¶
func AssignDecs(before dst.SpaceType) AssignDecsDSL
AssignDecs creates a new AssignDecsDSL
func (AssignDecsDSL) After ¶
func (d AssignDecsDSL) After(after dst.SpaceType) AssignDecsDSL
After adds whitespace after an assign statement
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache loads packages from the AST and caches the results
func (*Cache) FindPackage ¶
FindPackage finds the package for a given directory
func (*Cache) IsComparable ¶
IsComparable determines if an expression is comparable. The optional parentType can be used to supply type parameters.
func (*Cache) IsDefaultComparable ¶
IsDefaultComparable determines if an expression is comparable. Returns the same results as IsComparable but pointers and interfaces are not comparable by default (interface implementations that are not comparable and put into a map key will panic at runtime and by default pointers use a deep hash to be comparable).
func (*Cache) LoadPackage ¶
LoadPackage loads the specified pattern of package(s) and returns a list of mock-able types
func (*Cache) MockableTypes ¶
MockableTypes returns all the mockable types loaded so far
type CallDSL ¶
CallDSL translates to a dst.CallExpr
type CompDSL ¶
type CompDSL struct{ Obj *dst.CompositeLit }
CompDSL translates to a dst.CompositeLit
type ExprDecsDSL ¶
type ExprDecsDSL struct{ Obj dst.ExprStmtDecorations }
ExprDecsDSL translates to a dst.ExprStmtDecorations
type FieldDSL ¶
FieldDSL translates to a dst.Field
type FieldDecsDSL ¶
type FieldDecsDSL struct{ Obj dst.FieldDecorations }
FieldDecsDSL translates to a dst.FieldDecorations
func FieldDecs ¶
func FieldDecs(before, after dst.SpaceType) FieldDecsDSL
FieldDecs creates a new FieldDecsDSL
type FnDSL ¶
FnDSL translates to a dst.GenDecl containing a function type
func (FnDSL) Decs ¶
func (d FnDSL) Decs(decs dst.FuncDeclDecorations) FnDSL
Decs adds decorations to a FnDSL
func (FnDSL) ResultList ¶
ResultList specifies a result FieldList for a function
type ForDSL ¶
ForDSL translates to a dst.ForStmt
type IfDSL ¶
IfDSL translates to a dst.IfStmt
func (IfDSL) Decs ¶
func (d IfDSL) Decs(decs dst.IfStmtDecorations) IfDSL
Decs adds decorations to a IfDSL
type IfDecsDSL ¶
type IfDecsDSL struct{ Obj dst.IfStmtDecorations }
IfDecsDSL translates to a dst.IfStmtDecorations
type IndexDSL ¶
type IndexDSL struct{ Obj *dst.IndexListExpr }
IndexDSL translates to a dst.IndexListExpr
type KeyValueDSL ¶
type KeyValueDSL struct{ Obj *dst.KeyValueExpr }
KeyValueDSL translates to a dst.KeyValueExpr
func (KeyValueDSL) Decs ¶
func (d KeyValueDSL) Decs(decs dst.KeyValueExprDecorations) KeyValueDSL
Decs adds decorations to a KeyValueDSL
func (KeyValueDSL) Value ¶
func (d KeyValueDSL) Value(value dst.Expr) KeyValueDSL
Value specifies the value
type KeyValueDecsDSL ¶
type KeyValueDecsDSL struct{ Obj dst.KeyValueExprDecorations }
KeyValueDecsDSL translates to a dst.KeyValueExprDecorations
func KeyValueDecs ¶
func KeyValueDecs(before dst.SpaceType) KeyValueDecsDSL
KeyValueDecs creates a new KeyValueDecsDSL
func (KeyValueDecsDSL) After ¶
func (d KeyValueDecsDSL) After(after dst.SpaceType) KeyValueDecsDSL
After adds decorations after the KeyValueDSL
type MapTypeDSL ¶
MapTypeDSL translates to a dst.MapType
func (MapTypeDSL) Value ¶
func (d MapTypeDSL) Value(value dst.Expr) MapTypeDSL
Value specifies the value expression of a dst.MapType
type RangeDSL ¶
RangeDSL translates to a dst.RangeStmt
type ReadFileFn ¶ added in v0.25.0
ReadFileFn is the function type of os.ReadFile
type SelDSL ¶
type SelDSL struct{ Obj *dst.SelectorExpr }
SelDSL translates to a dst.SelectorExpr
type SliceExprDSL ¶
SliceExprDSL translates to a dst.SliceExpr
func (SliceExprDSL) High ¶
func (d SliceExprDSL) High(high dst.Expr) SliceExprDSL
High specifies the high expression of a slice expression
func (SliceExprDSL) Low ¶
func (d SliceExprDSL) Low(low dst.Expr) SliceExprDSL
Low specifies the low expression of a slice expression
type TypeDeclDSL ¶
TypeDeclDSL translates various types into a dst.GenDecl
func TypeDecl ¶
func TypeDecl(typeSpec *dst.TypeSpec) TypeDeclDSL
TypeDecl creates a new TypeDeclDSL
func (TypeDeclDSL) Decs ¶
func (d TypeDeclDSL) Decs(decs dst.GenDeclDecorations) TypeDeclDSL
Decs adds decorations to a TypeDeclDSL
type TypeSpecDSL ¶
TypeSpecDSL translates to a dst.TypeSpec
func (TypeSpecDSL) Type ¶
func (d TypeSpecDSL) Type(typ dst.Expr) TypeSpecDSL
Type creates a new TypeSpecDSL
func (TypeSpecDSL) TypeParams ¶ added in v0.27.0
func (d TypeSpecDSL) TypeParams(typeParams *dst.FieldList) TypeSpecDSL
TypeParams adds type parameters to TypeDeclDSL
type ValueDSL ¶
ValueDSL translates to a dst.ValueSpec
type VarDeclDSL ¶
VarDeclDSL translates variable declaration into a dst.GenDecl
func (VarDeclDSL) Decs ¶
func (d VarDeclDSL) Decs(decs dst.GenDeclDecorations) VarDeclDSL
Decs adds decorations to a VarDeclDSL