This repository was archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode.go
More file actions
113 lines (91 loc) · 2.06 KB
/
node.go
File metadata and controls
113 lines (91 loc) · 2.06 KB
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
package yamlpatch
import (
"reflect"
)
// Node holds a YAML document that has not yet been processed into a NodeMap or
// NodeSlice
type Node struct {
raw *interface{}
container Container
}
// NewNode returns a new Node. It expects a pointer to an interface{}
func NewNode(raw *interface{}) *Node {
return &Node{
raw: raw,
}
}
// NewNodeFromMap returns a new Node based on a map[interface{}]interface{}
func NewNodeFromMap(m map[interface{}]interface{}) *Node {
var raw interface{}
raw = m
return &Node{
raw: &raw,
}
}
// NewNodeFromSlice returns a new Node based on a []interface{}
func NewNodeFromSlice(s []interface{}) *Node {
var raw interface{}
raw = s
return &Node{
raw: &raw,
}
}
// MarshalYAML implements yaml.Marshaler, and returns the correct interface{}
// to be marshaled
func (n *Node) MarshalYAML() (interface{}, error) {
if n == nil {
return nil, nil
}
if n.container != nil {
return n.container, nil
}
return *n.raw, nil
}
// UnmarshalYAML implements yaml.Unmarshaler
func (n *Node) UnmarshalYAML(unmarshal func(interface{}) error) error {
var data interface{}
err := unmarshal(&data)
if err != nil {
return err
}
n.raw = &data
return nil
}
// Empty returns whether the raw value is nil
func (n *Node) Empty() bool {
return n == nil || *n.raw == nil
}
// Container returns the node as a Container
func (n *Node) Container() Container {
if n.container != nil {
return n.container
}
switch rt := (*n.raw).(type) {
case []interface{}:
c := make(nodeSlice, len(rt))
n.container = &c
for i := range rt {
c[i] = NewNode(&rt[i])
}
case map[interface{}]interface{}:
c := make(nodeMap, len(rt))
n.container = &c
for k := range rt {
v := rt[k]
c[k] = NewNode(&v)
}
}
return n.container
}
// Equal compares the values of the raw interfaces that the YAML was
// unmarshaled into
func (n *Node) Equal(other *Node) bool {
if n == nil {
return other == nil
}
return reflect.DeepEqual(*n.raw, *other.raw)
}
// Value returns the raw value of the node
func (n *Node) Value() interface{} {
return *n.raw
}