ast

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation ¶

Overview ¶

Package ast provides utilities for working with Go's Abstract Syntax Tree

Index ¶

Constants ¶

View Source
const (
	GenTypeSuffix = "_genType"
)

Variables ¶

View Source
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 Break ¶

func Break() *dst.BranchStmt

Break creates a break dst.BranchStmt

func CallDecs ¶ added in v0.28.0

func CallDecs(before, after dst.SpaceType) dst.CallExprDecorations

CallDecs creates a dst.CallExprDecorations

func Continue ¶

func Continue() *dst.BranchStmt

Continue creates a continue dst.BranchStmt

func Ellipsis ¶

func Ellipsis(elt dst.Expr) *dst.Ellipsis

Ellipsis translates an expression to a dst.Ellipsis

func FieldList ¶

func FieldList(fields ...*dst.Field) *dst.FieldList

FieldList translates to a dst.FieldList

func Id ¶

func Id(name string) *dst.Ident

Id returns a named dst.Ident

func IdDecs ¶ added in v0.28.0

func IdDecs(i *dst.Ident, decs dst.IdentDecorations) *dst.Ident

IdDecs adds decorations to a dst.Ident

func IdPath ¶

func IdPath(name, path string) *dst.Ident

IdPath returns a dst.Ident with a name and path

func IdentDecs ¶ added in v0.28.0

func IdentDecs(before, after dst.SpaceType) dst.IdentDecorations

IdentDecs returns a dst.IdentDecorations

func Idf ¶

func Idf(format string, a ...interface{}) *dst.Ident

Idf returns a formatted dst.Ident

func IncStmt ¶

func IncStmt(x dst.Expr) *dst.IncDecStmt

IncStmt creates a dst.IncDecStmt for incrementing an expression

func LitBool ¶ added in v0.28.0

func LitBool(value bool) *dst.Ident

LitBool returns a dst.Ident with a literal bool value

func LitInt ¶

func LitInt(value int) *dst.BasicLit

LitInt returns a dst.BasicLit with a literal int value

func LitString ¶

func LitString(value string) *dst.BasicLit

LitString returns a dst.BasicLit with a literal string value

func LitStringf ¶

func LitStringf(format string, a ...interface{}) *dst.BasicLit

LitStringf returns a formatted dst.BasicLit with a literal string value

func NodeDecsf ¶

func NodeDecsf(format string, a ...interface{}) dst.NodeDecs

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 Paren ¶

func Paren(x dst.Expr) *dst.ParenExpr

Paren translates to a dst.ParenExpr

func Return ¶

func Return(results ...dst.Expr) *dst.ReturnStmt

Return returns a dst.ReturnStmt

func SelDecs ¶ added in v0.28.0

func SelDecs(before, after dst.SpaceType) dst.SelectorExprDecorations

SelDecs returns a dst.SelectorExprDecorations

func SliceType ¶

func SliceType(elt dst.Expr) *dst.ArrayType

SliceType returns a dst.ArrayType representing a slice

func StarDecs ¶ added in v0.28.0

func StarDecs(before, after dst.SpaceType) dst.StarExprDecorations

StarDecs returns a dst.StarExprDecorations

func Struct ¶

func Struct(fields ...*dst.Field) *dst.StructType

Struct returns a dst.StructType

func StructFromList ¶

func StructFromList(fieldList *dst.FieldList) *dst.StructType

StructFromList returns a dst.StructType given a dst.FieldList

func Un ¶

func Un(op token.Token, x dst.Expr) *dst.UnaryExpr

Un returns a dst.UnaryExpr

func Var ¶

func Var(specs ...dst.Spec) *dst.DeclStmt

Var returns a var dst.DeclStmt

Types ¶

type AssignDSL ¶

type AssignDSL struct{ Obj *dst.AssignStmt }

AssignDSL translates to a dst.AssignStmt

func Assign ¶

func Assign(lhs ...dst.Expr) AssignDSL

Assign creates a new AssignDSL

func (AssignDSL) Decs ¶

Decs adds decorations to an AssignDSL

func (AssignDSL) Rhs ¶

func (d AssignDSL) Rhs(rhs ...dst.Expr) AssignDSL

Rhs specifies the right-hand expressions in the assignment

func (AssignDSL) Tok ¶

func (d AssignDSL) Tok(tok token.Token) AssignDSL

Tok specifies the token used in an assignment

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 BinDSL ¶

type BinDSL struct{ Obj *dst.BinaryExpr }

BinDSL translates to a dst.BinaryExpr

func Bin ¶

func Bin(x dst.Expr) BinDSL

Bin creates a new BinDSL

func (BinDSL) Op ¶

