Categorygithub.com/go-toolsmith/astcopy
modulepackage
1.1.0
Repository: https://github.com/go-toolsmith/astcopy.git
Documentation: pkg.go.dev

# README

astcopy

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

Package astcopy implements Go AST reflection-free deep copy operations.

Installation:

Go version 1.16+

go get github.com/go-toolsmith/astcopy

Example

package main

import (
	"fmt"
	"go/ast"
	"go/token"

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

func main() {
	x := strparse.Expr(`1 + 2`).(*ast.BinaryExpr)
	y := astcopy.BinaryExpr(x)
	fmt.Println(astequal.Expr(x, y)) // => true

	// Now modify x and make sure y is not modified.
	z := astcopy.BinaryExpr(y)
	x.Op = token.SUB
	fmt.Println(astequal.Expr(y, z)) // => true
	fmt.Println(astequal.Expr(x, y)) // => false
}

License

MIT License.

# Functions

ArrayType returns x deep copy.
AssignStmt returns x deep copy.
BadDecl returns x deep copy.
BadExpr returns x deep copy.
BadStmt returns x deep copy.
BasicLit returns x deep copy.
BinaryExpr returns x deep copy.
BlockStmt returns x deep copy.
BranchStmt returns x deep copy.
CallExpr returns x deep copy.
CaseClause returns x deep copy.
ChanType returns x deep copy.
CommClause returns x deep copy.
Comment returns x deep copy.
CommentGroup returns x deep copy.
CompositeLit returns x deep copy.
Decl returns x declaration deep copy.
DeclList returns xs declaration slice deep copy.
DeclStmt returns x deep copy.
DeferStmt returns x deep copy.
Ellipsis returns x deep copy.
EmptyStmt returns x deep copy.
Expr returns x expression deep copy.
ExprList returns xs expression slice deep copy.
ExprStmt returns x deep copy.
Field returns x deep copy.
FieldList returns x deep copy.
File returns x deep copy.
ForStmt returns x deep copy.
FuncDecl returns x deep copy.
FuncLit returns x deep copy.
FuncType returns x deep copy.
GenDecl returns x deep copy.
GoStmt returns x deep copy.
Ident returns x deep copy.
IdentList returns xs identifier slice deep copy.
IfStmt returns x deep copy.
ImportSpec returns x deep copy.
IncDecStmt returns x deep copy.
IndexExpr returns x deep copy.
IndexListExpr returns x deep copy.
InterfaceType returns x deep copy.
KeyValueExpr returns x deep copy.
LabeledStmt returns x deep copy.
MapType returns x deep copy.
Node returns x node deep copy.
NodeList returns xs node slice deep copy.
Package returns x deep copy.
ParenExpr returns x deep copy.
RangeStmt returns x deep copy.
ReturnStmt returns x deep copy.
SelectorExpr returns x deep copy.
SelectStmt returns x deep copy.
SendStmt returns x deep copy.
SliceExpr returns x deep copy.
Spec returns x deep copy.
SpecList returns xs spec slice deep copy.
StarExpr returns x deep copy.
Stmt returns x statement deep copy.
StmtList returns xs statement slice deep copy.
StructType returns x deep copy.
SwitchStmt returns x deep copy.
TypeAssertExpr returns x deep copy.
TypeSpec returns x deep copy.
TypeSwitchStmt returns x deep copy.
UnaryExpr returns x deep copy.
ValueSpec returns x deep copy.