-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdocs.go
151 lines (133 loc) · 3.7 KB
/
docs.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
package kuu
import (
"gopkg.in/yaml.v2"
"regexp"
"strings"
)
type Doc struct {
Openapi string
Info DocInfo
Servers []DocServer
Tags []DocTag
Paths map[string](DocPathItems)
Components DocComponent
}
type DocPathItems map[string]DocPathItem
type DocComponent struct {
Schemas map[string]DocComponentSchema
SecuritySchemes map[string]DocSecurityScheme `yaml:"securitySchemes"`
}
type DocComponentSchema struct {
Required []string
Type string
Properties map[string]DocSchemaProperty
}
type DocSchemaProperty struct {
Title string
Type string
Ref string `yaml:"$ref"`
Properties *DocPathSchema
Items *DocSchemaProperty
Format string
Enum []interface{}
Default interface{}
}
type DocInfo struct {
Title string
Description string
Contact DocInfoContact
Version string
}
type DocInfoContact struct {
Email string
}
type DocServer struct {
Url string
Description string
}
type DocTag struct {
Name string
Description string
}
type DocPathItem struct {
Tags []string
Summary string
Description string
OperationID string `yaml:"operationId"`
RequestBody DocPathRequestBody `yaml:"requestBody"`
Responses map[int]DocPathResponse
Parameters []DocPathParameter
Deprecated bool
Security []DocPathItemSecurity
}
type DocPathItemSecurity map[string]([]string)
type DocSecurityScheme struct {
Type string
Name string
In string
}
type DocPathParameter struct {
Name string
In string
Description string
Required bool
Style string
Explode bool
Schema DocPathSchema
AllowEmptyValue bool `yaml:"allowEmptyValue"`
}
type DocPathRequestBody struct {
Description string
Content map[string]DocPathContentItem
Required bool
}
type DocPathContentItem struct {
Schema DocPathSchema
}
type DocPathSchema struct {
Type string
Description string
Format string
Ref string `yaml:"$ref"`
Properties map[string]DocPathSchema
Items *DocPathSchema
Enum []interface{}
Default interface{}
Required bool
}
type DocPathResponse struct {
Description string
Content map[string]DocPathContentItem
}
// Marshal
func (d *Doc) Marshal() (result string) {
out, err := yaml.Marshal(d)
if err != nil {
ERROR(err)
return
}
result = string(out)
result = strings.ReplaceAll(result, "$ref: \"\"", "")
result = strings.ReplaceAll(result, "type: \"\"", "")
result = strings.ReplaceAll(result, "format: \"\"", "")
result = strings.ReplaceAll(result, "default: null", "")
result = strings.ReplaceAll(result, "enum: []", "")
result = strings.ReplaceAll(result, "items:\n\n", "")
result = strings.ReplaceAll(result, "parameters: []", "")
result = strings.ReplaceAll(result, "security: {}", "")
result = strings.ReplaceAll(result, "required: []", "")
result = strings.ReplaceAll(result, "items: null", "")
result = strings.ReplaceAll(result, "email: \"\"", "")
result = strings.ReplaceAll(result, "description: \"\"", "")
result = strings.ReplaceAll(result, "properties: null", "")
result = strings.ReplaceAll(result, "properties: {}", "")
result = strings.ReplaceAll(result, "content: {}", "")
result = strings.ReplaceAll(result, "style: \"\"", "")
result = strings.ReplaceAll(result, "explode: false", "")
result = strings.ReplaceAll(result, "required: false", "")
result = strings.ReplaceAll(result, "deprecated: false", "")
result = strings.ReplaceAll(result, "allowEmptyValue: false", "")
result = regexp.MustCompile(`(\s*.*)\s*\n\s*\n`).ReplaceAllString(result, "$1\n")
result = regexp.MustCompile(`\s*requestBody.*\n(\s*responses.*)`).ReplaceAllString(result, "\n$1")
return
}