/
exploreIndex.go
63 lines (56 loc) · 2 KB
/
exploreIndex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
package selector
import (
"fmt"
ipld "github.com/ipld/go-ipld-prime"
)
// ExploreIndex traverses a specific index in a list, and applies a next
// selector to the reached node.
type ExploreIndex struct {
next Selector // selector for element we're interested in
interest [1]ipld.PathSegment // index of element we're interested in
}
// Interests for ExploreIndex is just the index specified by the selector node
func (s ExploreIndex) Interests() []ipld.PathSegment {
return s.interest[:]
}
// Explore returns the node's selector if
// the path matches the index for this selector or nil if not
func (s ExploreIndex) Explore(n ipld.Node, p ipld.PathSegment) Selector {
if n.Kind() != ipld.Kind_List {
return nil
}
expectedIndex, expectedErr := p.Index()
actualIndex, actualErr := s.interest[0].Index()
if expectedErr != nil || actualErr != nil || expectedIndex != actualIndex {
return nil
}
return s.next
}
// Decide always returns false because this is not a matcher
func (s ExploreIndex) Decide(n ipld.Node) bool {
return false
}
// ParseExploreIndex assembles a Selector
// from a ExploreIndex selector node
func (pc ParseContext) ParseExploreIndex(n ipld.Node) (Selector, error) {
if n.Kind() != ipld.Kind_Map {
return nil, fmt.Errorf("selector spec parse rejected: selector body must be a map")
}
indexNode, err := n.LookupByString(SelectorKey_Index)
if err != nil {
return nil, fmt.Errorf("selector spec parse rejected: index field must be present in ExploreIndex selector")
}
indexValue, err := indexNode.AsInt()
if err != nil {
return nil, fmt.Errorf("selector spec parse rejected: index field must be a number in ExploreIndex selector")
}
next, err := n.LookupByString(SelectorKey_Next)
if err != nil {
return nil, fmt.Errorf("selector spec parse rejected: next field must be present in ExploreIndex selector")
}
selector, err := pc.ParseSelector(next)
if err != nil {
return nil, err
}
return ExploreIndex{selector, [1]ipld.PathSegment{ipld.PathSegmentOfInt(indexValue)}}, nil
}