-
Notifications
You must be signed in to change notification settings - Fork 50
/
walk.go
150 lines (135 loc) · 3.6 KB
/
walk.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
package traversal
import (
"fmt"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/traversal/selector"
)
func WalkMatching(n ipld.Node, s selector.Selector, fn VisitFn) error {
return Progress{}.WalkMatching(n, s, fn)
}
func WalkAdv(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
return Progress{}.WalkAdv(n, s, fn)
}
func WalkTransforming(n ipld.Node, s selector.Selector, fn TransformFn) (ipld.Node, error) {
return Progress{}.WalkTransforming(n, s, fn)
}
func (prog Progress) WalkMatching(n ipld.Node, s selector.Selector, fn VisitFn) error {
prog.init()
return prog.walkAdv(n, s, func(prog Progress, n ipld.Node, tr VisitReason) error {
if tr != VisitReason_SelectionMatch {
return nil
}
return fn(prog, n)
})
}
func (prog Progress) WalkAdv(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
prog.init()
return prog.walkAdv(n, s, fn)
}
func (prog Progress) walkAdv(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
if s.Decide(n) {
if err := fn(prog, n, VisitReason_SelectionMatch); err != nil {
return err
}
} else {
if err := fn(prog, n, VisitReason_SelectionCandidate); err != nil {
return err
}
}
nk := n.ReprKind()
switch nk {
case ipld.ReprKind_Map, ipld.ReprKind_List: // continue
default:
return nil
}
attn := s.Interests()
if attn == nil {
return prog.walkAdv_iterateAll(n, s, fn)
}
return prog.walkAdv_iterateSelective(n, attn, s, fn)
}
func (prog Progress) walkAdv_iterateAll(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
for itr := selector.NewSegmentIterator(n); !itr.Done(); {
ps, v, err := itr.Next()
if err != nil {
return err
}
sNext := s.Explore(n, ps)
if sNext != nil {
progNext := prog
progNext.Path = prog.Path.AppendSegment(ps)
if v.ReprKind() == ipld.ReprKind_Link {
lnk, _ := v.AsLink()
progNext.LastBlock.Path = progNext.Path
progNext.LastBlock.Link = lnk
v, err = progNext.loadLink(v, n)
if err != nil {
return err
}
}
err = progNext.walkAdv(v, sNext, fn)
if err != nil {
return err
}
}
}
return nil
}
func (prog Progress) walkAdv_iterateSelective(n ipld.Node, attn []ipld.PathSegment, s selector.Selector, fn AdvVisitFn) error {
for _, ps := range attn {
v, err := n.LookupSegment(ps)
if err != nil {
continue
}
sNext := s.Explore(n, ps)
if sNext != nil {
progNext := prog
progNext.Path = prog.Path.AppendSegment(ps)
if v.ReprKind() == ipld.ReprKind_Link {
lnk, _ := v.AsLink()
progNext.LastBlock.Path = progNext.Path
progNext.LastBlock.Link = lnk
v, err = progNext.loadLink(v, n)
if err != nil {
return err
}
}
err = progNext.walkAdv(v, sNext, fn)
if err != nil {
return err
}
}
}
return nil
}
func (prog Progress) loadLink(v ipld.Node, parent ipld.Node) (ipld.Node, error) {
lnk, err := v.AsLink()
if err != nil {
return nil, err
}
// Assemble the LinkContext in case the Loader or NBChooser want it.
lnkCtx := ipld.LinkContext{
LinkPath: prog.Path,
LinkNode: v,
ParentNode: parent,
}
// Pick what in-memory format we will build.
nb, err := prog.Cfg.LinkNodeBuilderChooser(lnk, lnkCtx)
if err != nil {
return nil, fmt.Errorf("error traversing node at %q: could not load link %q: %s", prog.Path, lnk, err)
}
// Load link!
v, err = lnk.Load(
prog.Cfg.Ctx,
lnkCtx,
nb,
prog.Cfg.LinkLoader,
)
if err != nil {
return nil, fmt.Errorf("error traversing node at %q: could not load link %q: %s", prog.Path, lnk, err)
}
return v, nil
}
func (prog Progress) WalkTransforming(n ipld.Node, s selector.Selector, fn TransformFn) (ipld.Node, error) {
panic("TODO")
}