Documentation ¶
Overview ¶
Package astcast wraps type assertion operations in such way that you don't have to worry about nil pointer results anymore.
Example ¶
package main import ( "fmt" "github.com/go-toolsmith/astcast" "github.com/go-toolsmith/strparse" ) func main() { x := strparse.Expr(`(foo * bar) + 1`) // x type is ast.Expr, we want to access bar operand // that is a RHS of the LHS of the addition. // Note that addition LHS (X field) is has parenthesis, // so we have to remove them too. add := astcast.ToBinaryExpr(x) mul := astcast.ToBinaryExpr(astcast.ToParenExpr(add.X).X) bar := astcast.ToIdent(mul.Y) fmt.Printf("%T %s\n", bar, bar.Name) // => *ast.Ident bar // If argument has different dynamic type, // non-nil sentinel object of requested type is returned. // Those sentinel objects are exported so if you need // to know whether it was a nil interface value of // failed type assertion, you can compare returned // object with such a sentinel. y := astcast.ToCallExpr(strparse.Expr(`x`)) if y == astcast.NilCallExpr { fmt.Println("it is a sentinel, type assertion failed") } }
Output: *ast.Ident bar it is a sentinel, type assertion failed
Index ¶
- Variables
- func ToArrayType(x ast.Node) *ast.ArrayType
- func ToAssignStmt(x ast.Node) *ast.AssignStmt
- func ToBadExpr(x ast.Node) *ast.BadExpr
- func ToBadStmt(x ast.Node) *ast.BadStmt
- func ToBasicLit(x ast.Node) *ast.BasicLit
- func ToBinaryExpr(x ast.Node) *ast.BinaryExpr
- func ToBlockStmt(x ast.Node) *ast.BlockStmt
- func ToBranchStmt(x ast.Node) *ast.BranchStmt
- func ToCallExpr(x ast.Node) *ast.CallExpr
- func ToCaseClause(x ast.Node) *ast.CaseClause
- func ToChanType(x ast.Node) *ast.ChanType
- func ToCommClause(x ast.Node) *ast.CommClause
- func ToComment(x ast.Node) *ast.Comment
- func ToCommentGroup(x ast.Node) *ast.CommentGroup
- func ToCompositeLit(x ast.Node) *ast.CompositeLit
- func ToDeclStmt(x ast.Node) *ast.DeclStmt
- func ToDeferStmt(x ast.Node) *ast.DeferStmt
- func ToEllipsis(x ast.Node) *ast.Ellipsis
- func ToEmptyStmt(x ast.Node) *ast.EmptyStmt
- func ToExprStmt(x ast.Node) *ast.ExprStmt
- func ToFieldList(x ast.Node) *ast.FieldList
- func ToFile(x ast.Node) *ast.File
- func ToForStmt(x ast.Node) *ast.ForStmt
- func ToFuncLit(x ast.Node) *ast.FuncLit
- func ToFuncType(x ast.Node) *ast.FuncType
- func ToGoStmt(x ast.Node) *ast.GoStmt
- func ToIdent(x ast.Node) *ast.Ident
- func ToIfStmt(x ast.Node) *ast.IfStmt
- func ToIncDecStmt(x ast.Node) *ast.IncDecStmt
- func ToIndexExpr(x ast.Node) *ast.IndexExpr
- func ToInterfaceType(x ast.Node) *ast.InterfaceType
- func ToKeyValueExpr(x ast.Node) *ast.KeyValueExpr
- func ToLabeledStmt(x ast.Node) *ast.LabeledStmt
- func ToMapType(x ast.Node) *ast.MapType
- func ToPackage(x ast.Node) *ast.Package
- func ToParenExpr(x ast.Node) *ast.ParenExpr
- func ToRangeStmt(x ast.Node) *ast.RangeStmt
- func ToReturnStmt(x ast.Node) *ast.ReturnStmt
- func ToSelectStmt(x ast.Node) *ast.SelectStmt
- func ToSelectorExpr(x ast.Node) *ast.SelectorExpr
- func ToSendStmt(x ast.Node) *ast.SendStmt
- func ToSliceExpr(x ast.Node) *ast.SliceExpr
- func ToStarExpr(x ast.Node) *ast.StarExpr
- func ToStructType(x ast.Node) *ast.StructType
- func ToSwitchStmt(x ast.Node) *ast.SwitchStmt
- func ToTypeAssertExpr(x ast.Node) *ast.TypeAssertExpr
- func ToTypeSwitchStmt(x ast.Node) *ast.TypeSwitchStmt
- func ToUnaryExpr(x ast.Node) *ast.UnaryExpr
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( NilArrayType = &ast.ArrayType{} NilBadExpr = &ast.BadExpr{} NilBasicLit = &ast.BasicLit{} NilBinaryExpr = &ast.BinaryExpr{} NilCallExpr = &ast.CallExpr{} NilChanType = &ast.ChanType{} NilCompositeLit = &ast.CompositeLit{} NilEllipsis = &ast.Ellipsis{} NilFuncLit = &ast.FuncLit{} NilFuncType = &ast.FuncType{} NilIdent = &ast.Ident{} NilIndexExpr = &ast.IndexExpr{} NilInterfaceType = &ast.InterfaceType{} NilKeyValueExpr = &ast.KeyValueExpr{} NilMapType = &ast.MapType{} NilParenExpr = &ast.ParenExpr{} NilSelectorExpr = &ast.SelectorExpr{} NilSliceExpr = &ast.SliceExpr{} NilStarExpr = &ast.StarExpr{} NilStructType = &ast.StructType{} NilTypeAssertExpr = &ast.TypeAssertExpr{} NilUnaryExpr = &ast.UnaryExpr{} NilAssignStmt = &ast.AssignStmt{} NilBadStmt = &ast.BadStmt{} NilBlockStmt = &ast.BlockStmt{} NilBranchStmt = &ast.BranchStmt{} NilCaseClause = &ast.CaseClause{} NilCommClause = &ast.CommClause{} NilDeclStmt = &ast.DeclStmt{} NilDeferStmt = &ast.DeferStmt{} NilEmptyStmt = &ast.EmptyStmt{} NilExprStmt = &ast.ExprStmt{} NilForStmt = &ast.ForStmt{} NilGoStmt = &ast.GoStmt{} NilIfStmt = &ast.IfStmt{} NilIncDecStmt = &ast.IncDecStmt{} NilLabeledStmt = &ast.LabeledStmt{} NilRangeStmt = &ast.RangeStmt{} NilReturnStmt = &ast.ReturnStmt{} NilSelectStmt = &ast.SelectStmt{} NilSendStmt = &ast.SendStmt{} NilSwitchStmt = &ast.SwitchStmt{} NilTypeSwitchStmt = &ast.TypeSwitchStmt{} NilComment = &ast.Comment{} NilCommentGroup = &ast.CommentGroup{} NilFieldList = &ast.FieldList{} NilFile = &ast.File{} NilPackage = &ast.Package{} )
A set of sentinel nil-like values that are returned by all "casting" functions in case of failed type assertion.
Functions ¶
func ToArrayType ¶
ToArrayType returns x as a non-nil *ast.ArrayType. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilArrayType.
func ToAssignStmt ¶
func ToAssignStmt(x ast.Node) *ast.AssignStmt
ToAssignStmt returns x as a non-nil *ast.AssignStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilAssignStmt.
func ToBadExpr ¶
ToBadExpr returns x as a non-nil *ast.BadExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilBadExpr.
func ToBadStmt ¶
ToBadStmt returns x as a non-nil *ast.BadStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilBadStmt.
func ToBasicLit ¶
ToBasicLit returns x as a non-nil *ast.BasicLit. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilBasicLit.
func ToBinaryExpr ¶
func ToBinaryExpr(x ast.Node) *ast.BinaryExpr
ToBinaryExpr returns x as a non-nil *ast.BinaryExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilBinaryExpr.
func ToBlockStmt ¶
ToBlockStmt returns x as a non-nil *ast.BlockStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilBlockStmt.
func ToBranchStmt ¶
func ToBranchStmt(x ast.Node) *ast.BranchStmt
ToBranchStmt returns x as a non-nil *ast.BranchStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilBranchStmt.
func ToCallExpr ¶
ToCallExpr returns x as a non-nil *ast.CallExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilCallExpr.
func ToCaseClause ¶
func ToCaseClause(x ast.Node) *ast.CaseClause
ToCaseClause returns x as a non-nil *ast.CaseClause. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilCaseClause.
func ToChanType ¶
ToChanType returns x as a non-nil *ast.ChanType. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilChanType.
func ToCommClause ¶
func ToCommClause(x ast.Node) *ast.CommClause
ToCommClause returns x as a non-nil *ast.CommClause. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilCommClause.
func ToComment ¶
ToComment returns x as a non-nil *ast.Comment. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilComment.
func ToCommentGroup ¶
func ToCommentGroup(x ast.Node) *ast.CommentGroup
ToCommentGroup returns x as a non-nil *ast.CommentGroup. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilCommentGroup.
func ToCompositeLit ¶
func ToCompositeLit(x ast.Node) *ast.CompositeLit
ToCompositeLit returns x as a non-nil *ast.CompositeLit. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilCompositeLit.
func ToDeclStmt ¶
ToDeclStmt returns x as a non-nil *ast.DeclStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilDeclStmt.
func ToDeferStmt ¶
ToDeferStmt returns x as a non-nil *ast.DeferStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilDeferStmt.
func ToEllipsis ¶
ToEllipsis returns x as a non-nil *ast.Ellipsis. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilEllipsis.
func ToEmptyStmt ¶
ToEmptyStmt returns x as a non-nil *ast.EmptyStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilEmptyStmt.
func ToExprStmt ¶
ToExprStmt returns x as a non-nil *ast.ExprStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilExprStmt.
func ToFieldList ¶
ToFieldList returns x as a non-nil *ast.FieldList. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilFieldList.
func ToFile ¶
ToFile returns x as a non-nil *ast.File. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilFile.
func ToForStmt ¶
ToForStmt returns x as a non-nil *ast.ForStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilForStmt.
func ToFuncLit ¶
ToFuncLit returns x as a non-nil *ast.FuncLit. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilFuncLit.
func ToFuncType ¶
ToFuncType returns x as a non-nil *ast.FuncType. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilFuncType.
func ToGoStmt ¶
ToGoStmt returns x as a non-nil *ast.GoStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilGoStmt.
func ToIdent ¶
ToIdent returns x as a non-nil *ast.Ident. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilIdent.
func ToIfStmt ¶
ToIfStmt returns x as a non-nil *ast.IfStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilIfStmt.
func ToIncDecStmt ¶
func ToIncDecStmt(x ast.Node) *ast.IncDecStmt
ToIncDecStmt returns x as a non-nil *ast.IncDecStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilIncDecStmt.
func ToIndexExpr ¶
ToIndexExpr returns x as a non-nil *ast.IndexExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilIndexExpr.
func ToInterfaceType ¶
func ToInterfaceType(x ast.Node) *ast.InterfaceType
ToInterfaceType returns x as a non-nil *ast.InterfaceType. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilInterfaceType.
func ToKeyValueExpr ¶
func ToKeyValueExpr(x ast.Node) *ast.KeyValueExpr
ToKeyValueExpr returns x as a non-nil *ast.KeyValueExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilKeyValueExpr.
func ToLabeledStmt ¶
func ToLabeledStmt(x ast.Node) *ast.LabeledStmt
ToLabeledStmt returns x as a non-nil *ast.LabeledStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilLabeledStmt.
func ToMapType ¶
ToMapType returns x as a non-nil *ast.MapType. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilMapType.
func ToPackage ¶
ToPackage returns x as a non-nil *ast.Package. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilPackage.
func ToParenExpr ¶
ToParenExpr returns x as a non-nil *ast.ParenExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilParenExpr.
func ToRangeStmt ¶
ToRangeStmt returns x as a non-nil *ast.RangeStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilRangeStmt.
func ToReturnStmt ¶
func ToReturnStmt(x ast.Node) *ast.ReturnStmt
ToReturnStmt returns x as a non-nil *ast.ReturnStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilReturnStmt.
func ToSelectStmt ¶
func ToSelectStmt(x ast.Node) *ast.SelectStmt
ToSelectStmt returns x as a non-nil *ast.SelectStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilSelectStmt.
func ToSelectorExpr ¶
func ToSelectorExpr(x ast.Node) *ast.SelectorExpr
ToSelectorExpr returns x as a non-nil *ast.SelectorExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilSelectorExpr.
func ToSendStmt ¶
ToSendStmt returns x as a non-nil *ast.SendStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilSendStmt.
func ToSliceExpr ¶
ToSliceExpr returns x as a non-nil *ast.SliceExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilSliceExpr.
func ToStarExpr ¶
ToStarExpr returns x as a non-nil *ast.StarExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilStarExpr.
func ToStructType ¶
func ToStructType(x ast.Node) *ast.StructType
ToStructType returns x as a non-nil *ast.StructType. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilStructType.
func ToSwitchStmt ¶
func ToSwitchStmt(x ast.Node) *ast.SwitchStmt
ToSwitchStmt returns x as a non-nil *ast.SwitchStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilSwitchStmt.
func ToTypeAssertExpr ¶
func ToTypeAssertExpr(x ast.Node) *ast.TypeAssertExpr
ToTypeAssertExpr returns x as a non-nil *ast.TypeAssertExpr. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilTypeAssertExpr.
func ToTypeSwitchStmt ¶
func ToTypeSwitchStmt(x ast.Node) *ast.TypeSwitchStmt
ToTypeSwitchStmt returns x as a non-nil *ast.TypeSwitchStmt. If ast.Node actually has such dynamic type, the result is identical to normal type assertion. In case if it has different type, the returned value is NilTypeSwitchStmt.
Types ¶
This section is empty.