/
node.go
243 lines (210 loc) · 6.27 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package dasel
import (
"fmt"
"io"
"os"
"reflect"
"regexp"
"github.com/tomwright/dasel/storage"
)
// Selector represents the selector for a node.
type Selector struct {
// Raw is the full selector.
Raw string `json:"raw"`
// Current is the selector to be used with the current node.
Current string `json:"current"`
// Remaining is the remaining parts of the Raw selector.
Remaining string `json:"remaining"`
// Type is the type of the selector.
Type string `json:"type"`
// Property is the name of the property this selector targets, if applicable.
Property string `json:"property,omitempty"`
// Index is the index to use if applicable.
Index int `json:"index,omitempty"`
// Conditions contains a set of conditions to optionally match a target.
Conditions []Condition `json:"conditions,omitempty"`
}
// Copy returns a copy of the selector.
func (s Selector) Copy() Selector {
return Selector{
Raw: s.Raw,
Current: s.Current,
Remaining: s.Remaining,
Type: s.Type,
Property: s.Property,
Index: s.Index,
Conditions: s.Conditions,
}
}
// Node represents a single node in the chain of nodes for a selector.
type Node struct {
// Previous is the previous node in the chain.
Previous *Node `json:"-"`
// Next contains the next node in the chain.
// This is used with Query and Put requests.
Next *Node `json:"next,omitempty"`
// NextMultiple contains the next nodes in the chain.
// This is used with QueryMultiple and PutMultiple requests.
// When a major version change occurs this will completely replace Next.
NextMultiple []*Node `json:"nextMultiple,omitempty"`
// OriginalValue is the value returned from the parser.
// In most cases this is the same as Value, but is different for thr YAML parser
// as it contains information on the original document.
OriginalValue interface{} `json:"-"`
// Value is the value of the current node.
Value reflect.Value `json:"value"`
// Selector is the selector for the current node.
Selector Selector `json:"selector"`
wasInitialised bool
}
// String returns the value of the node as a string.
// No formatting is done here, you get the raw value.
func (n *Node) String() string {
return fmt.Sprint(n.InterfaceValue())
}
// InterfaceValue returns the value stored within the node as an interface{}.
func (n *Node) InterfaceValue() interface{} {
// We shouldn't be able to get here but this will stop a panic if we do.
if !n.Value.IsValid() {
return nil
}
return n.Value.Interface()
}
const (
propertySelector = `(?P<property>[a-zA-Z\-_]+)`
indexSelector = `\[(?P<index>[0-9a-zA-Z\*]*?)\]`
)
var (
propertyRegexp = regexp.MustCompile(fmt.Sprintf("^\\.?%s", propertySelector))
indexRegexp = regexp.MustCompile(fmt.Sprintf("^\\.?%s", indexSelector))
newDynamicRegexp = regexp.MustCompile(fmt.Sprintf("^\\.?((?:\\(.*\\))+)"))
)
func isValid(value reflect.Value) bool {
return value.IsValid() && !safeIsNil(value)
}
func safeIsNil(value reflect.Value) bool {
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer,
reflect.Interface, reflect.Slice:
return value.IsNil()
}
return false
}
func nilValue() reflect.Value {
return reflect.ValueOf(nil)
}
func unwrapValue(value reflect.Value) reflect.Value {
if value.Kind() == reflect.Interface && !value.IsNil() {
return value.Elem()
}
return value
}
func derefValue(value reflect.Value) reflect.Value {
if value.Kind() == reflect.Ptr {
return value.Elem()
}
return value
}
// New returns a new root node with the given value.
func New(value interface{}) *Node {
rootNode := &Node{
Previous: nil,
Next: nil,
NextMultiple: nil,
Selector: Selector{
Raw: ".",
Current: ".",
Remaining: "",
Type: "ROOT",
Property: "",
},
}
rootNode.setRealValue(value)
return rootNode
}
// NewFromFile returns a new root node by parsing file using specified read parser.
func NewFromFile(filename, parser string) (*Node, error) {
readParser, err := storage.NewReadParserFromString(parser)
if err != nil {
return nil, err
}
data, err := storage.LoadFromFile(filename, readParser)
if err != nil {
return nil, err
}
return New(data), nil
}
// NewFromReader returns a new root node by parsing from Reader using specified read parser.
func NewFromReader(reader io.Reader, parser string) (*Node, error) {
readParser, err := storage.NewReadParserFromString(parser)
if err != nil {
return nil, err
}
data, err := storage.Load(readParser, reader)
if err != nil {
return nil, err
}
return New(data), nil
}
// WriteToFile writes data to the given file with the specified options.
func (n *Node) WriteToFile(filename, parser string, writeOptions []storage.ReadWriteOption) error {
f, err := os.Create(filename)
if err != nil {
return err
}
// https://www.joeshaw.org/dont-defer-close-on-writable-files/
if err = n.Write(f, parser, writeOptions); err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// Write writes data to Writer using specified write parser and options.
func (n *Node) Write(writer io.Writer, parser string, writeOptions []storage.ReadWriteOption) error {
writeParser, err := storage.NewWriteParserFromString(parser)
if err != nil {
return err
}
value := n.InterfaceValue()
originalValue := n.OriginalValue
if err := storage.Write(writeParser, value, originalValue, writer, writeOptions...); err != nil {
return err
}
return nil
}
func (n *Node) setValue(newValue interface{}) {
n.Value = reflect.ValueOf(newValue)
if n.Selector.Type == "ROOT" {
n.OriginalValue = newValue
}
}
func (n *Node) setRealValue(newValue interface{}) {
switch typed := newValue.(type) {
case storage.RealValue:
n.Value = reflect.ValueOf(typed.RealValue())
default:
n.Value = reflect.ValueOf(typed)
}
if n.Selector.Type == "ROOT" {
n.OriginalValue = newValue
}
}
func (n *Node) setReflectValue(newValue reflect.Value) {
n.Value = newValue
if n.Selector.Type == "ROOT" {
n.OriginalValue = unwrapValue(newValue).Interface()
}
}
func (n *Node) setRealReflectValue(newValue reflect.Value) {
val := unwrapValue(newValue).Interface()
switch typed := val.(type) {
case storage.RealValue:
n.OriginalValue = typed
n.Value = reflect.ValueOf(typed.RealValue())
default:
n.Value = newValue
}
if n.Selector.Type == "ROOT" {
n.OriginalValue = val
}
}