func (d BinDSL) Op(op token.Token) BinDSL

Op specifies the operator

func (BinDSL) Y ¶

func (d BinDSL) Y(y dst.Expr) BinDSL

Y specifies the right side expression

type BlockDSL ¶

type BlockDSL struct{ Obj *dst.BlockStmt }

BlockDSL translates to a dst.BlockStmt

func Block ¶

func Block(list ...dst.Stmt) BlockDSL

Block creates a new BlockDSL for a list of statements

type Cache ¶

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

Cache loads packages from the AST and caches the results

func NewCache ¶

func NewCache(load LoadFn, stat StatFn, readFile ReadFileFn, metrics metrics.Metrics) *Cache

NewCache returns a new empty Caches

func (*Cache) FindPackage ¶

func (c *Cache) FindPackage(dir string) (string, error)

FindPackage finds the package for a given directory

func (*Cache) IsComparable ¶

func (c *Cache) IsComparable(expr dst.Expr, parentType TypeInfo) (bool, error)

IsComparable determines if an expression is comparable. The optional parentType can be used to supply type parameters.

func (*Cache) IsDefaultComparable ¶

func (c *Cache) IsDefaultComparable(expr dst.Expr, parentType TypeInfo) (bool, error)

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 ¶

func (c *Cache) LoadPackage(pkgPattern string) error

LoadPackage loads the specified pattern of package(s) and returns a list of mock-able types

func (*Cache) MockableTypes ¶

func (c *Cache) MockableTypes(onlyExported bool) []dst.Ident

MockableTypes returns all the mockable types loaded so far

func (*Cache) Type ¶

func (c *Cache) Type(id dst.Ident, contextPkg string, testImport bool) (TypeInfo, error)

Type returns the requested TypeSpec or an error if the type can't be found

type CallDSL ¶

type CallDSL struct{ Obj *dst.CallExpr }

CallDSL translates to a dst.CallExpr

func Call ¶

func Call(fun dst.Expr) CallDSL

Call creates a new CallDSL

func (CallDSL) Args ¶

func (d CallDSL) Args(args ...dst.Expr) CallDSL

Args specifies the arguments to a call

func (CallDSL) Decs ¶ added in v0.28.0

func (d CallDSL) Decs(decs dst.CallExprDecorations) CallDSL

Decs specifies the decorations for a call

func (CallDSL) Ellipsis ¶

func (d CallDSL) Ellipsis(ellipsis bool) CallDSL

Ellipsis specifies if the last argument is variadic

type CompDSL ¶

type CompDSL struct{ Obj *dst.CompositeLit }

CompDSL translates to a dst.CompositeLit

func Comp ¶

func Comp(typ dst.Expr) CompDSL

Comp creates a new CompDSL

func (CompDSL) Decs ¶

Decs adds decorations to a CompDSL

func (CompDSL) Elts ¶

func (d CompDSL) Elts(elts ...dst.Expr) CompDSL

Elts defines the elements of a CompDSL

type ExprDSL ¶

type ExprDSL struct{ Obj *dst.ExprStmt }

ExprDSL translates to a dst.ExprStmt

func Expr ¶

func Expr(x dst.Expr) ExprDSL

Expr returns a new ExprDSL

func (ExprDSL) Decs ¶

func (d ExprDSL) Decs(decs dst.ExprStmtDecorations) ExprDSL

Decs adds decorations to a ExprDSL

type ExprDecsDSL ¶

type ExprDecsDSL struct{ Obj dst.ExprStmtDecorations }

ExprDecsDSL translates to a dst.ExprStmtDecorations

func ExprDecs ¶

func ExprDecs(after dst.SpaceType) ExprDecsDSL

ExprDecs creates a new ExprDecsDSL

type FieldDSL ¶

type FieldDSL struct{ Obj *dst.Field }

FieldDSL translates to a dst.Field

func Field ¶

func Field(typ dst.Expr) FieldDSL

Field creates a new FieldDSL

func (FieldDSL) Decs ¶

func (d FieldDSL) Decs(decs dst.FieldDecorations) FieldDSL

Decs adds decorations to a FieldDSL

func (FieldDSL) Names ¶

func (d FieldDSL) Names(names ...*dst.Ident) FieldDSL

Names sets the names of a 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 ¶

type FnDSL struct{ Obj *dst.FuncDecl }

FnDSL translates to a dst.GenDecl containing a function type

func Fn ¶

func Fn(name string) FnDSL

Fn creates a new FnDSL

func (FnDSL) Body ¶

func (d FnDSL) Body(list ...dst.Stmt) FnDSL

Body specifies the body for a function

func (FnDSL) Decs ¶

