github.com/ipld/go-ipld-prime@v0.21.0/traversal/selector/exploreInterpretAs.go (about)

     1  package selector
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ipld/go-ipld-prime/datamodel"
     7  )
     8  
     9  type ExploreInterpretAs struct {
    10  	next Selector // selector for element we're interested in
    11  	adl  string   // reifier for the ADL we're interested in
    12  }
    13  
    14  // Interests for ExploreIndex is just the index specified by the selector node
    15  func (s ExploreInterpretAs) Interests() []datamodel.PathSegment {
    16  	return s.next.Interests()
    17  }
    18  
    19  // Explore returns the node's selector if
    20  // the path matches the index for this selector or nil if not
    21  func (s ExploreInterpretAs) Explore(n datamodel.Node, p datamodel.PathSegment) (Selector, error) {
    22  	return s.next, nil
    23  }
    24  
    25  // Decide always returns false because this is not a matcher
    26  func (s ExploreInterpretAs) Decide(n datamodel.Node) bool {
    27  	return false
    28  }
    29  
    30  // Match always returns false because this is not a matcher
    31  func (s ExploreInterpretAs) Match(node datamodel.Node) (datamodel.Node, error) {
    32  	return nil, nil
    33  }
    34  
    35  // NamedReifier indicates how this selector expects to Reify the current datamodel.Node.
    36  func (s ExploreInterpretAs) NamedReifier() string {
    37  	return s.adl
    38  }
    39  
    40  // Reifiable provides a feature detection interface on selectors to understand when
    41  // and if Reification of the datamodel.node should be attempted when performing traversals.
    42  type Reifiable interface {
    43  	NamedReifier() string
    44  }
    45  
    46  // ParseExploreInterpretAs assembles a Selector
    47  // from a ExploreInterpretAs selector node
    48  func (pc ParseContext) ParseExploreInterpretAs(n datamodel.Node) (Selector, error) {
    49  	if n.Kind() != datamodel.Kind_Map {
    50  		return nil, fmt.Errorf("selector spec parse rejected: selector body must be a map")
    51  	}
    52  	adlNode, err := n.LookupByString(SelectorKey_As)
    53  	if err != nil {
    54  		return nil, fmt.Errorf("selector spec parse rejected: the 'as' field must be present in ExploreInterpretAs clause")
    55  	}
    56  	next, err := n.LookupByString(SelectorKey_Next)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("selector spec parse rejected: the 'next' field must be present in ExploreInterpretAs clause")
    59  	}
    60  	selector, err := pc.ParseSelector(next)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	adl, err := adlNode.AsString()
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return ExploreInterpretAs{selector, adl}, nil
    70  }