-
Notifications
You must be signed in to change notification settings - Fork 2
/
obj_slice.go
135 lines (117 loc) · 2.77 KB
/
obj_slice.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
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
type ObjSlice[T node] struct {
Location `json:"-"`
Items []T `json:"-"`
}
// Anchors implements node
func (os *ObjSlice[T]) Anchors() (*Anchors, error) {
if os == nil {
return nil, nil
}
var a *Anchors
var err error
for _, x := range os.Items {
if a, err = a.merge(x.Anchors()); err != nil {
return nil, err
}
}
return a, nil
}
// Kind implements node
func (os *ObjSlice[T]) Kind() Kind {
var t T
return objSliceKind(t)
}
func (os *ObjSlice[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(os.Items)
}
// Refs implements node
func (os *ObjSlice[T]) Refs() []Ref {
if os == nil {
return nil
}
var refs []Ref
for _, x := range os.Items {
refs = append(refs, x.Refs()...)
}
return refs
}
// // ResolveNodeByPointer implements node
// func (os *ObjSlice[T]) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return os.resolveNodeByPointer(ptr)
// }
// func (os *ObjSlice[T]) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return os, nil
// }
// nxt, tok, _ := ptr.Next()
// idx, err := tok.Int()
// if err != nil || idx < 0 {
// return nil, newErrNotResolvable(os.absolute, tok)
// }
// if idx >= len(os.Items) {
// return nil, newErrNotFound(os.AbsoluteLocation(), tok)
// }
// return os.Items[idx].resolveNodeByPointer(nxt)
// }
// UnmarshalJSON implements node
func (os *ObjSlice[T]) UnmarshalJSON(data []byte) error {
*os = ObjSlice[T]{}
items := []T{}
if err := json.Unmarshal(data, &items); err != nil {
return err
}
os.Items = items
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (os ObjSlice[T]) MarshalYAML() (interface{}, error) {
j, err := os.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 (os *ObjSlice[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, os)
}
func (os *ObjSlice[T]) nodes() []node {
var edges []node
for _, x := range os.Items {
edges = appendEdges(edges, x)
}
return edges
}
func (os *ObjSlice[T]) setLocation(loc Location) error {
if os == nil {
return nil
}
os.Location = loc
return nil
}
func (os *ObjSlice[T]) isNil() bool { return os == nil }
func (*ObjSlice[T]) sliceKind() Kind { return KindUndefined }
func (*ObjSlice[T]) mapKind() Kind { return KindUndefined }
var _ node = (*ObjSlice[*SecurityRequirement])(nil)