-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
166 lines (140 loc) · 4.25 KB
/
example_test.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
package textselector_test
import (
"bytes"
"context"
"fmt"
"io"
"log"
"github.com/ipfs/go-cid"
mipld "github.com/ipfs/go-ipld-format"
mdag "github.com/ipfs/go-merkledag"
mdagtest "github.com/ipfs/go-merkledag/test"
// .../Raw must be imported to init() Raw-codec support
// Dag-PB is additionally configured runtime
dagpb "github.com/ipld/go-codec-dagpb"
_ "github.com/ipld/go-ipld-prime/codec/raw"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal"
textselector "github.com/ipld/go-ipld-selector-text-lite"
)
const pathSelector = "Links/1/Hash/Links/001/Hash"
const dagjsonSelector = `{"selector":{"f":{"f>":{"Links":{"f":{"f>":{"1":{"f":{"f>":{"Hash":{"f":{"f>":{"Links":{"f":{"f>":{"001":{"f":{"f>":{"Hash":{".":{}}}}}}}}}}}}}}}}}}}}}`
func Example_selectorFromPath() {
//
// Selector spec from a path, hopefully within that rootCid
// The 001 is deliberate: making sure index navigation still works
selectorSpec, err := textselector.SelectorSpecFromPath(pathSelector, false, nil)
if err != nil {
log.Fatal(err)
}
runSelector(selectorSpec.Node())
// Output:
// WantedNode
}
func Example_selectorFromDagjson() {
//
// Selector spec from a dag-json string, hopefully within that rootCid
// The spec should be the same as for the path version
selectorSpec, err := textselector.SelectorSpecFromJson(dagjsonSelector)
if err != nil {
log.Fatal(err)
}
runSelector(selectorSpec.Node())
// Output:
// WantedNode
}
func runSelector(selectorSpec datamodel.Node) {
//
// we put together a fixture datastore, and also return its root
ds, rootCid := fixtureDagService()
//
// this is how we R/O interact with the fixture DS
linkSystem := cidlink.DefaultLinkSystem()
linkSystem.StorageReadOpener = func(lctx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
if cl, isCid := lnk.(cidlink.Link); !isCid {
return nil, fmt.Errorf("unexpected link type %#v", lnk)
} else {
node, err := ds.Get(lctx.Ctx, cl.Cid)
if err != nil {
return nil, err
}
return bytes.NewBuffer(node.RawData()), nil
}
}
//
// this is what allows us to understand dagpb
nodePrototypeChooser := dagpb.AddSupportToChooser(
func(ipld.Link, ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype.Any, nil
},
)
//
// compile our selector from spec
parsedSelector, err := selector.ParseSelector(selectorSpec)
if err != nil {
log.Fatal(err)
}
//
// Not sure why this indirection exists TBH...
// Also trapping it in a struct ( twice ) is ugh
ctx := context.TODO()
linkContext := ipld.LinkContext{Ctx: ctx}
//
// this is how we pull the root node out of the DS
startNodePrototype, err := nodePrototypeChooser(cidlink.Link{Cid: rootCid}, linkContext)
if err != nil {
log.Fatal(err)
}
startNode, err := linkSystem.Load(
linkContext,
cidlink.Link{Cid: rootCid},
startNodePrototype,
)
if err != nil {
log.Fatal(err)
}
//
// this is executing the selector over the entire DS
err = traversal.Progress{
Cfg: &traversal.Config{
Ctx: ctx,
LinkSystem: linkSystem,
LinkTargetNodePrototypeChooser: nodePrototypeChooser,
},
}.WalkAdv(
startNode,
parsedSelector,
func(p traversal.Progress, n ipld.Node, r traversal.VisitReason) error {
if r == traversal.VisitReason_SelectionMatch {
content, _ := n.AsBytes()
fmt.Printf("%s\n", content)
}
return nil
},
)
if err != nil {
log.Fatal(err)
}
}
func fixtureDagService() (mipld.DAGService, cid.Cid) {
ds := mdagtest.Mock()
correct := mdag.NewRawNode([]byte("WantedNode"))
incorrect := mdag.NewRawNode([]byte("UnwantedNode"))
bothIncorrect := &mdag.ProtoNode{}
bothIncorrect.AddNodeLink("a", incorrect)
bothIncorrect.AddNodeLink("b", incorrect)
bIsCorrect := &mdag.ProtoNode{}
bIsCorrect.AddNodeLink("b", correct)
bIsCorrect.AddNodeLink("a", incorrect)
root := &mdag.ProtoNode{}
root.AddNodeLink("", bothIncorrect)
root.AddNodeLink("", bIsCorrect)
ds.AddMany(context.Background(), []mipld.Node{
correct, incorrect, bothIncorrect, bIsCorrect, root,
})
return ds, root.Cid()
}