-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunc.go
More file actions
164 lines (143 loc) · 3.83 KB
/
Copy pathfunc.go
File metadata and controls
164 lines (143 loc) · 3.83 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package aster
import (
"fmt"
"go/ast"
"strings"
)
type FuncType struct {
Name string `json:",omitempty"`
Params []*FieldType `json:",omitempty"`
Results []*FieldType `json:",omitempty"`
astBolckStmt *ast.BlockStmt
}
func NewFuncTypeByASTField(astField *ast.Field, astFuncType *ast.FuncType) (*FuncType, error) {
funcType := &FuncType{}
if len(astField.Names) > 0 {
funcType.Name = astField.Names[0].Name
}
if astFuncType.Params != nil {
funcType.Params = make([]*FieldType, 0, astFuncType.Params.NumFields())
for _, astParamField := range astFuncType.Params.List {
err := funcType.ParseParam(astParamField)
if err != nil {
return nil, err
}
}
}
if astFuncType.Results != nil {
funcType.Results = make([]*FieldType, 0, astFuncType.Results.NumFields())
for _, astResultField := range astFuncType.Results.List {
err := funcType.ParseResult(astResultField)
if err != nil {
return nil, err
}
}
}
return funcType, nil
}
func NewFuncTypeByASTDecl(astDecl *ast.FuncDecl) (*FuncType, error) {
funcType := &FuncType{}
astFuncType := astDecl.Type
if astDecl.Name != nil {
funcType.Name = astDecl.Name.Name
}
if astFuncType.Params != nil {
funcType.Params = make([]*FieldType, 0, astFuncType.Params.NumFields())
for _, astParamField := range astFuncType.Params.List {
err := funcType.ParseParam(astParamField)
if err != nil {
return nil, err
}
}
}
if astFuncType.Results != nil {
funcType.Results = make([]*FieldType, 0, astFuncType.Results.NumFields())
for _, astResultField := range astFuncType.Results.List {
err := funcType.ParseResult(astResultField)
if err != nil {
return nil, err
}
}
}
funcType.astBolckStmt = astDecl.Body
return funcType, nil
}
func (this *FuncType) ParseParam(astParamField *ast.Field) error {
fieldTypes, err := NewFieldTypes(astParamField)
if err == nil {
this.Params = append(this.Params, fieldTypes...)
}
return err
}
func (this *FuncType) ParseResult(astResultField *ast.Field) error {
fieldTypes, err := NewFieldTypes(astResultField)
if err == nil {
this.Results = append(this.Results, fieldTypes...)
}
return err
}
// 不是所有FuncType都有body,可能只是个声明。
func (this *FuncType) GetASTBlockSTMT() *ast.BlockStmt {
return this.astBolckStmt
}
func (this *FuncType) String() string {
sb := strings.Builder{}
sb.WriteString("func " + this.Name + "(")
for i, paramType := range this.Params {
sb.WriteString(paramType.GetDecl())
if i < len(this.Params)-1 {
sb.WriteString(", ")
}
}
sb.WriteString(") ")
if len(this.Results) > 1 {
sb.WriteString("(")
}
for i, resultType := range this.Results {
sb.WriteString(resultType.GetDecl())
if i < len(this.Results)-1 {
sb.WriteString(", ")
}
}
if len(this.Results) > 1 {
sb.WriteString(")")
}
sb.WriteString(" {\n\t// ...\n}\n")
return sb.String()
}
type InterfaceFuncType struct {
FuncType
ASTField *ast.Field
ASTFuncType *ast.FuncType
}
func NewInterfaceFuncType(astField *ast.Field, astFuncType *ast.FuncType) (*InterfaceFuncType, error) {
funcType := &InterfaceFuncType{
ASTField: astField,
ASTFuncType: astFuncType,
}
if len(astField.Names) > 0 {
funcType.Name = astField.Names[0].Name
}
if astFuncType.Params != nil {
funcType.Params = make([]*FieldType, 0, astFuncType.Params.NumFields())
for _, astParamField := range astFuncType.Params.List {
err := funcType.ParseParam(astParamField)
if err != nil {
return nil, err
}
}
}
if astFuncType.Results != nil {
funcType.Results = make([]*FieldType, 0, astFuncType.Results.NumFields())
for _, astResultField := range astFuncType.Results.List {
err := funcType.ParseResult(astResultField)
if err != nil {
return nil, err
}
}
}
return funcType, nil
}
func (this *InterfaceFuncType) String() string {
return fmt.Sprintf("InterfaceFuncType.String()")
}