astequal

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 5 Imported by: 15 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

astequal

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

Package astequal provides AST (deep) equallity check operations.

Installation:

Go version 1.16+

go get github.com/go-toolsmith/astequal

Example

package main

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"log"
	"reflect"

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

func main() {
	const code = `
		package foo

		func main() {
			x := []int{1, 2, 3}
			x := []int{1, 2, 3}
		}`

	fset := token.NewFileSet()
	pkg, err := parser.ParseFile(fset, "string", code, 0)
	if err != nil {
		log.Fatalf("parse error: %+v", err)
	}

	fn := pkg.Decls[0].(*ast.FuncDecl)
	x := fn.Body.List[0]
	y := fn.Body.List[1]

	// Reflect DeepEqual will fail due to different Pos values.
	// astequal only checks whether two nodes describe AST.
	fmt.Println(reflect.DeepEqual(x, y)) // => false
	fmt.Println(astequal.Node(x, y))     // => true
	fmt.Println(astequal.Stmt(x, y))     // => true
}

Performance

astequal outperforms reflection-based comparison by a big margin:

BenchmarkEqualExpr/astequal.Expr-8       5000000     298 ns/op       0 B/op   0 allocs/op
BenchmarkEqualExpr/astequal.Node-8       3000000     409 ns/op       0 B/op   0 allocs/op
BenchmarkEqualExpr/reflect.DeepEqual-8     50000   38898 ns/op   10185 B/op   156 allocs/op

License

MIT License.

Documentation

Overview

Package astequal provides AST (deep) equallity check operations.

Example
package main

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"log"
	"reflect"

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

func main() {
	const code = `
		package foo

		func main() {
			x := []int{1, 2, 3}
			x := []int{1, 2, 3}
		}`

	fset := token.NewFileSet()
	pkg, err := parser.ParseFile(fset, "string", code, 0)
	if err != nil {
		log.Fatalf("parse error: %+v", err)
	}

	fn := pkg.Decls[0].(*ast.FuncDecl)
	x := fn.Body.List[0]
	y := fn.Body.List[1]

	// Reflect DeepEqual will fail due to different Pos values.
	// astequal only checks whether two nodes describe AST.
	fmt.Println(reflect.DeepEqual(x, y)) // => false
	fmt.Println(astequal.Node(x, y))     // => true
	fmt.Println(astequal.Stmt(x, y))     // => true

}
Output:
false
true
true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decl

func Decl(x, y ast.Decl) bool

Decl reports whether two AST declarations are structurally (deep) equal.

Nil arguments are permitted: true is returned if x and y are both nils. ast.BadDecl comparison always yields false.

func Diff added in v1.2.0

func Diff(x, y ast.Node) string

func Expr

func Expr(x, y ast.Expr) bool

Expr reports whether two AST expressions are structurally (deep) equal.

Nil arguments are permitted: true is returned if x and y are both nils. ast.BadExpr comparison always yields false.

func Node

func Node(x, y ast.Node) bool

Node reports whether two AST nodes are structurally (deep) equal.

Nil arguments are permitted: true is returned if x and y are both nils.

See also: Expr, Stmt, Decl functions.

func Stmt

func Stmt(x, y ast.Stmt) bool

Stmt reports whether two AST statements are structurally (deep) equal.

Nil arguments are permitted: true is returned if x and y are both nils. ast.BadStmt comparison always yields false.

Types

This section is empty.

Jump to

Keyboard shortcuts

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