Skip to content

Commit 6da3587

Browse files
authored
add generics support (#4)
Type params are accessed via `typeparams` package, so `astcopy` should continue to work for older Go releases.
1 parent 245af30 commit 6da3587

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

astcopy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package astcopy
33

44
import (
55
"go/ast"
6+
7+
"golang.org/x/exp/typeparams"
68
)
79

810
// Node returns x node deep copy.
@@ -341,6 +343,9 @@ func FuncType(x *ast.FuncType) *ast.FuncType {
341343
cp := *x
342344
cp.Params = FieldList(x.Params)
343345
cp.Results = FieldList(x.Results)
346+
if typeParams := typeparams.ForFuncType(x); typeParams != nil {
347+
*typeparams.ForFuncType(&cp) = *FieldList(typeParams)
348+
}
344349
return &cp
345350
}
346351

@@ -429,6 +434,9 @@ func TypeSpec(x *ast.TypeSpec) *ast.TypeSpec {
429434
cp.Type = copyExpr(x.Type)
430435
cp.Doc = CommentGroup(x.Doc)
431436
cp.Comment = CommentGroup(x.Comment)
437+
if typeParams := typeparams.ForTypeSpec(x); typeParams != nil {
438+
*typeparams.ForTypeSpec(&cp) = *FieldList(typeParams)
439+
}
432440
return &cp
433441
}
434442

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module github.com/go-toolsmith/astcopy
22

3+
go 1.16
4+
35
require (
4-
github.com/go-toolsmith/astequal v1.0.0
6+
github.com/go-toolsmith/astequal v1.0.2
57
github.com/go-toolsmith/strparse v1.0.0
8+
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171
69
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
github.com/go-toolsmith/astequal v1.0.0 h1:4zxD8j3JRFNyLN46lodQuqz3xdKSrur7U/sr0SDS/gQ=
2-
github.com/go-toolsmith/astequal v1.0.0/go.mod h1:H+xSiq0+LtiDC11+h1G32h7Of5O3CYFJ99GVbS5lDKY=
1+
github.com/go-toolsmith/astequal v1.0.2 h1:+XvaV8zNxua+9+Oa4AHmgmpo4RYAbwr/qjNppLfX2yM=
2+
github.com/go-toolsmith/astequal v1.0.2/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/OXVTSLnCyTAx4=
33
github.com/go-toolsmith/strparse v1.0.0 h1:Vcw78DnpCAKlM20kSbAyO4mPfJn/lyYA4BJUDxe2Jb4=
44
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
5+
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171 h1:DZhP7zSquENyG3Yb6ZpGqNEtgE8dfXhcLcheIF9RQHY=
6+
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=

0 commit comments

Comments
 (0)