-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnode.go
More file actions
217 lines (188 loc) · 4.35 KB
/
node.go
File metadata and controls
217 lines (188 loc) · 4.35 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
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
package seer
import (
"errors"
"fmt"
"strings"
"github.com/spf13/afero"
"github.com/taubyte/utils/maps"
pathUtils "github.com/taubyte/utils/path"
)
// Helper
func Fork(n *Query) *Query {
return n.Fork()
}
// Copy a query ... the conly way to reuse a query.
func (n *Query) Fork() *Query {
nq := &Query{
seer: n.seer,
write: n.write,
requestedPath: make([]string, len(n.requestedPath)),
ops: make([]op, len(n.ops)),
errors: make([]error, 0),
}
copy(nq.requestedPath, n.requestedPath)
copy(nq.ops, n.ops)
return nq
}
func (n *Query) Set(value interface{}) *Query {
n.ops = append(n.ops,
op{
opType: opTypeSet,
value: value,
handler: opSetInYaml,
},
)
return n
}
func (n *Query) Delete() *Query {
n.ops = append(n.ops,
op{
opType: opTypeSet,
handler: opDelete,
},
)
return n
}
func (n *Query) Get(name string) *Query {
n.requestedPath = append(n.requestedPath, name)
n.ops = append(n.ops,
op{
opType: opTypeGetOrCreate,
name: name,
handler: opGetOrCreate,
},
)
return n
}
func (n *Query) Document() *Query {
if len(n.ops) == 0 {
// should never happen actually, as you need to call get or set before
n.errors = append(n.errors, errors.New("can't convert root to a document"))
return n
}
n.write = true
// grab path from previous
// and delete last op
last_op_index := len(n.ops) - 1
name := n.ops[last_op_index].name
n.ops = n.ops[:last_op_index]
n.ops = append(n.ops,
op{
opType: opTypeCreateDocument,
name: name,
handler: opCreateDocument,
},
)
return n
}
// return a copy of the Stack Error
func (n *Query) Errors() []error {
ret := make([]error, len(n.errors))
copy(ret, n.errors)
return ret
}
func (n *Query) Clear() *Query {
n.write = false
n.ops = n.ops[:0]
n.errors = n.errors[:0]
return n
}
func (n *Query) Commit() error {
n.seer.lock.Lock()
defer n.seer.lock.Unlock()
n.write = true
if len(n.errors) > 0 {
return fmt.Errorf("%d errors preventing commit", len(n.errors))
}
var (
path []string = make([]string, 0)
doc *yamlNode // nil when created here
err error
)
for _, op := range n.ops {
path, doc, err = op.handler(op, n, path, doc)
if err != nil {
return fmt.Errorf("committing failed with %s", err.Error())
}
}
return nil
}
func (n *Query) Value(dst interface{}) error {
n.seer.lock.Lock()
defer n.seer.lock.Unlock()
n.write = false
if len(n.errors) > 0 {
return fmt.Errorf("%d errors preventing getting value", len(n.errors))
}
var (
path []string = make([]string, 0)
doc *yamlNode // nil when created here
err error
)
for _, op := range n.ops {
path, doc, err = op.handler(op, n, path, doc)
if err != nil {
return fmt.Errorf("Value failed with %s", err.Error())
}
}
if doc == nil {
//let's see if we're looking at a folder
_path := "/" + pathUtils.Join(path)
if st, exist := n.seer.fs.Stat(_path); exist == nil && st.IsDir() {
// it's a folder
dirFiles, err := afero.ReadDir(n.seer.fs, _path)
if err != nil {
return fmt.Errorf("parsing folder `%s` failed with %w", path, err)
}
_dst := make([]string, 0)
for _, f := range dirFiles {
if f.IsDir() {
_dst = append(_dst, f.Name())
} else {
fname := f.Name()
item := strings.TrimSuffix(fname, ".yaml")
if item+".yaml" == fname {
_dst = append(_dst, item)
}
}
}
switch idst := dst.(type) {
case *interface{}:
*idst = _dst
case *[]string:
*idst = _dst
default:
return fmt.Errorf("value of a folder can only be mapped to `*[]string` or *interface{} not `%T`", dst)
}
return nil
} else {
return fmt.Errorf("no data found for %s", path)
}
}
err = doc.this.Decode(dst)
if err != nil {
return fmt.Errorf("decode(%T) failed with %s", dst, err)
}
return nil
}
func (n *Query) List() ([]string, error) {
var val interface{}
err := n.Value(&val)
if err != nil {
return nil, fmt.Errorf("listing keys failed with %s", err)
}
// Empty value should be an empty list
if val == nil {
return nil, nil
}
switch ival := val.(type) {
case []string:
return ival, nil
case map[string]interface{}:
return maps.Keys(ival), nil
case map[interface{}]interface{}:
return maps.Keys(maps.SafeInterfaceToStringKeys(ival)), nil
default:
return nil, fmt.Errorf("listing keys failed with %v type(%T) is not a map or a slice", val, val)
}
}