Documentation
¶
Overview ¶
Package ast provides utilities for working with Go's Abstract Syntax Tree
Index ¶
- Variables
- func Break() *dst.BranchStmt
- 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 IdPath(name, path string) *dst.Ident
- func Idf(format string, a ...interface{}) *dst.Ident
- func IncStmt(x dst.Expr) *dst.IncDecStmt
- 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 SliceType(elt dst.Expr) *dst.ArrayType
- func Star(x dst.Expr) *dst.StarExpr
- 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) (bool, error)
- func (c *Cache) IsDefaultComparable(expr dst.Expr) (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
- 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 StatFn
- type TypeDeclDSL
- type TypeInfo
- type TypeSpecDSL
- type ValueDSL
- type VarDeclDSL
Constants ¶
This section is empty.
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") )
Functions ¶
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 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
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 translatesto a dst.ForStmt
type IfDSL ¶
IfDSL translates to a dst.IfStmt
type IfDecsDSL ¶
type IfDecsDSL struct{ Obj dst.IfStmtDecorations }
IfDecsDSL translates to a dst.IfStmtDecorations
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 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
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