traverse.go 2.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package traversal

import (
	ipld "github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/traversal/selector"
)

func Traverse(n ipld.Node, s selector.Selector, fn VisitFn) error {
	return TraversalProgress{}.Traverse(n, s, fn)
}

func TraverseInformatively(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
	return TraversalProgress{}.TraverseInformatively(n, s, fn)
}

func TraverseTransform(n ipld.Node, s selector.Selector, fn TransformFn) (ipld.Node, error) {
	return TraversalProgress{}.TraverseTransform(n, s, fn)
}

func (tp TraversalProgress) Traverse(n ipld.Node, s selector.Selector, fn VisitFn) error {
21
	tp.init()
Eric Myhre's avatar
Eric Myhre committed
22 23
	return tp.traverseInformatively(n, s, func(tp TraversalProgress, n ipld.Node, tr TraversalReason) error {
		if tr != TraversalReason_SelectionMatch {
24 25 26 27 28 29 30
			return nil
		}
		return fn(tp, n)
	})
}

func (tp TraversalProgress) TraverseInformatively(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
Eric Myhre's avatar
Eric Myhre committed
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
	tp.init()
	return tp.traverseInformatively(n, s, fn)
}

func (tp TraversalProgress) traverseInformatively(n ipld.Node, s selector.Selector, fn AdvVisitFn) error {
	if s.Decide(n) {
		if err := fn(tp, n, TraversalReason_SelectionMatch); err != nil {
			return err
		}
	} else {
		if err := fn(tp, n, TraversalReason_SelectionCandidate); err != nil {
			return err
		}
	}
	nk := n.ReprKind()
	switch nk {
	case ipld.ReprKind_Map, ipld.ReprKind_List: // continue
	default:
		return nil
	}
	// TODO: should only do this full loop if high-cardinality indicated.
	//   attn := s.Interests()
	//   if attn == nil {
	// FIXME need another kind switch here, and list support!
	for itr := n.MapIterator(); !itr.Done(); {
		k, v, err := itr.Next()
		if err != nil {
			return err
		}
		kstr, _ := k.AsString()
		sNext := s.Explore(n, selector.PathSegmentString{kstr})
		if sNext != nil {
			// TODO when link load is implemented, it should go roughly here.
			tpNext := tp
			tpNext.Path = tp.Path.AppendSegment(kstr)
			if err := tpNext.traverseInformatively(v, sNext, fn); err != nil {
				return err
			}
		}
	}
	return nil
72 73 74 75 76
}

func (tp TraversalProgress) TraverseTransform(n ipld.Node, s selector.Selector, fn TransformFn) (ipld.Node, error) {
	panic("TODO")
}