Commit e798ae8c authored by Daniel Martí's avatar Daniel Martí

node/basic: add Chooser

Fixes #185.
parent ccb9eea4
......@@ -11,6 +11,20 @@ var (
//_ ipld.NodeAssembler = &anyAssembler{}
)
// Note that we don't use a "var _" declaration to assert that Chooser
// implements traversal.LinkTargetNodePrototypeChooser, to keep basicnode's
// dependencies fairly light.
// Chooser implements traversal.LinkTargetNodePrototypeChooser.
//
// It can be used directly when loading links into the "any" prototype,
// or with another chooser layer on top, such as:
//
// prototypeChooser := dagpb.AddSupportToChooser(basicnode.Chooser)
func Chooser(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return Prototype__Any{}, nil
}
// anyNode is a union meant for alloc amortization; see anyAssembler.
// Note that anyBuilder doesn't use anyNode, because it's not aiming to amortize anything.
//
......
......@@ -136,10 +136,8 @@ func TestFocusWithLinkLoading(t *testing.T) {
lsys.StorageReadOpener = (&store).OpenRead
err := traversal.Progress{
Cfg: &traversal.Config{
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: func(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype__Any{}, nil
},
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: basicnode.Chooser,
},
}.Focus(rootNode, ipld.ParsePath("linkedMap/nested/nonlink"), func(prog traversal.Progress, n ipld.Node) error {
Wish(t, n, ShouldEqual, basicnode.NewString("zoo"))
......@@ -168,10 +166,8 @@ func TestGetWithLinkLoading(t *testing.T) {
lsys.StorageReadOpener = (&store).OpenRead
n, err := traversal.Progress{
Cfg: &traversal.Config{
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: func(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype__Any{}, nil
},
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: basicnode.Chooser,
},
}.Get(rootNode, ipld.ParsePath("linkedMap/nested/nonlink"))
Wish(t, err, ShouldEqual, nil)
......@@ -312,10 +308,8 @@ func TestFocusedTransformWithLinks(t *testing.T) {
lsys.StorageReadOpener = (&store).OpenRead
lsys.StorageWriteOpener = (&store2).OpenWrite
cfg := traversal.Config{
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: func(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype.Any, nil
},
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: basicnode.Chooser,
}
t.Run("UpdateMapBeyondLink", func(t *testing.T) {
n, err := traversal.Progress{
......
......@@ -122,10 +122,8 @@ func TestWalkMatching(t *testing.T) {
lsys.StorageReadOpener = (&store).OpenRead
err = traversal.Progress{
Cfg: &traversal.Config{
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: func(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype__Any{}, nil
},
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: basicnode.Chooser,
},
}.WalkMatching(middleMapNode, s, func(prog traversal.Progress, n ipld.Node) error {
switch order {
......@@ -168,10 +166,8 @@ func TestWalkMatching(t *testing.T) {
lsys.StorageReadOpener = (&store).OpenRead
err = traversal.Progress{
Cfg: &traversal.Config{
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: func(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype__Any{}, nil
},
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: basicnode.Chooser,
},
}.WalkMatching(middleListNode, s, func(prog traversal.Progress, n ipld.Node) error {
switch order {
......@@ -213,10 +209,8 @@ func TestWalkMatching(t *testing.T) {
lsys.StorageReadOpener = (&store).OpenRead
err = traversal.Progress{
Cfg: &traversal.Config{
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: func(_ ipld.Link, _ ipld.LinkContext) (ipld.NodePrototype, error) {
return basicnode.Prototype__Any{}, nil
},
LinkSystem: lsys,
LinkTargetNodePrototypeChooser: basicnode.Chooser,
},
}.WalkMatching(rootNode, s, func(prog traversal.Progress, n ipld.Node) error {
switch order {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment