-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterfaceParser.go
133 lines (126 loc) · 3.35 KB
/
interfaceParser.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
package go_annotation
import (
"fmt"
"go/ast"
)
type InterfaceParser struct {
typeSpec *ast.TypeSpec
genDecl *ast.GenDecl
interfaceSpec *ast.InterfaceType
serviceName string
fileImports map[string]*ImportDesc
}
func NewInterfaceParser(
serviceName string,
typeSpec *ast.TypeSpec,
genDecl *ast.GenDecl,
fileImports map[string]*ImportDesc) *InterfaceParser {
return &InterfaceParser{
serviceName: serviceName,
typeSpec: typeSpec,
genDecl: genDecl,
interfaceSpec: typeSpec.Type.(*ast.InterfaceType),
fileImports: fileImports,
}
}
func (s *InterfaceParser) Parse() (*InterfaceDesc, error) {
comments := parseAtComments(s.genDecl.Doc)
description := parseDescription(s.serviceName, s.genDecl.Doc)
funcList, err := s.getFuncList()
if err != nil {
return nil, err
}
if funcList == nil || len(funcList) == 0 {
return nil, nil
}
methods := make([]*MethodDesc, 0)
for _, method := range funcList {
methodDesc, err := s.parserMethod(method)
if err != nil {
return nil, err
}
methods = append(methods, methodDesc)
}
sDesc := &InterfaceDesc{
Name: s.serviceName,
Description: description,
Methods: methods,
Imports: s.parserImports(methods),
Comments: comments,
Annotations: getAnnotationParser(currentAnnotationMode).Parse(comments),
}
return sDesc, nil
}
func (s *InterfaceParser) getFuncList() ([]*ast.Field, error) {
list := make([]*ast.Field, 0)
for _, method := range s.interfaceSpec.Methods.List {
list = append(list, method)
}
return list, nil
}
func (s *InterfaceParser) parserMethod(method *ast.Field) (methodDesc *MethodDesc, err error) {
methodDesc = &MethodDesc{
Comments: make([]string, 0),
Params: make([]*Field, 0),
Results: make([]*Field, 0),
}
// method name
if method.Names == nil || len(method.Names) == 0 {
err = fmt.Errorf("method name is empty")
return
}
if len(method.Names) > 1 {
err = fmt.Errorf("method name is not unique")
return
}
methodDesc.Name = method.Names[0].Name
// funcType
if funcType, ok := method.Type.(*ast.FuncType); ok {
// params
if funcType.Params != nil {
for _, param := range funcType.Params.List {
field, err := parseField(param)
if err != nil {
return nil, err
}
methodDesc.Params = append(methodDesc.Params, field)
}
}
// results
if funcType.Results != nil {
for _, result := range funcType.Results.List {
field, err := parseField(result)
if err != nil {
return nil, err
}
methodDesc.Results = append(methodDesc.Results, field)
}
}
// comment
methodDesc.Comments = parseAtComments(method.Doc)
methodDesc.Description = parseDescription(methodDesc.Name, method.Doc)
methodDesc.Annotations = getAnnotationParser(currentAnnotationMode).Parse(methodDesc.Comments)
return methodDesc, err
} else {
err = fmt.Errorf("method type is not funcType")
return
}
}
func (s *InterfaceParser) parserImports(methods []*MethodDesc) (imports map[string]*ImportDesc) {
imports = make(map[string]*ImportDesc)
fields := make([]*Field, 0)
for _, method := range methods {
for _, param := range method.Params {
fields = append(fields, param)
}
for _, result := range method.Results {
fields = append(fields, result)
}
}
for _, field := range fields {
if imp, ok := s.fileImports[field.PackageName]; ok {
imports[field.PackageName] = imp
}
}
return imports
}