Commit 64bf8a21 authored by hannahhoward's avatar hannahhoward

feat(type): add nodebuilder hint to typed links

Add the concept of a suggested node builder to links that are a reference to a specific typed node
that has a code-gen'd node builder
parent 907fb144
package typed
import "github.com/ipld/go-ipld-prime"
// typed.Link is a superset of the ipld.Link interface, and has one additional behavior.
//
// A typed.Link contains a hint for the appropriate node builder to use for loading data
// on the other side of a link, so that it can be assembled into a node representation
// and validated against the schema as quickly as possible
//
// So, for example, if you wanted to support loading the other side of a link
// with a code-gen'd node builder while utilizing the automatic loading facilities
// of the traversal package, you could write a LinkNodeBuilderChooser as follows:
//
// func LinkNodeBuilderChooser(lnk ipld.Link, lnkCtx ipld.LinkContext) ipld.NodeBuilder {
// if tlnk, ok := lnk.(typed.Link); ok {
// return tlnk.SuggestedNodeBuilder()
// }
// return ipldfree.NodeBuilder()
// }
//
type Link interface {
SuggestedNodeBuilder() ipld.NodeBuilder
}
......@@ -7,6 +7,7 @@ import (
ipld "github.com/ipld/go-ipld-prime"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
"github.com/ipld/go-ipld-prime/impl/typed"
)
// init sets all the values in TraveralConfig to reasonable defaults
......@@ -21,7 +22,10 @@ func (tc *Config) init() {
}
}
if tc.LinkNodeBuilderChooser == nil {
tc.LinkNodeBuilderChooser = func(ipld.Link, ipld.LinkContext) ipld.NodeBuilder {
tc.LinkNodeBuilderChooser = func(lnk ipld.Link, lnkCtx ipld.LinkContext) ipld.NodeBuilder {
if tlnk, ok := lnk.(typed.Link); ok {
return tlnk.SuggestedNodeBuilder()
}
return ipldfree.NodeBuilder()
}
}
......
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