func (d FnDSL) Decs(decs dst.FuncDeclDecorations) FnDSL

Decs adds decorations to a FnDSL

func (FnDSL) ParamList ¶

func (d FnDSL) ParamList(fieldList *dst.FieldList) FnDSL

ParamList specifies a parameter FieldList for a function

func (FnDSL) Params ¶

func (d FnDSL) Params(fields ...*dst.Field) FnDSL

Params specifies parameters for a function

func (FnDSL) Recv ¶

func (d FnDSL) Recv(fields ...*dst.Field) FnDSL

Recv specifies the receiver for a function

func (FnDSL) ResultList ¶

func (d FnDSL) ResultList(fieldList *dst.FieldList) FnDSL

ResultList specifies a result FieldList for a function

func (FnDSL) Results ¶

func (d FnDSL) Results(fields ...*dst.Field) FnDSL

Results specifies results for a function

func (FnDSL) TypeParams ¶ added in v0.27.0

func (d FnDSL) TypeParams(fieldList *dst.FieldList) FnDSL

type FnLitDSL ¶

type FnLitDSL struct{ Obj *dst.FuncLit }

FnLitDSL translates to a dst.FuncLit

func FnLit ¶

func FnLit(typ *dst.FuncType) FnLitDSL

FnLit creates a new FnLitDSL

func (FnLitDSL) Body ¶

func (d FnLitDSL) Body(list ...dst.Stmt) FnLitDSL

Body specifies a body

type FnTypeDSL ¶

type FnTypeDSL struct{ Obj *dst.FuncType }

FnTypeDSL translates to a dst.FuncType

func FnType ¶

func FnType(paramFieldList *dst.FieldList) FnTypeDSL

FnType creates a new FnTypeDSL

func (FnTypeDSL) Results ¶

func (d FnTypeDSL) Results(resultFieldList *dst.FieldList) FnTypeDSL

Results adds a result field list

type ForDSL ¶

type ForDSL struct{ Obj *dst.ForStmt }

ForDSL translates to a dst.ForStmt

func For ¶

func For(init dst.Stmt) ForDSL

For returns a new ForDSL

func (ForDSL) Body ¶

func (d ForDSL) Body(list ...dst.Stmt) ForDSL

Body defines the body of a for statement

func (ForDSL) Cond ¶

func (d ForDSL) Cond(cond dst.Expr) ForDSL

Cond specifies the condition of a for statement

func (ForDSL) Post ¶

func (d ForDSL) Post(post dst.Stmt) ForDSL

Post specifies the post statement of a for statement

type IfDSL ¶

type IfDSL struct{ Obj *dst.IfStmt }

IfDSL translates to a dst.IfStmt

func If ¶

func If(cond dst.Expr) IfDSL

If creates a new If

func (IfDSL) Body ¶

func (d IfDSL) Body(list ...dst.Stmt) IfDSL

Body specifies the body of the If

func (IfDSL) Decs ¶

func (d IfDSL) Decs(decs dst.IfStmtDecorations) IfDSL

Decs adds decorations to a IfDSL

func (IfDSL) Else ¶

func (d IfDSL) Else(els ...dst.Stmt) IfDSL

Else specifies an optional else branch of the If

func (IfDSL) Init ¶ added in v0.28.0

func (d IfDSL) Init(init dst.Stmt) IfDSL

Init specifies the initialization statement of the If

type IfDecsDSL ¶

type IfDecsDSL struct{ Obj dst.IfStmtDecorations }

IfDecsDSL translates to a dst.IfStmtDecorations

func IfDecs ¶

func IfDecs(after dst.SpaceType) IfDecsDSL

IfDecs creates a new IfDecsDSL

type IndexDSL ¶

type IndexDSL struct{ Obj *dst.IndexListExpr }

IndexDSL translates to a dst.IndexListExpr

func Index ¶

func Index(x dst.Expr) IndexDSL

Index creates a new IndexDSL

func (IndexDSL) Sub ¶

func (d IndexDSL) Sub(index ...dst.Expr) IndexDSL

Sub specifies the sub-expression

type KeyValueDSL ¶

type KeyValueDSL struct{ Obj *dst.KeyValueExpr }

KeyValueDSL translates to a dst.KeyValueExpr

func Key ¶

func Key(key dst.Expr) KeyValueDSL

Key creates a new KeyValueDSL

func (KeyValueDSL) Decs ¶

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 ¶

After adds decorations after the KeyValueDSL

type LoadFn ¶

type LoadFn func(cfg *packages.Config, patterns ...string) ([]*packages.Package, error)

LoadFn is the function type of packages.Load

type MapTypeDSL ¶

