astcast

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: MIT Imports: 1 Imported by: 8

README

astcast

build-img pkg-img reportcard-img version-img

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

Installation

Go version 1.16+

go get github.com/go-toolsmith/astcast

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")
	}
}

Without astcast, you would have to do a lots of type assertions:

package main

import (
	"fmt"

	"github.com/go-toolsmith/strparse"
)

func main() {
	x := strparse.Expr(`(foo * bar) + 1`)

	add, ok := x.(*ast.BinaryExpr)
	if !ok || add == nil {
		return
	}
	additionLHS, ok := add.X.(*ast.ParenExpr)
	if !ok || additionLHS == nil {
		return
	}
	mul, ok := additionLHS.X.(*ast.BinaryExpr)
	if !ok || mul == nil {
		return
	}
	bar, ok := mul.Y.(*ast.Ident)
	if !ok || bar == nil {
		return
	}
	fmt.Printf("%T %s\n", bar, bar.Name)
}

License

MIT License.

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

Examples

Constants

This section is empty.

Variables

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

func ToUnaryExpr

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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