-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobj_map.go
219 lines (198 loc) · 4.13 KB
/
obj_map.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
package openapi
import (
"bytes"
"encoding/json"
"reflect"
"github.com/chanced/jsonx"
"github.com/chanced/transcode"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
)
type Item[T node] struct {
Location
Key Text
Value T
}
// ObjMap is a map of OpenAPI Objects of type T
type ObjMap[T node] struct {
Location
Items []Item[T]
}
func (*ObjMap[T]) Kind() Kind {
var t T
return t.Kind()
}
func (*ObjMap[T]) mapKind() Kind { return KindUndefined }
func (*ObjMap[T]) sliceKind() Kind {
var t T
return objSliceKind(t)
}
func (om *ObjMap[T]) Refs() []Ref {
if om == nil {
return nil
}
refs := []Ref{}
for _, item := range om.Items {
refs = append(refs, item.Value.Refs()...)
}
return refs
}
func (om *ObjMap[T]) nodes() []node {
if om == nil {
return nil
}
edges := make([]node, 0, len(om.Items))
for _, item := range om.Items {
edges = appendEdges(edges, item.Value)
}
return edges
}
func (om *ObjMap[T]) setLocation(loc Location) error {
if om == nil {
return nil
}
om.Location = loc
for _, kv := range om.Items {
if err := kv.Value.setLocation(loc.AppendLocation(string(kv.Key))); err != nil {
return err
}
}
return nil
}
func (om *ObjMap[T]) Get(key Text) T {
var t T
for _, kv := range om.Items {
if kv.Key == key {
t = kv.Value
break
}
}
return t
}
func (om *ObjMap[T]) Set(key Text, obj T) {
if om == nil || om.Items == nil {
*om = ObjMap[T]{
Items: []Item[T]{},
}
}
for i, kv := range om.Items {
if kv.Key == key {
om.Items[i] = Item[T]{
Location: om.AppendLocation(key.String()),
Key: key,
Value: obj,
}
return
}
}
om.Items = append(om.Items, Item[T]{
Location: om.AppendLocation(key.String()),
Key: key,
Value: obj,
})
}
func (om *ObjMap[T]) Del(key Text) {
}
func (om *ObjMap[T]) UnmarshalJSON(data []byte) error {
var t T
var m ObjMap[T]
*om = m
if !jsonx.IsObject(data) {
return &json.UnmarshalTypeError{
Value: jsonx.TypeOf(data).String(),
Type: reflect.TypeOf(t),
Struct: "PathItemMap",
}
}
var pi T
var err error
gjson.ParseBytes(data).ForEach(func(key, value gjson.Result) bool {
if err = json.Unmarshal([]byte(value.Raw), &pi); err != nil {
return false
}
m.Items = append(m.Items, Item[T]{Key: Text(key.String()), Value: pi})
return true
})
*om = m
return err
}
func (om *ObjMap[T]) MarshalJSON() ([]byte, error) {
var err error
b := bytes.Buffer{}
var j []byte
_ = j
b.WriteByte('{')
for _, entry := range om.Items {
if b.Len() > 1 {
b.WriteByte(',')
}
jsonx.EncodeAndWriteString(&b, entry.Key)
b.WriteByte(':')
j, err = entry.Value.MarshalJSON()
if err != nil {
return nil, err
}
b.Write(j)
}
b.WriteByte('}')
return b.Bytes(), err
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (om ObjMap[T]) MarshalYAML() (interface{}, error) {
j, err := om.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 (om *ObjMap[T]) 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, om)
}
func (om *ObjMap[T]) Anchors() (*Anchors, error) {
if om == nil {
return nil, nil
}
var anchors *Anchors
var err error
for _, e := range om.Items {
anchors, err = anchors.merge(e.Value.Anchors())
if err != nil {
return anchors, err
}
}
//∆
return anchors, nil
}
func (om *ObjMap[T]) isNil() bool { return om == nil }
var _ (node) = (*ObjMap[*Server])(nil)
// func (om *ObjMap[T]) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return om.resolveNodeByPointer(ptr)
// }
// func (om *ObjMap[T]) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return om, nil
// }
// tok, _ := ptr.NextToken()
// v := om.Get(Text(tok))
// if v.isNil() {
// return nil, newErrNotFound(om.Location.AbsoluteLocation(), tok)
// }
// return nil, nil
// }