Skip to content

Commit

Permalink
Add Diff func (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jan 2, 2024
1 parent 8dece9b commit 84b0b16
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package astequal

import (
"bytes"
"go/ast"
"go/format"
"go/token"

"github.com/google/go-cmp/cmp"
)

func Diff(x, y ast.Node) string {
var buf bytes.Buffer
format.Node(&buf, token.NewFileSet(), x)
s1 := buf.String()

buf.Reset()
format.Node(&buf, token.NewFileSet(), y)
s2 := buf.String()

// TODO(cristaloleg): replace with a more lightweight diff impl.
return cmp.Diff(s1, s2)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.16

require (
github.com/go-toolsmith/strparse v1.1.0
github.com/google/go-cmp v0.6.0
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/go-toolsmith/astequal v1.0.3/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
github.com/go-toolsmith/strparse v1.1.0 h1:GAioeZUK9TGxnLS+qfdqNbA4z0SSm5zVNtCQiyP2Bvw=
github.com/go-toolsmith/strparse v1.1.0/go.mod h1:7ksGy58fsaQkGQlY8WVoBFNyEPMGuJin1rfoPS4lBSQ=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9 h1:6WHiuFL9FNjg8RljAaT7FNUuKDbvMqS1i5cr2OE2sLQ=
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=

0 comments on commit 84b0b16

Please sign in to comment.