-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
188 lines (152 loc) · 4.67 KB
/
generator.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
package docgen
import (
"fmt"
"io/ioutil"
"strings"
"github.com/swaggest/openapi-go/openapi3"
)
func GenerateDoc(r *Router, name string) ([]byte, error) {
// Read the Router.routes ang get "name", "method", "query", "path", "body".
reflector := openapi3.Reflector{}
reflector.Spec = &openapi3.Spec{Openapi: "3.1.0"}
s := reflector.SpecEns() // Création de la Spec (Object contenant les informations du service)
// o(nRoutes*(nParams+nResps))
for _, op := range r.Routes {
if &op != nil {
oasOp := openapi3.Operation{}
if &op.Path != nil && strings.Contains(op.Path, "{") { //o(len(op.Path))
op.setParameters(&oasOp)
}
reflector.SetRequest(&oasOp, op.Input, strings.ToUpper(op.Method))
if op.Resps != nil {
for code, body := range op.Resps {
reflector.SetJSONResponse(&oasOp, body, code)
}
}
if op.Internal {
oasOp.MapOfAnything = map[string]interface{}{
"x-internal": true,
}
}
oasOp.Deprecated = &op.Deprecated
s.AddOperation(op.Method, op.Path, oasOp)
}
}
for _, comp := range s.Components.Schemas.MapOfSchemaOrRefValues {
setInternal(&comp, s)
}
schema, err := reflector.Spec.MarshalYAML()
if err == nil {
fmt.Print("NO ERROR")
}
return schema, nil
}
func GenerateDocWrite(r *Router, name string) ([]byte, error) {
// Read the Router.routes ang get "name", "method", "query", "path", "body".
reflector := openapi3.Reflector{}
reflector.Spec = &openapi3.Spec{Openapi: "3.1.0"}
s := reflector.SpecEns() // Création de la Spec (Object contenant les informations du service)
internalTag := "[internal]"
// o(nRoutes*(nParams+nResps))
for _, op := range r.Routes {
if &op != nil {
oasOp := openapi3.Operation{}
if &op.Path != nil && strings.Contains(op.Path, "{") { //o(len(op.Path))
op.setParameters(&oasOp)
}
reflector.SetRequest(&oasOp, op.Input, strings.ToUpper(op.Method))
if oasOp.RequestBody != nil {
if oasOp.RequestBody.RequestBody.Content["application/json"].Schema.Schema != nil {
oasOp.RequestBody.RequestBody.Content["application/json"].Schema.Schema.WithMapOfAnythingItem("x-internal", true)
}
if oasOp.RequestBody.RequestBody.Content["application/json"].Schema.SchemaReference != nil {
ref := oasOp.RequestBody.RequestBody.Content["application/json"].Schema.SchemaReference.Ref
ref = strings.TrimPrefix(ref, "#/components/schemas/")
sch := s.Components.Schemas.MapOfSchemaOrRefValues[ref].Schema
sch.WithMapOfAnythingItem("x-internal", true)
print("debug")
}
//oasOp.RequestBody.RequestBody.MapOfAnything["x-internal"] = true
}
if op.Resps != nil {
for code, body := range op.Resps {
reflector.SetJSONResponse(&oasOp, body, code)
}
}
oasOp.MapOfAnything = map[string]interface{}{
"x-internal": true,
}
oasOp.Deprecated = &op.Deprecated
oasOp.Description = &internalTag
s.AddOperation(op.Method, op.Path, oasOp)
}
}
//o(comp * props)
for _, comp := range s.Components.Schemas.MapOfSchemaOrRefValues {
setInternal(&comp, s)
}
schema, err := reflector.Spec.MarshalYAML()
ioutil.WriteFile("openapi.yaml", schema, 0644)
if err == nil {
fmt.Print("NO ERROR")
}
return schema, nil
}
// Need oasSpec to find the components referenced by others
// o(props)
func setInternal(s *openapi3.SchemaOrRef, oasSpec *openapi3.Spec) {
tagInternal := "[internal]"
// o(1)
if s.SchemaReference != nil {
ref := s.SchemaReference.Ref
if &ref != nil {
refTrim := strings.TrimPrefix(ref, "#/components/schemas/")
sch := oasSpec.Components.Schemas.MapOfSchemaOrRefValues[refTrim]
if &sch != nil {
setInternal(&sch, oasSpec)
}
}
}
// o(props)
if s.Schema != nil {
s.Schema.WithMapOfAnythingItem("x-internal", true)
s.Schema.WithDescription(tagInternal)
for _, prop := range s.Schema.Properties {
if prop.Schema != nil {
prop.Schema.WithMapOfAnythingItem("x-internal", true)
prop.Schema.WithDescription(tagInternal)
}
}
}
}
// o(nParams)
func (r *Route) setParameters(oasOp *openapi3.Operation) {
_path := r.Path
n := strings.Count(_path, "{") //o(len(_path))
oasOp.Parameters = make([]openapi3.ParameterOrRef, n)
//o(n + 2*strings.Cut)
for n >= 0 {
n--
// Get param name between '{' et '}'
_, aft, _ := strings.Cut(_path, "{")
_path = aft
pName, _, ok := strings.Cut(aft, "}")
if ok {
required := true
strSchema := openapi3.SchemaTypeString
// o(1)
oasOp.Parameters[n] = openapi3.ParameterOrRef{
Parameter: &openapi3.Parameter{
Name: pName,
In: openapi3.ParameterInPath,
Required: &required,
Schema: &openapi3.SchemaOrRef{
Schema: &openapi3.Schema{
Type: &strSchema,
},
},
},
}
}
}
}