Skip to content

Commit

Permalink
Impelement Segement.SelectLocated
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Dec 26, 2024
1 parent 7b5fb7d commit c2065c5
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 8 deletions.
30 changes: 30 additions & 0 deletions spec/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ func (s *Segment) Select(current, root any) []any {
return ret
}

// SelectLocated selects and returns values as [LocatedNode]s from current or
// root for each of seg's selectors. Defined by the [Selector] interface.
func (s *Segment) SelectLocated(current, root any, parent NormalizedPath) []*LocatedNode {
ret := []*LocatedNode{}
for _, sel := range s.selectors {
ret = append(ret, sel.SelectLocated(current, root, parent)...)
}
if s.descendant {
ret = append(ret, s.descendLocated(current, root, parent)...)
}
return ret
}

// descend recursively executes seg.Select for each value in current and/or
// root and returns the results.
func (s *Segment) descend(current, root any) []any {
Expand All @@ -77,6 +90,23 @@ func (s *Segment) descend(current, root any) []any {
return ret
}

// descend recursively executes seg.Select for each value in current and/or
// root and returns the results.
func (s *Segment) descendLocated(current, root any, parent NormalizedPath) []*LocatedNode {
ret := []*LocatedNode{}
switch val := current.(type) {
case []any:
for i, v := range val {
ret = append(ret, s.SelectLocated(v, root, append(parent, Index(i)))...)
}
case map[string]any:
for k, v := range val {
ret = append(ret, s.SelectLocated(v, root, append(parent, Name(k)))...)
}
}
return ret
}

// isSingular returns true if the segment selects at most one node. Defined by
// the [Selector] interface.
func (s *Segment) isSingular() bool {
Expand Down
Loading

0 comments on commit c2065c5

Please sign in to comment.