fns.go 2.26 KB
Newer Older
1 2 3 4 5 6 7 8
package traversal

import (
	"context"

	ipld "github.com/ipld/go-ipld-prime"
)

9 10 11
// This file defines interfaces for things users provide.
//------------------------------------------------

12 13 14 15 16 17 18
// VisitFn is a read-only visitor.
type VisitFn func(TraversalProgress, ipld.Node) error

// TransformFn is like a visitor that can also return a new Node to replace the visited one.
type TransformFn func(TraversalProgress, ipld.Node) (ipld.Node, error)

// AdvVisitFn is like VisitFn, but for use with AdvTraversal: it gets additional arguments describing *why* this node is visited.
19
type AdvVisitFn func(TraversalProgress, ipld.Node, TraversalReason) error
20 21 22 23 24

// TraversalReason provides additional information to traversals using AdvVisitFn.
type TraversalReason byte // enum = SelectionMatch | SelectionParent | SelectionCandidate // probably only pointful for block edges?

type TraversalProgress struct {
25
	Cfg       *TraversalConfig
Eric Myhre's avatar
Eric Myhre committed
26
	Path      ipld.Path // Path is how we reached the current point in the traversal.
27
	LastBlock struct {  // LastBlock stores the Path and Link of the last block edge we had to load.  (It will always be zero in traversals with no linkloader.)
28 29
		Path ipld.Path
		Link ipld.Link
30 31 32 33
	}
}

type TraversalConfig struct {
34 35 36 37
	Ctx                    context.Context    // Context carried through a traversal.  Optional; use it if you need cancellation.
	LinkLoader             ipld.Loader        // Loader used for automatic link traversal.
	LinkNodeBuilderChooser NodeBuilderChooser // Chooser for Node implementations to produce during automatic link traversal.
	LinkStorer             ipld.Storer        // Storer used if any mutation features (e.g. traversal.Transform) are used.
38
}
39 40 41 42 43 44 45 46 47 48 49

// NodeBuilderChooser is a function that returns a NodeBuilder based on
// the information in a Link its LinkContext.
//
// A NodeBuilderChooser can be used in a TraversalConfig to be clear about
// what kind of Node implementation to use when loading a Link.
// In a simple example, it could constantly return an `ipldfree.NodeBuilder`.
// In a more complex example, a program using `bind` over native Go types
// could decide what kind of native type is expected, and return a
// `bind.NodeBuilder` for that specific concrete native type.
type NodeBuilderChooser func(ipld.Link, ipld.LinkContext) ipld.NodeBuilder