/
type.go
132 lines (126 loc) · 2.98 KB
/
type.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
package astutil
import (
"go/ast"
"go/token"
)
// AddKVToStruct 为 type xx struct 添加成员属性
//
// 参数key允许为空
// 例子:
// type EmptyStruct struct {
// }
// 执行:
// AddKVToStruct(f,"EmptyStruct", "age", "int")
// AddKVToStruct(f,"EmptyStruct", "", "TypeStruct")
// 结果为:
// type EmptyStruct struct {
// age int
// TypeStruct
// }
func AddKVToStruct(f *ast.File, name, key, value string, addDocLine token.Pos) bool {
if name == "" || value == "" {
return false
}
for _, decl := range f.Decls {
gen, ok := decl.(*ast.GenDecl)
if ok && gen.Tok == token.TYPE {
varSpec := gen.Specs[0].(*ast.TypeSpec)
if varSpec.Name.Name == name {
// 重置doc
if gen.Doc != nil {
gen.Doc.List[0].Slash = gen.Doc.List[0].Slash + token.NoPos + addDocLine
}
switch specType := varSpec.Type.(type) {
case *ast.StructType:
typeFields := specType.Fields
if typeFields.List == nil {
typeFields.List = []*ast.Field{}
}
typeFields.List = append(typeFields.List, getField(key, value))
return true
}
}
}
}
return false
}
// AddFuncToInterface 为interface添加函数
//
// 测试数据:
//
// type EmptyInf interface {
// }
//
// 传参:
//
// params := &AstFunc{
// Name: "test0",
// Params: []AstKv{
// {Key: "p1", Value: "string"},
// {Key: "p2", Value: "...*AstKv"},
// },
// Results: []AstKv{
// {Key: "ret1", Value: "string"},
// },
// }
//
// 结果为:
//
// type EmptyInf interface {
// test0(p1 string, p2 ...*AstKv) (ret1 string)
// }
func AddFuncToInterface(f *ast.File, name string, params *AstFunc) bool {
if name == "" || params == nil {
return false
}
for _, decl := range f.Decls {
gen, ok := decl.(*ast.GenDecl)
if ok && gen.Tok == token.TYPE {
varSpec := gen.Specs[0].(*ast.TypeSpec)
if varSpec.Name.Name == name {
switch specType := varSpec.Type.(type) {
case *ast.InterfaceType:
typeFields := specType.Methods
if typeFields.List == nil {
typeFields.List = []*ast.Field{}
}
typeFields.List = append(typeFields.List, &ast.Field{
Names: []*ast.Ident{ast.NewIdent(params.Name)},
Type: getFuncType(params),
})
return true
}
}
}
}
return false
}
func getField(key, value string) *ast.Field {
ret := &ast.Field{
Type: &ast.Ident{NamePos: token.NoPos + 1, Name: value},
}
if key != "" {
ret.Names = []*ast.Ident{ast.NewIdent(key)}
}
return ret
}
func getFuncType(params *AstFunc) *ast.FuncType {
newFunc := &ast.FuncType{}
if params.Params != nil {
newFunc.Params = &ast.FieldList{
List: make([]*ast.Field, 0),
}
for _, param := range params.Params {
newFunc.Params.List = append(newFunc.Params.List, getField(param.Key, param.Value))
}
}
if params.Results != nil {
newFunc.Results = &ast.FieldList{
List: make([]*ast.Field, 0),
}
for _, param := range params.Results {
newFunc.Results.List = append(newFunc.Results.List, getField(param.Key, param.Value))
}
}
return newFunc
}