-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathref_resolver.go
514 lines (431 loc) · 13.1 KB
/
ref_resolver.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package core
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
mgcSchema "github.com/MagaluCloud/magalu/mgc/core/schema"
"github.com/MagaluCloud/magalu/mgc/core/utils"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-openapi/jsonpointer"
)
// Similar to JSON Pointers - [RFC6901]
//
// The difference is that it allow an URL to be specified before the JSON Pointer,
// so both are valid:
// - /path/to/element
// - http://some.url.com#/path/to/element
//
// [RFC6901]: https://datatracker.ietf.org/doc/html/rfc6901
type RefPath string
type RefPathResolver interface {
// validate the given string, convert it to RefPath and resolve it
Resolve(path string) (result any, err error)
ResolvePath(path RefPath) (result any, err error)
}
// refResolverContextKey is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type refResolverContextKey string
// theRefResolverContextKey is the key for sdk.Grouper values in Contexts. It is
// unexported; clients use NewGrouperContext() and GrouperFromContext()
// instead of using this key directly.
var theRefResolverContextKey refResolverContextKey = "github.com/MagaluCloud/magalu/mgc/core/RefPathResolver"
func NewRefPathResolverContext(parent context.Context, refResolver RefPathResolver) context.Context {
return context.WithValue(parent, theRefResolverContextKey, refResolver)
}
func RefPathResolverFromContext(ctx context.Context) RefPathResolver {
if value, ok := ctx.Value(theRefResolverContextKey).(RefPathResolver); !ok {
return nil
} else {
return value
}
}
func (path RefPath) Split() (parentPath RefPath, field string) {
if path == "" || path == "/" {
return
}
i := strings.LastIndex(string(path), "/")
if i < 0 {
return
}
parentPath = path[:i]
field = jsonpointer.Unescape(string(path[i+1:]))
return
}
var errorInvalidStart = errors.New(`JSON pointer must be empty or start with a "/"`)
var errorDocumentUrlUnsupported = errors.New("document URL is unsupported")
func (path RefPath) Validate() (err error) {
_, p, _ := strings.Cut(string(path), "#")
if p == "" {
p = string(path)
}
if !strings.HasPrefix(p, "/") {
return &RefPathResolveError{path, errorInvalidStart}
}
return nil
}
func (path RefPath) Add(parts ...string) RefPath {
for _, part := range parts {
path += RefPath("/" + jsonpointer.Escape(part))
}
return path
}
func (path RefPath) SplitUrl() (url string, p RefPath) {
before, after, found := strings.Cut(string(path), "#")
if !found {
return "", path
}
return before, RefPath(after)
}
func (p *RefPath) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*p = RefPath(s)
return p.Validate()
}
var _ json.Unmarshaler = (*RefPath)(nil)
type RefPathResolveError struct {
Path RefPath
Err error
}
func (e *RefPathResolveError) Error() string {
leaf := e.Err
path := e.Path
for {
if e, ok := leaf.(*RefPathResolveError); ok {
path = e.Path
leaf = e.Err
} else {
break
}
}
return fmt.Sprintf("could not resolve %q: %s", path, leaf.Error())
}
func (e *RefPathResolveError) Unwrap() error {
return e.Err
}
var _ error = (*RefPathResolveError)(nil)
type MissingFieldError string
func (e MissingFieldError) Error() string {
return fmt.Sprintf("missing field: %q", string(e))
}
var _ error = (*MissingFieldError)(nil)
// Wraps MultiRefPathResolver and automatically pass CurrentUrl
type BoundRefPathResolver struct {
Resolver *MultiRefPathResolver
CurrentUrl string // if RefPath's URL is this placeholder given to MultiRefPathResolver, use this URL instead.
}
var _ RefPathResolver = (*BoundRefPathResolver)(nil)
func NewBoundRefResolver(currentUrl string, resolver *MultiRefPathResolver) *BoundRefPathResolver {
return &BoundRefPathResolver{resolver, currentUrl}
}
func (r *BoundRefPathResolver) Resolve(ref string) (result any, err error) {
return r.Resolver.Resolve(ref, r.CurrentUrl)
}
func (r *BoundRefPathResolver) ResolvePath(path RefPath) (result any, err error) {
return r.Resolver.ResolvePath(path, r.CurrentUrl)
}
// Discovers the document from the pointerPath and call the specific document resolver.
//
// if RefPath's URL is CurrentUrlPlaceholder, then use the given currentUrl. If the placeholder
// if empty, currentUrl is also used.
//
// if CurrentUrlPlaceholder is non-empty and RefPath's URL is empty, then use EmptyDocumentUrl.
type MultiRefPathResolver struct {
Resolvers map[string]RefPathResolver // maps url => resolver
CurrentUrlPlaceholder string // if RefPath's URL is this placeholder, use the given currentUrl
EmptyDocumentUrl string // if RefPath's URL is empty, then use this URL.
}
func NewMultiRefPathResolver() *MultiRefPathResolver {
return &MultiRefPathResolver{
Resolvers: map[string]RefPathResolver{},
}
}
func (r *MultiRefPathResolver) Resolve(ref string, currentUrl string) (result any, err error) {
path := RefPath(ref)
err = path.Validate()
if err != nil {
return
}
return r.ResolvePath(path, currentUrl)
}
func (r *MultiRefPathResolver) ResolvePath(path RefPath, currentUrl string) (result any, err error) {
url, p := path.SplitUrl()
if url == r.CurrentUrlPlaceholder {
url = currentUrl
} else if url == "" {
url = r.EmptyDocumentUrl
}
docResolver := r.Resolvers[url]
if docResolver == nil {
return nil, &RefPathResolveError{path, fmt.Errorf("unknown document %q", url)}
}
return docResolver.ResolvePath(p)
}
// Associates the given URL to a document resolver
func (r *MultiRefPathResolver) Add(url string, docResolver RefPathResolver) error {
if _, ok := r.Resolvers[url]; ok {
return fmt.Errorf("document resolver already added: %q", url)
}
r.Resolvers[url] = docResolver
return nil
}
// Resolve within a single document, walks from root group to leaf Executors and their sub-fields
//
// Most users will want BoundRefPathResolver instead.
type DocumentRefPathResolver struct {
cache sync.Map // JSON Pointer string => resolved value
getRoot func() (any, error)
}
var _ RefPathResolver = (*DocumentRefPathResolver)(nil)
// NOTE: getRoot() will be called multiple times, make sure it caches its own results, ex utils.NewLazyLoaderWithError()
func NewDocumentRefPathResolver(getRoot utils.LoadWithError[any]) *DocumentRefPathResolver {
return &DocumentRefPathResolver{
cache: sync.Map{},
getRoot: getRoot,
}
}
func (r *DocumentRefPathResolver) Resolve(ref string) (result any, err error) {
path := RefPath(ref)
err = path.Validate()
if err != nil {
return
}
return r.ResolvePath(path)
}
func (r *DocumentRefPathResolver) ResolvePath(path RefPath) (result any, err error) {
if path == "" || path == "/" {
var root any
root, err = r.getRoot()
if err != nil {
return
}
result = root
return
}
if cachedResult, ok := r.cache.Load(path); ok {
return cachedResult, nil
}
url, _ := path.SplitUrl()
if url != "" {
err = &RefPathResolveError{path, errorDocumentUrlUnsupported}
return
}
parentPath, field := path.Split()
parent, err := r.ResolvePath(parentPath)
if err != nil {
err = &RefPathResolveError{path, err}
return
}
if field == "" { // not expected to have "parent/", but okay...
result = parent
} else {
result, err = resolveField(field, parent)
}
if err == nil {
r.cache.Store(path, result)
} else {
err = &RefPathResolveError{path, err}
}
return
}
func resolveField(field string, doc any) (result any, err error) {
switch v := doc.(type) {
case Grouper:
return resolveGrouper(field, v)
case Executor:
return resolveExecutor(field, v)
case Linker:
return resolveLinker(field, v)
case Links:
return resolveLinkerMap(field, v)
case map[string]Executor:
return resolveExecutorMap(field, v)
default:
return resolveGeneric(field, v)
}
}
func resolveGeneric(field string, doc any) (result any, err error) {
result, _, err = jsonpointer.GetForToken(doc, field)
if err != nil {
return
}
return
}
func resolveDescriptor(field string, desc Descriptor) (result any, err error) {
switch field {
case "name":
return desc.Name(), nil
case "version":
return desc.Version(), nil
case "description":
return desc.Description(), nil
}
return nil, MissingFieldError(field)
}
func resolveGrouper(field string, group Grouper) (result any, err error) {
result, err = group.GetChildByName(field)
if err != nil {
// if no child with names clobbering Descriptor fields, resolve those:
result, err = resolveDescriptor(field, group)
}
return
}
func resolveExecutor(name string, exec Executor) (result any, err error) {
switch name {
case "parametersSchema":
return exec.ParametersSchema(), nil
case "configsSchema":
return exec.ConfigsSchema(), nil
case "resultSchema":
return exec.ResultSchema(), nil
case "links":
return exec.Links(), nil
case "related":
return exec.Related(), nil
default:
return resolveDescriptor(name, exec)
}
}
func resolveLinker(field string, linker Linker) (result any, err error) {
switch field {
case "name":
return linker.Name(), nil
case "description":
return linker.Description(), nil
case "parametersSchema", "additionalParametersSchema":
return linker.AdditionalParametersSchema(), nil
case "configsSchema", "additionalConfigsSchema":
return linker.AdditionalConfigsSchema(), nil
case "resultSchema":
return linker.ResultSchema(), nil
default:
return nil, MissingFieldError(field)
}
}
func resolveLinkerMap(field string, m Links) (result Linker, err error) {
if result, ok := m[field]; ok {
return result, nil
}
return nil, MissingFieldError(field)
}
func resolveExecutorMap(field string, m map[string]Executor) (result Executor, err error) {
if result, ok := m[field]; ok {
return result, nil
}
return nil, MissingFieldError(field)
}
const extensionResolvedKey = "github.com/MagaluCloud/magalu/mgc/core/resolved"
func isSchemaResolved(schema *mgcSchema.Schema) bool {
return schema.Extensions[extensionResolvedKey] == "true"
}
func markSchemaResolved(schema *mgcSchema.Schema) {
if schema.Extensions == nil {
schema.Extensions = map[string]any{}
}
schema.Extensions[extensionResolvedKey] = "true"
}
// This changes schema *IN PLACE* so future resolutions will use the same pointers
//
// This guarantees the Schema and internal elements are fully resolved.
func ResolveSchemaChildren(r RefPathResolver, schema *mgcSchema.Schema) (result *mgcSchema.Schema, err error) {
if isSchemaResolved(schema) {
return schema, nil
}
var pendingResolution mgcSchema.SchemaRefs
addPending := func(ref *mgcSchema.SchemaRef) {
if ref != nil && ref.Value == nil {
pendingResolution = append(pendingResolution, ref)
}
}
addPendingSlice := func(refs []*mgcSchema.SchemaRef) {
for _, ref := range refs {
addPending(ref)
}
}
for _, ref := range schema.Properties {
addPending(ref)
}
addPending(schema.Not)
addPending(schema.Items)
addPending(schema.AdditionalProperties.Schema)
addPendingSlice(schema.OneOf)
addPendingSlice(schema.AnyOf)
addPendingSlice(schema.AllOf)
for _, ref := range pendingResolution {
value, err := ResolveSchemaRef(r, ref)
if err != nil {
return nil, err
}
ref.Value = (*openapi3.Schema)(value)
}
markSchemaResolved(schema)
return schema, nil
}
func ResolveSchema(refResolver RefPathResolver, ref string) (result *mgcSchema.Schema, err error) {
path := RefPath(ref)
err = path.Validate()
if err != nil {
return
}
return ResolveSchemaPath(refResolver, path)
}
func ResolveSchemaPath(refResolver RefPathResolver, path RefPath) (result *mgcSchema.Schema, err error) {
v, err := refResolver.ResolvePath(path)
if err != nil {
return
}
switch t := v.(type) {
case *mgcSchema.Schema:
return ResolveSchemaChildren(refResolver, t)
case *openapi3.Schema:
return ResolveSchemaChildren(refResolver, (*mgcSchema.Schema)(t))
case *mgcSchema.SchemaRef: // same as openapi3.SchemaRef
return ResolveSchemaRef(refResolver, t)
default:
return nil, fmt.Errorf("could not resolve %q: expected type %T, got %T", path, result, t)
}
}
// This changes schemaRef *IN PLACE* so future resolutions will use the same pointers
//
// This guarantees the SchemaRef, Schema and internal elements are fully resolved.
func ResolveSchemaRef(refResolver RefPathResolver, schemaRef *mgcSchema.SchemaRef) (result *mgcSchema.Schema, err error) {
if schemaRef.Value != nil {
return ResolveSchemaChildren(refResolver, (*mgcSchema.Schema)(schemaRef.Value))
}
ref := schemaRef.Ref
if ref == "" {
return nil, fmt.Errorf("empty schema reference")
}
result, err = ResolveSchema(refResolver, ref)
if err != nil {
return
}
schemaRef.Ref = "" // force kin-openapi/openapi3 to MarshalJSON()/MarshalYAML() the full object
schemaRef.Value = (*openapi3.Schema)(result)
return
}
func ResolveExecutor(refResolver RefPathResolver, ref string) (exec Executor, err error) {
path := RefPath(ref)
err = path.Validate()
if err != nil {
return
}
return ResolveExecutorPath(refResolver, path)
}
func ResolveExecutorPath(refResolver RefPathResolver, path RefPath) (exec Executor, err error) {
v, err := refResolver.ResolvePath(path)
if err != nil {
return
}
exec, ok := v.(Executor)
if !ok {
err = fmt.Errorf("path must be an executor, %q is %#v", path, v)
return
}
return exec, nil
}