Commit 28dd4925 authored by Eric Myhre's avatar Eric Myhre

Move Transform method to root package.

Upon a good night's sleep and reexamining the situation, it's extremely
clear that this function is *not* a fluent thing at all.  (Duh; look at
the Node types.)

A non-fluent Transform method has tons of virtue.  In many cases, we
might not actually need the fluent.Node helper features at all when
using Transform: since all of the intermediate error handling to
traverse to the reachedNode is already handled, that's half the battle;
and whether or we need to fight the ease-of-use battle on the interior
function is... *it depends*: if we use (codegen'd) native types, those
already address a lot of the error handling; fluent.Node can also be
used on the inside, but it's simply not needed in all cases.

We'll come back to the subject of writing the fluent equivalent later;
some example usage development is needed first so we know how to make
something that's actually helpful.
It will, of course, mostly be a wrapper; the interesting question is
whether a fluent.Transform function should use a fluent.Node either,
since this will make it harder to compose (unless we make fluent.Node
still embed ipld.Node?)... ahem.  As I said.  We'll come back to that.

One more quick comment about this relocation: I'm a little tepid about
putting the Transform method in the main package, because that really
puts it on-screen quite in the forefront.  If it's just Transform and
Traverse, maybe this is fine; but we end up implementing a large suite
of optics methods, this will become cluttery.  So, potentially this
will move to another sub-package again if that becomes an issue. (But
it still wouldn't be the fluent package.)
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent dd8ef869
package fluent
import (
ipld "github.com/ipld/go-ipld-prime"
)
func Traverse(
node ipld.Node,
path ipld.Path,
) (reachedNode ipld.Node, reachedPath ipld.Path) {
return TraverseUsingTraversal(node, path.Traverse)
}
func TraverseUsingTraversal(
node ipld.Node,
traversal ipld.Traversal,
) (reachedNode ipld.Node, reachedPath ipld.Path) {
return nil, ipld.Path{} // TODO this method doesn't have much to do except the error bouncing
}
package fluent
import (
ipld "github.com/ipld/go-ipld-prime"
)
package ipld
// Transform traverses an ipld.Node graph and applies a function
// to the reached node.
......@@ -30,13 +26,15 @@ import (
// if it so happens that those replacement elements are easiest to construct
// by regarding them as incremental updates to the previous values.
//
// Transform will panic with a fluent.Error if any intermediate operations
// error. (We are in the fluent package, after all.)
// Note that anything you can do with the Transform function, you can also
// do with regular Node and NodeBuilder usage directly. Transform just
// does a large amount of the intermediate bookkeeping that's useful when
// creating new values which are partial updates to existing values.
func Transform(
node ipld.Node,
path ipld.Path,
applicative func(targetNode ipld.Node) (targetReplacement ipld.Node),
) (nodeReplacement ipld.Node) {
node Node,
path Path,
applicative func(reachedNode Node, reachedPath Path) (reachedNodeReplacement Node),
) (nodeReplacement Node, err error) {
return TransformUsingTraversal(node, path.Traverse, applicative)
}
......@@ -44,9 +42,9 @@ func Transform(
// ipld.Traversal function instead of an ipld.Path for guiding its selection
// of a node to transform.
func TransformUsingTraversal(
node ipld.Node,
traversal ipld.Traversal,
applicative func(targetNode ipld.Node) (targetReplacement ipld.Node),
) (nodeReplacement ipld.Node) {
return nil // TODO
node Node,
traversal Traversal,
applicative func(reachedNode Node, reachedPath Path) (reachedNodeReplacement Node),
) (nodeReplacement Node, err error) {
panic("TODO") // TODO
}
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