-
Notifications
You must be signed in to change notification settings - Fork 2
/
anchor.go
84 lines (72 loc) · 1.58 KB
/
anchor.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
package openapi
import "fmt"
type DuplicateAnchorError struct {
A *Anchor
B *Anchor
}
func (dae *DuplicateAnchorError) Error() string {
return fmt.Sprintf("duplicate anchor: %s", dae.A.Name)
}
type AnchorType uint8
const (
AnchorTypeUndefined AnchorType = iota
AnchorTypeRegular // $anchor
AnchorTypeRecursive // $recursiveAnchor
AnchorTypeDynamic // $dynamicAnchor
)
type Anchor struct {
Location
In *Schema
Name Text
Type AnchorType
}
type Anchors struct {
Standard []Anchor // $anchor
Recursive *Anchor // $recursiveAnchor
Dynamic []Anchor // $dynamicAnchor
}
func (a *Anchors) StandardAnchor(name Text) *Anchor {
if a == nil {
return nil
}
for _, anchor := range a.Standard {
if anchor.Name == name {
return &anchor
}
}
return nil
}
func (a *Anchors) DynamicAnchor(name Text) *Anchor {
if a == nil {
return nil
}
for _, anchor := range a.Dynamic {
if anchor.Name == name {
return &anchor
}
}
return nil
}
func (a *Anchors) merge(b *Anchors, err error) (*Anchors, error) {
if err != nil {
return nil, err
}
if b == nil {
return a, nil
}
// we do not merge recursive anchors as they must be at the root of the
// document. This method is only called when merging schemas from nested
// components, so we can, and should, drop them from result if not coming
// from a.
if a == nil {
return &Anchors{
Standard: b.Standard,
Dynamic: b.Dynamic,
}, nil
}
return &Anchors{
Standard: append(a.Standard, b.Standard...),
Dynamic: append(a.Dynamic, b.Dynamic...),
Recursive: a.Recursive,
}, nil
}