forked from vmware-archive/yaml-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
83 lines (67 loc) · 1.6 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
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,
}
}
// MarshalYAML implements yaml.Marshaler, and returns the correct interface{}
// to be marshaled
func (n *Node) MarshalYAML() (interface{}, error) {
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.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 {
return reflect.DeepEqual(*n.raw, *other.raw)
}
// Value returns the raw value of the node
func (n *Node) Value() interface{} {
return *n.raw
}