/
interface.go
145 lines (120 loc) · 3.09 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package parser
import (
"go/ast"
"log"
"github.com/lawrsp/pigo/pkg/printutil"
)
type OldFuncType struct {
Expr ast.Expr
Package *Package
Name *ast.Ident
OldFuncType *ast.FuncType
Signature string
}
func NewOldFuncType(name string) *OldFuncType {
if expr, err := ParseExpr(name); err == nil {
return &OldFuncType{Expr: expr}
}
log.Fatalf("func type name error:%s", name)
return nil
}
func (ft *OldFuncType) Fill(node *DeclNode) {
decl, ok := node.Node.(*ast.FuncDecl)
if !ok {
printutil.FatalNodef(node, "cannot fill OldFuncType:")
}
ft.Name = decl.Name
if ft.Name == nil {
ft.Name = node.Name
}
ft.Signature = ""
ft.OldFuncType = decl.Type
}
func (ft *OldFuncType) copyFieldListToPackage(pkg *Package, src *ast.FieldList) *ast.FieldList {
if src == nil {
return nil
}
dstList := make([]*ast.Field, len(src.List))
for i, field := range src.List {
// Names []*Ident //copy
// Type Expr //direct assgin
var names []*ast.Ident
for _, fdId := range field.Names {
ident := &ast.Ident{}
*ident = *fdId
names = append(names, ident)
}
dstList[i] = &ast.Field{
Names: names,
Type: field.Type,
}
}
return &ast.FieldList{List: dstList}
}
func (ft *OldFuncType) CopyAstOldFuncType() *ast.FuncType {
nft := &ast.FuncType{}
nft.Params = ft.copyFieldListToPackage(nil, ft.OldFuncType.Params)
nft.Results = ft.copyFieldListToPackage(nil, ft.OldFuncType.Results)
return nft
}
func (ft *OldFuncType) CopyAstOldFuncTypeToPackage(pkg *Package) *ast.FuncType {
nft := &ast.FuncType{}
nft.Params = ft.copyFieldListToPackage(pkg, ft.OldFuncType.Params)
nft.Results = ft.copyFieldListToPackage(pkg, ft.OldFuncType.Results)
return nft
}
type OldInterface struct {
OldFuncTypes []*OldFuncType
}
func NewOldInterface() *OldInterface {
return &OldInterface{}
}
func (i *OldInterface) GetFuncByName(name string) *OldFuncType {
for _, ft := range i.OldFuncTypes {
if ft.Name.Name == name {
return ft
}
}
return nil
}
func (i *OldInterface) parseType(pkg *Package, name *ast.Ident, typ interface{}) []*OldFuncType {
switch t := typ.(type) {
case *ast.FuncType:
return []*OldFuncType{
&OldFuncType{
Package: pkg,
Name: name,
OldFuncType: t,
Signature: "",
},
}
case *ast.TypeSpec:
return i.parseType(pkg, t.Name, t.Type)
case *ast.InterfaceType:
return i.parseFieldList(pkg, t.Methods)
}
return nil
}
func (i *OldInterface) parseField(pkg *Package, field *ast.Field) []*OldFuncType {
switch t := field.Type.(type) {
case *ast.Ident:
return i.parseType(pkg, t, t.Obj.Decl)
case *ast.FuncType:
return i.parseType(pkg, field.Names[0], t)
}
return nil
}
func (i *OldInterface) parseFieldList(pkg *Package, list *ast.FieldList) []*OldFuncType {
fnctyps := []*OldFuncType{}
for _, field := range list.List {
fnctyps = append(fnctyps, i.parseField(pkg, field)...)
}
return fnctyps
}
func (i *OldInterface) Fill(node *DeclNode) {
itf, ok := node.Node.(*ast.InterfaceType)
if !ok {
printutil.FatalNodef(node.Node, "type Error:")
}
i.OldFuncTypes = i.parseFieldList(node.File.BelongTo, itf.Methods)
}