matcher.go 1.43 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 15 16 17 18 19 20 21 22
)

// Matcher marks a node to be included in the "result" set.
// (All nodes traversed by a selector are in the "covered" set (which is a.k.a.
// "the merkle proof"); the "result" set is a subset of the "covered" set.)
//
// In libraries using selectors, the "result" set is typically provided to
// some user-specified callback.
//
// A selector tree with only "explore*"-type selectors and no Matcher selectors
// is valid; it will just generate a "covered" set of nodes and no "result" set.
// TODO: From spec: implement conditions and labels
type Matcher struct{}

// Interests are empty for a matcher (for now) because
// It is always just there to match, not explore further
tavit ohanian's avatar
tavit ohanian committed
23 24
func (s Matcher) Interests() []ld.PathSegment {
	return []ld.PathSegment{}
25 26 27
}

// Explore will return nil because a matcher is a terminal selector
tavit ohanian's avatar
tavit ohanian committed
28
func (s Matcher) Explore(n ld.Node, p ld.PathSegment) Selector {
29 30 31 32 33
	return nil
}

// Decide is always true for a match cause it's in the result set
// TODO: Implement boolean logic for conditionals
tavit ohanian's avatar
tavit ohanian committed
34
func (s Matcher) Decide(n ld.Node) bool {
35 36 37 38 39 40
	return true
}

// ParseMatcher assembles a Selector
// from a matcher selector node
// TODO: Parse labels and conditions
tavit ohanian's avatar
tavit ohanian committed
41 42
func (pc ParseContext) ParseMatcher(n ld.Node) (Selector, error) {
	if n.Kind() != ld.Kind_Map {
43 44 45 46
		return nil, fmt.Errorf("selector spec parse rejected: selector body must be a map")
	}
	return Matcher{}, nil
}