-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema_ref.go
203 lines (172 loc) · 3.99 KB
/
schema_ref.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
package openapi
import (
"encoding/json"
"fmt"
"github.com/chanced/jsonx"
"github.com/chanced/transcode"
"github.com/chanced/uri"
"gopkg.in/yaml.v3"
)
type SchemaRefType uint8
const (
SchamRefTypeUndefined SchemaRefType = iota
SchemaRefTypeRef
SchemaRefTypeDynamic
SchemaRefTypeRecursive
)
type SchemaRef struct {
Location
Ref *uri.URI `json:"-"`
Resolved *Schema `json:"-"`
SchemaRefKind SchemaRefType `json:"-"`
}
func (sr *SchemaRef) Nodes() []Node {
if sr == nil {
return nil
}
return downcastNodes(sr.nodes())
}
func (sr *SchemaRef) RefType() RefType {
switch sr.SchemaRefKind {
case SchemaRefTypeRef:
return RefTypeSchema
case SchemaRefTypeDynamic:
return RefTypeSchemaDynamicRef
case SchemaRefTypeRecursive:
return RefTypeSchemaRecursiveRef
default:
return RefTypeUndefined
}
}
func (sr *SchemaRef) RefKind() Kind { return KindSchema }
func (sr *SchemaRef) nodes() []node { return []node{sr.Resolved} }
func (*SchemaRef) Refs() []Ref { return nil }
func (sr *SchemaRef) IsResolved() bool {
return sr.Resolved != nil
}
func (sr *SchemaRef) URI() *uri.URI { return sr.Ref }
func (*SchemaRef) Kind() Kind { return KindSchemaRef }
func (*SchemaRef) mapKind() Kind { return KindUndefined }
func (*SchemaRef) sliceKind() Kind { return KindUndefined }
func (sr *SchemaRef) ResolvedNode() Node {
if sr == nil {
return nil
}
return sr.Resolved
}
// func (sr *SchemaRef) Clone() *SchemaRef {
// if sr == nil {
// return nil
// }
// c := *sr
// return &c
// }
func (sr *SchemaRef) resolve(n Node) error {
if n == nil {
return fmt.Errorf("node is nil")
}
if s, ok := n.(*Schema); ok {
sr.Resolved = s
return nil
}
return NewResolutionError(sr, KindSchema, n.Kind())
}
func (*SchemaRef) Anchors() (*Anchors, error) { return nil, nil }
func (sr *SchemaRef) setLocation(l Location) error {
if sr == nil {
return nil
}
sr.Location = l
// if sr.Schema != nil {
// if sr.Ref != nil {
// nl, err := NewLocation(*sr.Ref)
// if err != nil {
// return err
// }
// sr.Schema.setLocation(nl)
// return nil
// }
// return sr.Schema.setLocation(l)
// }
return nil
}
func (sr *SchemaRef) UnmarshalJSON(data []byte) error {
if jsonx.IsString(data) {
var u uri.URI
if err := json.Unmarshal(data, &u); err != nil {
return err
}
sr.Ref = &u
return nil
}
var s Schema
err := json.Unmarshal(data, &s)
sr.Resolved = &s
return err
}
func (sr SchemaRef) MarshalJSON() ([]byte, error) {
return json.Marshal(sr.Ref)
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (sr SchemaRef) MarshalYAML() (interface{}, error) {
j, err := sr.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (sr *SchemaRef) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, sr)
}
func (sr *SchemaRef) isNil() bool { return sr == nil }
func (sr *SchemaRef) Clone() *SchemaRef {
if sr == nil {
return nil
}
var ref *uri.URI
if sr.Ref != nil {
ref = sr.Ref.Clone()
}
return &SchemaRef{
Ref: ref,
Location: Location{
absolute: sr.Location.absolute,
relative: sr.Location.relative,
},
Resolved: sr.Resolved.Clone(), // should this be cloned?
SchemaRefKind: sr.SchemaRefKind,
}
}
var (
_ node = (*SchemaRef)(nil)
_ Ref = (*SchemaRef)(nil)
)
// func (sr *SchemaRef) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return sr.resolveNodeByPointer(ptr)
// }
// func (sr *SchemaRef) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// tok, _ := ptr.NextToken()
// if !ptr.IsRoot() {
// if sr.Ref != nil {
// return nil, newErrNotResolvable(sr.Location.AbsoluteLocation(), tok)
// }
// }
// return sr, nil
// }