-
Notifications
You must be signed in to change notification settings - Fork 2
/
walker.go
67 lines (60 loc) · 1.41 KB
/
walker.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
package jsonast
import (
"fmt"
)
// Walker is a utility for walking the JSON tree with a few function calls,
// and transforming the JSON that was walked into a type
type Walker struct {
initialValue Value
latestPath string
latestValue Value
Nulls []Null
Strings []String
Numbers []Number
Objects []Object
Arrays []Array
err error
}
// NewWalker creates a new Walker from the given initVal
func NewWalker(initVal Value) Walker {
return Walker{initialValue: initVal}
}
// Elt picks out the ith element of the current array
func (w Walker) Elt(i int) Walker {
if w.err != nil {
return w
}
if !w.latestValue.IsArray() {
w.err = fmt.Errorf("element at %s isn't an array", w.latestPath)
return w
}
// TODO
return w
}
// Path picks out the value pointed to by all the given path elements
// and puts it into the next value
func (w Walker) Path(pathElt PathElt, pathElts ...PathElt) Walker {
if w.err != nil {
return w
}
// TODO
return Walker{}
}
// Field picks out the key s in the current dictionary
func (w Walker) Field(s string) Walker {
if w.err != nil {
return w
}
return w.Path(NewStringPathElt(s))
}
// Validate validates the latest fetched value in w
func (w Walker) Validate(fn func(Value) bool) Walker {
if w.err != nil {
return w
}
if !fn(w.latestValue) {
w.err = fmt.Errorf("validation on path %s failed", w.latestPath)
return w
}
return w
}