-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
191 lines (147 loc) · 3.58 KB
/
routes.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Routes
*/
package main
import (
"log"
"strconv"
"strings"
)
// GetRoutes returns a map of route arrays indexed by their path
func GetRoutes(lines []string, filePath string) (routes map[string][]Route, err error) {
routes = map[string][]Route{}
// Routes
symbols := GetSymbols(lines, "@route ")
// If no symbols are found, skip
if len(symbols) == 0 {
return
}
for _, symbol := range symbols {
route := Route{}
comments, _, blockEnd := GetCommentBlock(lines, symbol.LineNum)
tagMap := ParseTags(comments)
// Assume that after the block ends, so the method starts
route.LineNum = blockEnd + 1
route.FilePath = filePath
// Parse the route field (only use the first)
// Note: There may be more than one route tag, but anything after the first is ignored
log.Printf("Route: %s", tagMap)
if _, ok := tagMap["route"]; !ok {
log.Printf("No routes found at path %s", filePath)
continue
}
routeParts := strings.Fields(tagMap["route"][0])
if len(routeParts) < 2 {
log.Fatalf("The tag @route is not in the correct format. File: %s; Line: %d", filePath, route.LineNum)
}
if len(comments) > 0 {
route.Description = comments[0]
}
route.OperationID = routeParts[0]
route.Verb = routeParts[1]
route.Path = routeParts[2]
// Return tags
if _, ok := tagMap["return"]; ok {
for _, ret := range tagMap["return"] {
response, err := parseRouteResponse(ret)
if err != nil {
continue
}
route.Responses = append(route.Responses, response)
}
}
// Param tags
if _, ok := tagMap["param"]; ok {
for _, ret := range tagMap["param"] {
param, err := parseRouteParam(ret)
if err != nil {
continue
}
route.Params = append(route.Params, param)
}
}
if _, ok := tagMap["tag"]; ok {
for _, ret := range tagMap["tag"] {
tags, err := parseRouteTag(ret)
if err != nil {
continue
}
route.Tags = tags
}
}
if _, ok := routes[route.Path]; !ok {
routes[route.Path] = []Route{}
}
routes[route.Path] = append(routes[route.Path], route)
}
return
}
func parseRouteParam(ret string) (param Param, err error) {
retParts := strings.Fields(ret)
retPartLen := len(retParts)
if retPartLen < 2 {
// invalid
}
param.Name = retParts[0]
param.Type = retParts[1]
param.Required = true
curIdx := 2
maxIdx := 4
for {
if retPartLen <= curIdx {
break
}
if len(retParts[curIdx]) > 3 && retParts[curIdx][0:3] == "in:" {
in := retParts[curIdx][3:]
switch in {
case "path":
param.In = "path"
case "query":
param.In = "query"
case "form":
param.In = "form"
case "header":
param.In = "header"
case "body":
param.In = in
default:
param.In = "query"
}
}
if len(retParts[curIdx]) == 8 && (retParts[curIdx] == "optional" || retParts[curIdx] == "required") {
param.Required = retParts[curIdx] == "required"
}
curIdx = curIdx + 1
if maxIdx <= curIdx {
break
}
}
// add the description
if retPartLen >= curIdx {
param.Description = strings.Join(retParts[curIdx:], " ")
}
return
}
func parseRouteResponse(ret string) (response Response, err error) {
retParts := strings.Fields(ret)
retPartLen := len(retParts)
responseInt, _ := strconv.Atoi(retParts[0])
response.ResponseCode = responseInt
if retPartLen > 1 {
response.SchemaRef = retParts[1]
}
if retPartLen > 2 {
response.Description = strings.Join(retParts[2:], " ")
}
return
}
func parseRouteTag(ret string) (tags []string, err error) {
if strings.Index(ret, ",") > -1 {
tags = strings.Split(ret, ",")
} else {
tags = []string{
ret,
}
}
return
}