Skip to content

Commit

Permalink
add groupQuery, supports grouping node query, (xpath expression). f…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Jun 3, 2021
1 parent cd2afb8 commit 50cf86e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ func (b *builder) processNode(root node) (q query, err error) {
q, err = b.processFunctionNode(root.(*functionNode))
case nodeOperator:
q, err = b.processOperatorNode(root.(*operatorNode))
case nodeGroup:
q, err = b.processNode(root.(*groupNode).Input)
if err != nil {
return
}
q = &groupQuery{Input: q}
}
return
}
Expand Down
18 changes: 18 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
nodeOperator
nodeVariable
nodeConstantOperand
nodeGroup
)

type parser struct {
Expand Down Expand Up @@ -104,6 +105,10 @@ func newFilterNode(n, m node) node {
return &filterNode{nodeType: nodeFilter, Input: n, Condition: m}
}

func newGroupNode(n node) node {
return &groupNode{nodeType: nodeGroup, Input: n}
}

// newRootNode returns a root node.
func newRootNode(s string) node {
return &rootNode{nodeType: nodeRoot, slash: s}
Expand Down Expand Up @@ -492,6 +497,9 @@ func (p *parser) parsePrimaryExpr(n node) (opnd node) {
case itemLParens:
p.next()
opnd = p.parseExpression(n)
if opnd.Type() != nodeConstantOperand {
opnd = newGroupNode(opnd)
}
p.skipItem(itemRParens)
case itemName:
if p.r.canBeFunc && !isNodeType(p.r) {
Expand Down Expand Up @@ -587,6 +595,16 @@ func (o *operandNode) String() string {
return fmt.Sprintf("%v", o.Val)
}

// groupNode holds a set of node expression
type groupNode struct {
nodeType
Input node
}

func (g *groupNode) String() string {
return fmt.Sprintf("%s", g.Input)
}

// filterNode holds a condition filter.
type filterNode struct {
nodeType
Expand Down
29 changes: 29 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,35 @@ func (c *constantQuery) Clone() query {
return c
}

type groupQuery struct {
posit int

Input query
}

func (g *groupQuery) Select(t iterator) NodeNavigator {
for {
node := g.Input.Select(t)
if node == nil {
return nil
}
g.posit++
return node.Copy()
}
}

func (g *groupQuery) Evaluate(t iterator) interface{} {
return g.Input.Evaluate(t)
}

func (g *groupQuery) Clone() query {
return &groupQuery{Input: g.Input}
}

func (g *groupQuery) position() int {
return g.posit
}

// logicalQuery is an XPath logical expression.
type logicalQuery struct {
Left, Right query
Expand Down

0 comments on commit 50cf86e

Please sign in to comment.