-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
101 lines (85 loc) · 2.48 KB
/
node.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
package openapi
import (
"github.com/chanced/jsonpointer"
"github.com/chanced/uri"
"gopkg.in/yaml.v3"
)
type refable interface {
node
refable()
}
type Node interface {
// AbsoluteLocation returns the absolute path of the node in URI form.
// This includes the URI path of the resource and the JSON pointer
// of the node.
//
// e.g. openapi.json#/components/schemas/Example
AbsoluteLocation() uri.URI
// RelativeLocation returns the path as a JSON pointer for the Node.
RelativeLocation() jsonpointer.Pointer
// Kind returns the Kind for the given Node
Kind() Kind
// Anchors returns a list of all Anchors in the Node and all descendants.
Anchors() (*Anchors, error)
// Refs returns a list of all Refs from the Node and all descendants.
Refs() []Ref
// MarshalJSON marshals JSON
//
// MarshalJSON satisfies the json.Marshaler interface
MarshalJSON() ([]byte, error)
// UnmarshalJSON unmarshals JSON
//
// UnmarshalJSON satisfies the json.Unmarshaler interface
UnmarshalJSON(data []byte) error
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
MarshalYAML() (interface{}, error)
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
UnmarshalYAML(value *yaml.Node) error
// ResolveNodeByPointer resolves a Node by a jsonpointer. It validates the
// pointer and then attempts to resolve the Node.
//
// # Errors
//
// - [ErrNotFound] indicates that the component was not found
//
// - [ErrNotResolvable] indicates that the pointer path can not resolve to a
// Node
//
// - [jsonpointer.ErrMalformedEncoding] indicates that the pointer encoding
// is malformed
//
// - [jsonpointer.ErrMalformedStart] indicates that the pointer is not empty
// and does not start with a slash
// ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error)
}
type node interface {
Node
setLocation(loc Location) error
// init(ctx context.Context, resolver *resolver) error
// resolveNodeByPointer(ctx context.Context, resolver *resolver, p jsonpointer.Pointer) (node, error)
mapKind() Kind
sliceKind() Kind
// resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error)
location() Location
isNil() bool
nodes() []node
}
type objSlicedNode interface {
node
objSliceKind() Kind
}
func downcastNodes(n []node) []Node {
nodes := make([]Node, len(n))
for i, v := range n {
nodes[i] = v
}
return nodes
}
func appendEdges(nodes []node, elems ...node) []node {
for _, n := range elems {
if !n.isNil() {
nodes = append(nodes, n)
}
}
return nodes
}