/
scheme.go
233 lines (193 loc) · 5.82 KB
/
scheme.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package summerfish
import (
"fmt"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
type SchemeHolder struct {
SwaggerVersion string `json:"swagger" yaml:"swagger"`
Information SchemeInformation `json:"info" yaml:"info"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
BasePath string `json:"basePath" yaml:"basePath"`
Schemes []string `json:"schemes"`
Paths PathsHolder `json:"paths"`
}
type SchemeInformation struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
}
var jsonMapping = map[string]string{
"bool": "boolean",
"string": "string",
"int": "number",
"int8": "number",
"int16": "number",
"int32": "number",
"int64": "number",
"uint": "number",
"uint8": "number",
"uint16": "number",
"uint32": "number",
"uint64": "number",
"uintptr": "number",
"byte": "number",
"rune": "number",
"float32": "number",
"float64": "number",
"complex64": "number",
"complex128": "number",
}
var link = regexp.MustCompile("(^[A-Za-z])|_([A-Za-z])")
var versionRegex = regexp.MustCompile(`v\d+`)
func mapRoutesToPaths(routerHolders []RouteHolder, prefix string) PathsHolder {
paths := PathsHolder{}
for i, router := range routerHolders {
if len(router.Methods) == 0 {
continue
}
if strings.HasSuffix(prefix, "/") {
prefix = prefix[0 : len(prefix)-1]
}
router.Route = strings.TrimPrefix(router.Route, prefix)
if _, ok := paths[router.Route]; !ok {
paths[router.Route] = Method{}
}
//Must be initialized like this so that empty converts to json properly
parameters := []InputParameter{}
for _, entry := range router.Query {
parameters = append(parameters, generateInputParameter("query", entry.Name, entry.Type, false))
}
for _, entry := range router.Path {
parameters = append(parameters, generateInputParameter("path", entry.Name, entry.Type, true))
}
if len(router.Body.Name) > 0 {
parameters = append(parameters, mapBodyRoute(router.Body))
}
hasFormData := false
for _, entry := range router.FormData {
parameters = append(parameters, generateInputParameter("formData", entry.Name, entry.Type, true))
hasFormData = true
}
tag := strings.Replace(getTagFromRoute(router.Route), "-", "_", -1)
operation := Operation{
ID: fmt.Sprintf("%s_%d", router.Name, i),
Summary: convertFromCamelCase(router.Name),
Parameters: parameters,
Tags: []string{convertToCamelCase(tag)},
Responses: map[string]OperationResponse{"200": OperationResponse{Description: "successful operation"}},
}
if hasFormData {
operation.Consumes = []string{"multipart/form-data"}
}
paths[router.Route][strings.ToLower(router.Methods[0])] = operation
}
return paths
}
func getTagFromRoute(route string) string {
split := strings.Split(route, "/")
if len(split) == 0 {
return ""
}
if len(split) == 1 {
return split[0]
}
versionFound := versionRegex.FindStringSubmatch(split[1])
if len(versionFound) == 0 || len(versionFound[0]) != len(split[1]) || len(split) <= 2 {
return split[1]
}
return split[2]
}
func mapBodyRoute(bodyField NameType) (result InputParameter) {
result = generateInputParameter("body", bodyField.Name, "", true)
result.Schema = mapInternalParameters(bodyField)
return
}
func mapInternalParameters(bodyField NameType) SchemaParameters {
props := make(map[string]SchemaParameters)
for _, param := range bodyField.Children {
if len(param.Children) > 0 {
props[param.Name] = mapInternalParameters(param)
} else {
mappedParamType, ok := jsonMapping[param.Type]
if !ok {
mappedParamType = param.Type
}
if param.IsArray {
props[param.Name] = SchemaParameters{Type: "array", Items: &SchemaParameters{Type: mappedParamType}}
} else {
props[param.Name] = SchemaParameters{Type: mappedParamType}
}
}
}
if bodyField.IsArray {
items := &SchemaParameters{Type: "object", Properties: props}
return SchemaParameters{Type: "array", Items: items}
}
return SchemaParameters{Type: "object", Properties: props}
}
func generateInputParameter(queryType, name, varType string, isRequired bool) InputParameter {
ip := InputParameter{
QueryType: queryType,
Type: varType,
Name: name,
Description: name,
Required: isRequired,
}
//convert from snake case since camelcase is needed for the next step
if strings.Contains(name, "_") {
ip.Description = convertToCamelCase(ip.Description)
}
ip.Description = convertFromCamelCase(ip.Description)
return ip
}
func convertToCamelCase(str string) string {
return link.ReplaceAllStringFunc(str, func(s string) string {
return strings.ToUpper(strings.Replace(s, "_", "", -1))
})
}
func convertFromCamelCase(input string) string {
if !utf8.ValidString(input) {
return input
}
var entries []string
var runes [][]rune
lastClass := 0
class := 0
// split into fields based on class of unicode character
for _, letter := range input {
switch true {
case unicode.IsLower(letter):
class = 1
case unicode.IsUpper(letter):
class = 2
case unicode.IsDigit(letter):
class = 3
default:
class = 4
}
if class == lastClass {
runes[len(runes)-1] = append(runes[len(runes)-1], letter)
} else {
runes = append(runes, []rune{letter})
}
lastClass = class
}
for i := 0; i < len(runes)-1; i++ {
if unicode.IsUpper(runes[i][0]) && unicode.IsLower(runes[i+1][0]) {
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
runes[i] = runes[i][:len(runes[i])-1]
}
}
for i, entry := range runes {
if len(entry) == 0 {
continue
}
if i == 0 && unicode.IsLower(entry[0]) {
entry[0] -= 32
}
entries = append(entries, string(entry))
}
return strings.Join(entries, " ")
}