exploreAll.go 1.23 KB
Newer Older
1 2 3 4 5
package selector

import (
	"fmt"

tavit ohanian's avatar
tavit ohanian committed
6
	ld "gitlab.dms3.io/ld/go-ld-prime"
7 8 9 10 11 12 13 14
)

// ExploreAll is similar to a `*` -- it traverses all elements of an array,
// or all entries in a map, and applies a next selector to the reached nodes.
type ExploreAll struct {
	next Selector // selector for element we're interested in
}

hannahhoward's avatar
hannahhoward committed
15
// Interests for ExploreAll is nil (meaning traverse everything)
tavit ohanian's avatar
tavit ohanian committed
16
func (s ExploreAll) Interests() []ld.PathSegment {
17 18 19 20
	return nil
}

// Explore returns the node's selector for all fields
tavit ohanian's avatar
tavit ohanian committed
21
func (s ExploreAll) Explore(n ld.Node, p ld.PathSegment) Selector {
22 23 24 25
	return s.next
}

// Decide always returns false because this is not a matcher
tavit ohanian's avatar
tavit ohanian committed
26
func (s ExploreAll) Decide(n ld.Node) bool {
27 28 29 30
	return false
}

// ParseExploreAll assembles a Selector from a ExploreAll selector node
tavit ohanian's avatar
tavit ohanian committed
31 32
func (pc ParseContext) ParseExploreAll(n ld.Node) (Selector, error) {
	if n.Kind() != ld.Kind_Map {
33 34
		return nil, fmt.Errorf("selector spec parse rejected: selector body must be a map")
	}
35
	next, err := n.LookupByString(SelectorKey_Next)
36 37 38
	if err != nil {
		return nil, fmt.Errorf("selector spec parse rejected: next field must be present in ExploreAll selector")
	}
39
	selector, err := pc.ParseSelector(next)
40 41 42 43 44
	if err != nil {
		return nil, err
	}
	return ExploreAll{selector}, nil
}