astcast: github.com/go-toolsmith/astcast Index | Examples | Files

package astcast

import "github.com/go-toolsmith/astcast"

Package astcast wraps type assertion operations in such way that you don't have to worry about nil pointer results anymore.

Code:

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

Examples

Package Files

astcast.go

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.

func ToArrayType Uses

func ToArrayType(x ast.Node) *ast.ArrayType

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 Uses

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 Uses

func ToBadExpr(x ast.Node) *ast.BadExpr

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 Uses

func ToBadStmt(x ast.Node) *ast.BadStmt

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 Uses

func ToBasicLit(x ast.Node) *ast.BasicLit

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 Uses

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 Uses

func ToBlockStmt(x ast.Node) *ast.BlockStmt

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 Uses

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 Uses

func ToCallExpr(x ast.Node) *ast.CallExpr

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 Uses

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 Uses

func ToChanType(x ast.Node) *ast.ChanType

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 Uses

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 Uses

func ToComment(x ast.Node) *ast.Comment

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 Uses

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 Uses

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 Uses

func ToDeclStmt(x ast.Node) *ast.DeclStmt

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 Uses

func ToDeferStmt(x ast.Node) *ast.DeferStmt

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 Uses

func ToEllipsis(x ast.Node) *ast.Ellipsis

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 Uses

func ToEmptyStmt(x ast.Node) *ast.EmptyStmt

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 Uses

func ToExprStmt(x ast.Node) *ast.ExprStmt

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 Uses

func ToFieldList(x ast.Node) *ast.FieldList

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 Uses

func ToFile(x ast.Node) *ast.File

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 Uses

func ToForStmt(x ast.Node) *ast.ForStmt

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 Uses

func ToFuncLit(x ast.Node) *ast.FuncLit

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 Uses

func ToFuncType(x ast.Node) *ast.FuncType

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 Uses

func ToGoStmt(x ast.Node) *ast.GoStmt

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 Uses

func ToIdent(x ast.Node) *ast.Ident

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 Uses

func ToIfStmt(x ast.Node) *ast.IfStmt

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 Uses

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 Uses

func ToIndexExpr(x ast.Node) *ast.IndexExpr

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 Uses

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 Uses

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 Uses

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 Uses

func ToMapType(x ast.Node) *ast.MapType

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 Uses

func ToPackage(x ast.Node) *ast.Package

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 Uses

func ToParenExpr(x ast.Node) *ast.ParenExpr

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 Uses

func ToRangeStmt(x ast.Node) *ast.RangeStmt

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 Uses

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 Uses

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 Uses

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 Uses

func ToSendStmt(x ast.Node) *ast.SendStmt

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 Uses

func ToSliceExpr(x ast.Node) *ast.SliceExpr

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 Uses

func ToStarExpr(x ast.Node) *ast.StarExpr

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 Uses

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 Uses

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 Uses

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 Uses

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.

func ToUnaryExpr Uses

func ToUnaryExpr(x ast.Node) *ast.UnaryExpr

ToUnaryExpr returns x as a non-nil *ast.UnaryExpr. 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 NilUnaryExpr.

Package astcast imports 1 packages (graph) and is imported by 2 packages. Updated 2019-01-17. Refresh now. Tools for package owners.