-
Notifications
You must be signed in to change notification settings - Fork 5
/
unwrap.go
444 lines (412 loc) · 12.1 KB
/
unwrap.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
package raml
import (
"fmt"
"github.com/acronis/go-stacktrace"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
func (r *RAML) unwrapTypes(
types *orderedmap.OrderedMap[string, *BaseShape],
f *Library,
isAnnotationType bool,
) *stacktrace.StackTrace {
var st *stacktrace.StackTrace
for pair := types.Oldest(); pair != nil; pair = pair.Next() {
base := pair.Value
if base == nil {
se := StacktraceNew("shape is nil", f.Location,
stacktrace.WithType(StacktraceTypeUnwrapping))
if st == nil {
st = se
} else {
st = st.Append(se)
}
continue
}
us, err := r.UnwrapShape(base)
if err != nil {
se := StacktraceNewWrapped("unwrap shape", err, f.Location,
stacktrace.WithType(StacktraceTypeUnwrapping), stacktrace.WithPosition(&base.Position))
if st == nil {
st = se
} else {
st = st.Append(se)
}
continue
}
types.Set(pair.Key, us)
if isAnnotationType {
r.PutAnnotationTypeIntoFragment(us.Name, f.Location, base)
} else {
r.PutTypeIntoFragment(us.Name, f.Location, base)
}
}
return st
}
func (r *RAML) unwrapLibrary(f *Library) *stacktrace.StackTrace {
st := r.unwrapTypes(f.AnnotationTypes, f, true)
se := r.unwrapTypes(f.Types, f, false)
if se != nil {
if st == nil {
st = se
} else {
st = st.Append(se)
}
}
return st
}
func (r *RAML) unwrapDataType(f *DataType) *stacktrace.StackTrace {
if f.Shape == nil {
return StacktraceNew("shape is nil", f.Location,
stacktrace.WithType(StacktraceTypeUnwrapping))
}
us, err := r.UnwrapShape(f.Shape)
if err != nil {
return StacktraceNewWrapped("unwrap shape", err, f.Location,
stacktrace.WithType(StacktraceTypeUnwrapping), stacktrace.WithPosition(&f.Shape.Position))
}
f.Shape = us
r.PutTypeIntoFragment(us.Name, f.Location, f.Shape)
return nil
}
func (r *RAML) unwrapFragments() *stacktrace.StackTrace {
var st *stacktrace.StackTrace
for _, frag := range r.fragmentsCache {
switch f := frag.(type) {
case *Library:
se := r.unwrapLibrary(f)
if se != nil {
if st == nil {
st = se
} else {
st = st.Append(se)
}
}
case *DataType:
se := r.unwrapDataType(f)
if se != nil {
if st == nil {
st = se
} else {
st = st.Append(se)
}
}
}
}
return st
}
func (r *RAML) unwrapDomainExtensions() *stacktrace.StackTrace {
var st *stacktrace.StackTrace
for _, item := range r.domainExtensions {
db := item.DefinedBy
ptr, err := r.GetAnnotationTypeFromFragmentPtr(db.Location, db.Name)
if err != nil {
se := StacktraceNewWrapped("get annotation from fragment", err, db.Location,
stacktrace.WithPosition(&db.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
if st == nil {
st = se
} else {
st = st.Append(se)
}
continue
}
item.DefinedBy = ptr
}
return st
}
// UnwrapShapes unwraps all shapes in the RAML in-place.
func (r *RAML) UnwrapShapes() error {
// We need to invalidate old cache and re-populate it because references will no longer be valid after unwrapping.
r.fragmentTypes = make(map[string]map[string]*BaseShape)
r.fragmentAnnotationTypes = make(map[string]map[string]*BaseShape)
r.shapes = make([]*BaseShape, 0, len(r.shapes))
st := r.unwrapFragments()
if st != nil {
return st
}
err := r.markShapeRecursions()
if err != nil {
return fmt.Errorf("mark shape recursions: %w", err)
}
// Links to definedBy must be updated after unwrapping.
st = r.unwrapDomainExtensions()
if st != nil {
return st
}
return nil
}
// markShapeRecursions marks recursive shapes by replacing the beginning of recursion with RecursiveShape in the RAML.
func (r *RAML) markShapeRecursions() error {
// TODO: Maybe count shapes here?
for _, frag := range r.fragmentsCache {
switch f := frag.(type) {
case *Library:
for pair := f.AnnotationTypes.Oldest(); pair != nil; pair = pair.Next() {
if _, err := r.FindAndMarkRecursion(pair.Value); err != nil {
return err
}
}
for pair := f.Types.Oldest(); pair != nil; pair = pair.Next() {
if _, err := r.FindAndMarkRecursion(pair.Value); err != nil {
return err
}
}
case *DataType:
if _, err := r.FindAndMarkRecursion(f.Shape); err != nil {
return err
}
}
}
return nil
}
const HookBeforeFindAndMarkRecursion HookKey = "RAML.FindAndMarkRecursion"
// FindAndMarkRecursion finds and marks recursion in the shape.
func (r *RAML) FindAndMarkRecursion(base *BaseShape) (*BaseShape, error) {
if err := r.callHooks(HookBeforeFindAndMarkRecursion, base); err != nil {
return nil, err
}
if !base.IsUnwrapped() {
return nil, fmt.Errorf("shape is not unwrapped")
}
if base.ShapeVisited {
s := r.MakeRecursiveShape(base)
s.unwrapped = true
return s, nil
}
base.ShapeVisited = true
var err error
switch t := base.Shape.(type) {
case *ArrayShape:
err = r.findAndMarkRecursionInArrayShape(t)
case *ObjectShape:
err = r.findAndMarkRecursionInObjectShape(t)
case *UnionShape:
err = r.findAndMarkRecursionInUnionShape(t)
}
base.ShapeVisited = false
if err != nil {
return nil, err
}
return nil, ErrNil
}
func (r *RAML) findAndMarkRecursionInArrayShape(t *ArrayShape) error {
if t.Items != nil {
rs, err := r.FindAndMarkRecursion(t.Items)
if err != nil {
return fmt.Errorf("find and mark recursion: %w", err)
}
if rs != nil {
t.Items = rs
}
}
return nil
}
func (r *RAML) findAndMarkRecursionInObjectShape(t *ObjectShape) error {
if t.Properties != nil {
for pair := t.Properties.Oldest(); pair != nil; pair = pair.Next() {
prop := pair.Value
rs, err := r.FindAndMarkRecursion(prop.Base)
if err != nil {
return fmt.Errorf("find and mark recursion: %w", err)
}
if rs != nil {
prop.Base = rs
t.Properties.Set(pair.Key, prop)
}
}
}
if t.PatternProperties != nil {
for pair := t.PatternProperties.Oldest(); pair != nil; pair = pair.Next() {
prop := pair.Value
rs, err := r.FindAndMarkRecursion(prop.Base)
if err != nil {
return fmt.Errorf("find and mark recursion: %w", err)
}
if rs != nil {
prop.Base = rs
t.PatternProperties.Set(pair.Key, prop)
}
}
}
return nil
}
func (r *RAML) findAndMarkRecursionInUnionShape(t *UnionShape) error {
for i, item := range t.AnyOf {
rs, err := r.FindAndMarkRecursion(item)
if err != nil {
return fmt.Errorf("find and mark recursion: %w", err)
}
if rs != nil {
t.AnyOf[i] = rs
}
}
return nil
}
func (r *RAML) unwrapObjShape(objShape *ObjectShape) error {
if objShape.Properties != nil {
for pair := objShape.Properties.Oldest(); pair != nil; pair = pair.Next() {
prop := pair.Value
us, err := r.UnwrapShape(prop.Base)
if err != nil {
return StacktraceNewWrapped("object property unwrap", err, objShape.Location,
stacktrace.WithPosition(&objShape.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
prop.Base = us
objShape.Properties.Set(pair.Key, prop)
}
}
if objShape.PatternProperties != nil {
for pair := objShape.PatternProperties.Oldest(); pair != nil; pair = pair.Next() {
prop := pair.Value
us, err := r.UnwrapShape(prop.Base)
if err != nil {
return StacktraceNewWrapped("object pattern property unwrap", err, objShape.Location,
stacktrace.WithPosition(&objShape.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
prop.Base = us
objShape.PatternProperties.Set(pair.Key, prop)
}
}
return nil
}
func (r *RAML) unwrapArrayShape(arrayShape *ArrayShape) error {
if arrayShape.Items != nil {
us, err := r.UnwrapShape(arrayShape.Items)
if err != nil {
return StacktraceNewWrapped("array item unwrap", err, arrayShape.Location,
stacktrace.WithPosition(&arrayShape.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
arrayShape.Items = us
}
return nil
}
func (r *RAML) unwrapUnionShape(unionShape *UnionShape) error {
for i, item := range unionShape.AnyOf {
us, err := r.UnwrapShape(item)
if err != nil {
return StacktraceNewWrapped("union unwrap", err, unionShape.Location,
stacktrace.WithPosition(&unionShape.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
unionShape.AnyOf[i] = us
}
return nil
}
func (r *RAML) unwrapParents(base *BaseShape) (*BaseShape, error) {
var source *BaseShape
switch {
case base.Link != nil:
us, err := r.UnwrapShape(base.Link.Shape)
if err != nil {
return nil, StacktraceNewWrapped("link unwrap", err, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
source = us
base.Link = nil
case len(base.Inherits) > 0:
inherits := base.Inherits
// FIXME: Multiple inheritance members must be checked for compatibility with each other before unwrapping.
ss, err := r.UnwrapShape(inherits[0])
if err != nil {
return nil, StacktraceNewWrapped("parent unwrap", err, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
inherits[0] = ss
for i := 1; i < len(inherits); i++ {
us, errUnwrap := r.UnwrapShape(inherits[i])
if errUnwrap != nil {
return nil, errUnwrap
}
_, errUnwrap = ss.Inherit(us)
if errUnwrap != nil {
return nil, StacktraceNewWrapped("multiple parents unwrap", errUnwrap, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
inherits[i] = us
}
source = ss
}
return source, nil
}
func (r *RAML) unwrapTarget(target Shape) error {
switch trg := target.(type) {
case *ArrayShape:
if err := r.unwrapArrayShape(trg); err != nil {
return fmt.Errorf("unwrap array shape: %w", err)
}
case *ObjectShape:
if err := r.unwrapObjShape(trg); err != nil {
return fmt.Errorf("unwrap object shape: %w", err)
}
case *UnionShape:
if err := r.unwrapUnionShape(trg); err != nil {
return fmt.Errorf("unwrap union shape: %w", err)
}
}
return nil
}
const HookBeforeUnwrapShape HookKey = "RAML.UnwrapShape"
// UnwrapShape recursively copies and unwraps a shape in-place. Use Clone() to create a copy of a shape if necessary.
// Note that this method removes information about links.
func (r *RAML) UnwrapShape(base *BaseShape) (*BaseShape, error) {
if err := r.callHooks(HookBeforeUnwrapShape, base); err != nil {
return nil, err
}
s := base.Shape
if s == nil {
return nil, fmt.Errorf("shape is nil")
}
if base.ShapeVisited {
return base, nil
}
base.ShapeVisited = true
// Skip already unwrapped shapes
if base.IsUnwrapped() {
base.ShapeVisited = false
return base, nil
}
// NOTE: Type aliasing is not inheritance and is not used as a source. It must be unwrapped and returned as is.
if base.Alias != nil {
us, err := r.UnwrapShape(base.Alias)
if err != nil {
return nil, StacktraceNewWrapped("alias unwrap", err, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
base.unwrapped = true
base.ShapeVisited = false
r.PutShape(base)
return base.AliasTo(us)
}
source, err := r.unwrapParents(base)
if err != nil {
return nil, StacktraceNewWrapped("unwrap parents", err, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
if errUnwrap := r.unwrapTarget(s); errUnwrap != nil {
return nil, StacktraceNewWrapped("unwrap target", errUnwrap, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
for pair := base.CustomShapeFacetDefinitions.Oldest(); pair != nil; pair = pair.Next() {
prop := pair.Value
us, errUnwrap := r.UnwrapShape(prop.Base)
if errUnwrap != nil {
return nil, StacktraceNewWrapped("custom shape facet definition unwrap", errUnwrap, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
prop.Base = us
base.CustomShapeFacetDefinitions.Set(pair.Key, prop)
}
if source != nil {
is, errInherit := base.Inherit(source)
if errInherit != nil {
return nil, StacktraceNewWrapped("merge shapes", errInherit, base.Location,
stacktrace.WithPosition(&base.Position), stacktrace.WithType(StacktraceTypeUnwrapping))
}
is.ShapeVisited = false
is.unwrapped = true
r.PutShape(is)
return is, nil
}
base.ShapeVisited = false
base.unwrapped = true
r.PutShape(base)
return base, nil
}