-
Notifications
You must be signed in to change notification settings - Fork 0
/
from.go
291 lines (274 loc) · 7.71 KB
/
from.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package chioas
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/go-chi/chi/v5"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"io"
"net/http"
"reflect"
"strings"
)
// FromJson builds a Definition from an existing OAS spec JSON
//
// where all methods in the spec must have a `x-handler` property, e.g.
//
// "paths": {
// "/": {
// "get": {
// "x-handler": "getRoot"
// ...
//
// where the key "getRoot" must be provided in the handlers arg
//
// or...
//
// "paths": {
// "/": {
// "get": {
// "x-handler": ".GetRoot"
// ...
//
// where the "GetRoot" must be a http.HandlerFunc method on the supplied api arg
func FromJson(r io.Reader, opts *FromOptions) (result *Definition, err error) {
useOptions := defaultedFromOptions(opts, true)
useReader := r
if useOptions.DocOptions.ServeDocs {
if useOptions.DocOptions.specData, err = io.ReadAll(r); err == nil {
useReader = bytes.NewReader(useOptions.DocOptions.specData)
}
}
if err == nil {
d := json.NewDecoder(useReader)
d.UseNumber()
obj := map[string]any{}
if err = d.Decode(&obj); err == nil {
result, err = useOptions.definitionFrom(obj)
}
}
return
}
// FromYaml builds a Definition from an existing OAS spec YAML
//
// where all methods in the spec must have a `x-handler` tag, e.g.
//
// paths:
// "/":
// get:
// x-handler: "getRoot"
//
// where the key "getRoot" must be provided in the handlers arg
//
// or...
//
// paths:
// "/":
// get:
// x-handler: ".GetRoot"
//
// where the "GetRoot" must be a http.HandlerFunc method on the supplied api arg
func FromYaml(r io.Reader, opts *FromOptions) (result *Definition, err error) {
useOptions := defaultedFromOptions(opts, false)
useReader := r
if useOptions.DocOptions.ServeDocs {
if useOptions.DocOptions.specData, err = io.ReadAll(r); err == nil {
useReader = bytes.NewReader(useOptions.DocOptions.specData)
}
}
if err == nil {
d := yaml.NewDecoder(useReader)
rootNode := &node{}
if err = d.Decode(rootNode); err == nil {
rootObj, ok := rootNode.Value.(map[string]any)
if !ok {
err = errors.New("bad yaml")
} else {
result, err = useOptions.definitionFrom(rootObj)
}
}
}
return
}
func (f *FromOptions) definitionFrom(obj map[string]any) (result *Definition, err error) {
result = &Definition{
DocOptions: *f.DocOptions,
Methods: Methods{},
Paths: Paths{},
Middlewares: f.getPathMiddlewares("/"),
}
if paths, ok := getMap(obj, tagNamePaths); ok {
err = f.processPaths(result, paths)
} else {
err = errors.New("no paths defined")
}
return
}
func (f *FromOptions) processPaths(def *Definition, paths map[string]any) error {
for path, rawV := range paths {
if v, ok := rawV.(map[string]any); ok {
if path == "/" {
if err := f.processMethods(path, v, def.Methods); err != nil {
return err
}
} else if pObj, err := f.getPath(def, path); err == nil {
if err := f.processMethods(path, v, pObj.Methods); err != nil {
return err
}
} else {
return err
}
} else {
return fmt.Errorf("path '%s' not a map", path)
}
}
return nil
}
func (f *FromOptions) getPath(def *Definition, path string) (result Path, err error) {
parts := strings.Split(path, "/")
if len(parts) > 0 && parts[0] == "" {
parts = parts[1:]
}
if len(parts) > 0 && parts[len(parts)-1] == "" {
parts = parts[:len(parts)-1]
}
if l := len(parts); l > 0 && !slices.Contains(parts, "") {
curr := def.Paths
for i, pt := range parts {
if rp, ok := curr["/"+pt]; ok {
result = rp
} else {
result = Path{
Paths: Paths{},
Methods: Methods{},
Middlewares: f.getPathMiddlewares("/" + strings.Join(parts[:i+1], "/")),
}
curr["/"+pt] = result
}
if i == l-1 {
return
}
curr = result.Paths
}
} else {
err = fmt.Errorf("invalid path '%s'", path)
}
return
}
func (f *FromOptions) getPathMiddlewares(path string) chi.Middlewares {
if f.PathMiddlewares != nil {
return f.PathMiddlewares(path)
}
return nil
}
var chiMethods = map[string]bool{
http.MethodConnect: true,
http.MethodDelete: true,
http.MethodGet: true,
http.MethodHead: true,
http.MethodOptions: true,
http.MethodPatch: true,
http.MethodPost: true,
http.MethodPut: true,
http.MethodTrace: true,
}
func (f *FromOptions) processMethods(path string, obj map[string]any, methods Methods) error {
for k, rawV := range obj {
m := strings.ToUpper(k)
if chiMethods[m] {
if v, ok := rawV.(map[string]any); ok {
if method, err := f.getMethod(path, m, v); err == nil && method != nil {
methods[m] = *method
} else if err != nil {
return err
}
} else {
return fmt.Errorf("method '%s' on path '%s' not a map", m, path)
}
}
}
return nil
}
func (f *FromOptions) getMethod(path string, method string, m map[string]any) (result *Method, err error) {
if rawV, ok := m["x-handler"]; ok {
if handlerName, ok := rawV.(string); ok {
var hf http.HandlerFunc
if hf, err = f.getMethodHandler(path, method, handlerName); err == nil {
result = &Method{Handler: hf}
}
} else {
err = fmt.Errorf("path '%s', method '%s' - 'x-handler' tag not a string", path, method)
}
} else if f.Strict {
err = fmt.Errorf("path '%s', method '%s' - missing 'x-handler' tag", path, method)
}
return
}
func (f *FromOptions) getMethodHandler(path string, method string, handlerName string) (http.HandlerFunc, error) {
if strings.HasPrefix(handlerName, ".") && f.Api != nil {
mfn := reflect.ValueOf(f.Api).MethodByName(handlerName[1:])
if !mfn.IsValid() {
return nil, fmt.Errorf("path '%s', method '%s' - 'x-handler' method '%s' does not exist", path, method, handlerName[1:])
}
if hf, ok := mfn.Interface().(func(http.ResponseWriter, *http.Request)); ok {
return hf, nil
}
return nil, fmt.Errorf("path '%s', method '%s' - 'x-handler' method '%s' is not http.HandlerFunc", path, method, handlerName[1:])
} else if f.Handlers != nil {
if hf, ok := f.Handlers[handlerName]; ok && hf != nil {
return hf, nil
}
}
return nil, fmt.Errorf("path '%s', method '%s' - 'x-handler' no method or func found for '%s'", path, method, handlerName)
}
func getMap(obj map[string]any, tagName string) (map[string]any, bool) {
if raw, ok := obj[tagName]; ok {
if m, ok := raw.(map[string]any); ok {
return m, true
}
}
return nil, false
}
// Handlers is a lookup, by name, used by FromJson or FromYaml
type Handlers map[string]http.HandlerFunc
type PathMiddlewares func(path string) chi.Middlewares
// FromOptions is used by FromJson and FromYaml to control how the definition is
// built from a pre-existing OAS spec
type FromOptions struct {
// DocOptions is the optional doc options for the generated Definition
DocOptions *DocOptions
// Api is the optional api object that provides methods where `x-handler` is specified as ".HandlerMethodName"
Api any
// Handlers is the optional look for handlers specified by `x-handler`
Handlers Handlers
// Strict when set, causes the FromJson / FromYaml to error if no `x-handler` tag is specified
Strict bool
// PathMiddlewares is an optional func that sets middlewares on paths found in the from spec
PathMiddlewares PathMiddlewares
}
func defaultedFromOptions(opts *FromOptions, asJson bool) *FromOptions {
if opts != nil {
return &FromOptions{
DocOptions: defaultedDocsOptions(opts.DocOptions, asJson),
Api: opts.Api,
Handlers: opts.Handlers,
Strict: opts.Strict,
PathMiddlewares: opts.PathMiddlewares,
}
}
return &FromOptions{
DocOptions: defaultedDocsOptions(nil, asJson),
Strict: true,
}
}
func defaultedDocsOptions(opts *DocOptions, asJson bool) *DocOptions {
if opts != nil {
opts.AsJson = asJson
return opts
}
return &DocOptions{
AsJson: asJson,
}
}