-
Notifications
You must be signed in to change notification settings - Fork 24
/
enrich-api-swagger.go
225 lines (203 loc) · 5.07 KB
/
enrich-api-swagger.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
package main
import (
"os"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
func main() {
swaggerFile := os.Args[1]
swaggerBytes, err := os.ReadFile(swaggerFile)
if err != nil {
panic(err)
}
// decode as YAML
var swagger map[string]interface{}
err = yaml.Unmarshal(swaggerBytes, &swagger)
if err != nil {
panic(err)
}
// look for definitions
definitions, ok := swagger["definitions"].(map[string]interface{})
if !ok {
panic("no definitions")
}
replaceRefs := make(map[string]string)
// read all definitions to look for keys with prefix
// these deinitions have to replaced with new names that do not contain the prefix and all the dot separators are removed
for k, v := range definitions {
// range through properties and remove all properties that start with "private"
if m, ok := v.(map[string]interface{}); ok {
if properties, ok := m["properties"].(map[string]interface{}); ok {
for k := range properties {
// field starts with private
if strings.HasPrefix(k, "private") {
delete(properties, k)
}
}
}
}
key := k
if strings.HasPrefix(key, "aperture.") {
v1 := ".v1."
// find the first occurrence of v1 in key
i := strings.Index(key, v1)
if i == -1 {
panic("cannot find v1 in key: all aperture definitions must have v1 in their name")
}
// remove these characters from key including v1
key = key[i+len(v1):]
}
// remove dots in key
key = strings.ReplaceAll(key, ".", "")
if key == k {
continue
}
// replace this key
// first check if the new key already exists
if _, ok := definitions[key]; ok {
panic("please provide unique definition name as the key already exists: " + key)
}
definitions[key] = v
delete(definitions, k)
replaceRefs["#/definitions/"+k] = "#/definitions/" + key
}
// search for $ref key in entire swagger and replace with new name
replaceRef(swagger, replaceRefs)
// process gotags
processGoTags(swagger)
// encode as YAML
swaggerBytes, err = yaml.Marshal(swagger)
if err != nil {
panic(err)
}
// write to file
err = os.WriteFile(swaggerFile, swaggerBytes, 0o600)
if err != nil {
panic(err)
}
}
func replaceRef(content map[string]interface{}, replacements map[string]string) {
for k, v := range content {
if k == "$ref" {
ref, ok := v.(string)
if !ok {
continue
}
if newRef, ok := replacements[ref]; ok {
content[k] = newRef
}
}
// dive into the map
if m, ok := v.(map[string]interface{}); ok {
replaceRef(m, replacements)
}
// dive into the array
if a, ok := v.([]interface{}); ok {
for _, v1 := range a {
if m, ok := v1.(map[string]interface{}); ok {
replaceRef(m, replacements)
}
}
}
}
}
func processGoTags(content map[string]interface{}) {
// look for "@gotags: " line in the description or title
// example of gotags annotation: "@gotags: default:"info" validate:"oneof=info warn crit"
for k, v := range content {
var text string
switch k {
case "description":
if t, ok := v.(string); ok {
text = t
}
case "title":
if t, ok := v.(string); ok {
text = t
}
default:
// dive into the map or array
if m, ok := v.(map[string]interface{}); ok {
processGoTags(m)
} else if a, ok := v.([]interface{}); ok {
for _, v1 := range a {
if m, ok := v1.(map[string]interface{}); ok {
processGoTags(m)
}
}
}
continue
}
// split desc into separate lines
lines := strings.Split(text, "\n")
// look for "@gotags: " line
for _, line := range lines {
prefix := "@gotags: "
// look for "@gotags:" line
if !strings.HasPrefix(line, prefix) {
continue
}
// remove line from description
text = strings.ReplaceAll(text, line, "")
if text == "" {
delete(content, k)
} else {
content[k] = text
}
// remove "@gotags: " prefix
tags := line[len(prefix):]
tagMap := parseStructTag(tags)
// add each tag as "x-go-tag-<tagname>" key
for k1, v1 := range tagMap {
content["x-go-tag-"+k1] = v1
}
}
}
}
func parseStructTag(tags string) map[string]string {
tagMap := make(map[string]string)
for tags != "" {
// Skip leading space.
i := 0
for i < len(tags) && tags[i] == ' ' {
i++
}
tags = tags[i:]
if tags == "" {
break
}
// Scan to colon. A space, a quote or a control character is a syntax error.
// Strictly speaking, control chars include the range [0x7f, 0x9f], not just
// [0x00, 0x1f], but in practice, we ignore the multi-byte control characters
// as it is simpler to inspect the tag's bytes than the tag's runes.
i = 0
for i < len(tags) && tags[i] > ' ' && tags[i] != ':' && tags[i] != '"' && tags[i] != 0x7f {
i++
}
if i == 0 || i+1 >= len(tags) || tags[i] != ':' || tags[i+1] != '"' {
break
}
name := string(tags[:i])
tags = tags[i+1:]
// Scan quoted string to find value.
i = 1
for i < len(tags) && tags[i] != '"' {
if tags[i] == '\\' {
i++
}
i++
}
if i >= len(tags) {
break
}
qvalue := string(tags[:i+1])
tags = tags[i+1:]
value, err := strconv.Unquote(qvalue)
if err != nil {
continue
}
tagMap[name] = value
}
return tagMap
}