type MapTypeDSL struct{ Obj *dst.MapType }

MapTypeDSL translates to a dst.MapType

func MapType ¶

func MapType(key dst.Expr) MapTypeDSL

MapType returns a new MapTypeDSL

func (MapTypeDSL) Value ¶

func (d MapTypeDSL) Value(value dst.Expr) MapTypeDSL

Value specifies the value expression of a dst.MapType

type RangeDSL ¶

type RangeDSL struct{ Obj *dst.RangeStmt }

RangeDSL translates to a dst.RangeStmt

func Range ¶

func Range(x dst.Expr) RangeDSL

Range returns a new RangeDSL

func (RangeDSL) Body ¶

func (d RangeDSL) Body(list ...dst.Stmt) RangeDSL

Body defines the body of a range statement

func (RangeDSL) Key ¶

func (d RangeDSL) Key(key dst.Expr) RangeDSL

Key sets the key of a range statement

func (RangeDSL) Tok ¶

func (d RangeDSL) Tok(tok token.Token) RangeDSL

Tok sets the token of a range statement

func (RangeDSL) Value ¶

func (d RangeDSL) Value(value dst.Expr) RangeDSL

Value sets the value of a range statement

type ReadFileFn ¶ added in v0.25.0

type ReadFileFn func(name string) ([]byte, error)

ReadFileFn is the function type of os.ReadFile

type SelDSL ¶

type SelDSL struct{ Obj *dst.SelectorExpr }

SelDSL translates to a dst.SelectorExpr

func Sel ¶

func Sel(x dst.Expr) SelDSL

Sel creates a new SelDSL

func (SelDSL) Decs ¶ added in v0.28.0

Decs add decorations to a SelDSL

func (SelDSL) Dot ¶

func (d SelDSL) Dot(sel *dst.Ident) SelDSL

Dot specifies what is selected

type SliceExprDSL ¶

type SliceExprDSL struct{ Obj *dst.SliceExpr }

SliceExprDSL translates to a dst.SliceExpr

func SliceExpr ¶

func SliceExpr(x dst.Expr) SliceExprDSL

SliceExpr creates a new slice expression

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 StarDSL ¶ added in v0.28.0

type StarDSL struct{ Obj *dst.StarExpr }

StarDSL translates to a dst.StarExpr

func Star ¶

func Star(x dst.Expr) StarDSL

Star returns a new star expression

func (StarDSL) Decs ¶ added in v0.28.0

func (d StarDSL) Decs(decs dst.StarExprDecorations) StarDSL

Decs adds decorations to a StarDSL

type StatFn ¶ added in v0.25.0

type StatFn func(name string) (os.FileInfo, error)

StatFn is the function type of os.Stat

type TypeDeclDSL ¶

type TypeDeclDSL struct{ Obj *dst.GenDecl }

TypeDeclDSL translates various types into a dst.GenDecl

func TypeDecl ¶

func TypeDecl(typeSpec *dst.TypeSpec) TypeDeclDSL

TypeDecl creates a new TypeDeclDSL

func (TypeDeclDSL) Decs ¶

Decs adds decorations to a TypeDeclDSL

type TypeInfo ¶

type TypeInfo struct {
	Type       *dst.TypeSpec
	PkgPath    string
	Exported   bool
	Fabricated bool
}

TypeInfo returns all the information the cache holds for a type

type TypeSpecDSL ¶

type TypeSpecDSL struct{ Obj *dst.TypeSpec }

TypeSpecDSL translates to a dst.TypeSpec

func TypeSpec ¶

func TypeSpec(name string) TypeSpecDSL

TypeSpec creates a new TypeSpecDSL

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 ¶

type ValueDSL struct{ Obj *dst.ValueSpec }

ValueDSL translates to a dst.ValueSpec

func Value ¶

func Value(typ dst.Expr) ValueDSL

Value creates a new ValueDSL

func (ValueDSL) Names ¶

func (d ValueDSL) Names(names ...*dst.Ident) ValueDSL

Names sets the names of a value

func (ValueDSL) Values ¶

func (d ValueDSL) Values(values ...dst.Expr) ValueDSL

Values sets the values of the spec

type VarDeclDSL ¶

type VarDeclDSL struct{ Obj *dst.GenDecl }

VarDeclDSL translates variable declaration into a dst.GenDecl

func VarDecl ¶

func VarDecl(specs ...dst.Spec) VarDeclDSL

VarDecl creates a new VarDeclDSL

func (VarDeclDSL) Decs ¶

Decs adds decorations to a VarDeclDSL

Directories ¶

Path Synopsis
testpkgs

Jump to

Keyboard shortcuts

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