summaryrefslogtreecommitdiff
path: root/jsonpath
diff options
context:
space:
mode:
Diffstat (limited to 'jsonpath')
-rw-r--r--jsonpath/parser.go53
-rw-r--r--jsonpath/parser_test.go5
-rw-r--r--jsonpath/selector.go18
3 files changed, 35 insertions, 41 deletions
diff --git a/jsonpath/parser.go b/jsonpath/parser.go
index 5f0cd3a..3631f65 100644
--- a/jsonpath/parser.go
+++ b/jsonpath/parser.go
@@ -5,7 +5,7 @@ import (
"strconv"
"strings"
- "src.userspace.com.au/query/json"
+ base "src.userspace.com.au/query"
"src.userspace.com.au/query/lexer"
)
@@ -162,58 +162,52 @@ func (p *Parser) parsePredicateExprSelector() (Selector, error) {
}
// rootSelector checks node is root
-func rootSelector(n *json.Node) bool {
+func rootSelector(n base.Node) bool {
result := false
- if n.Parent != nil && n.Parent.Type == json.DocumentNode {
+ parent := n.Parent()
+ if parent != nil && parent.Type() == base.DocumentNode {
result = true
} else {
- result = (n.Type == json.DocumentNode)
+ result = (n.Type() == base.DocumentNode)
}
return result
}
// wildcardSelector returns true
-func wildcardSelector(n *json.Node) bool {
+func wildcardSelector(n base.Node) bool {
return true
}
// childSelector creates a selector for c being a child of p
func childSelector(p, c Selector) Selector {
- return func(n *json.Node) bool {
- /*
- for child := n.FirstChild; child != nil; child = child.NextSibling {
- if p(n) && c(child) {
- return true
- }
- }
- return false
- */
- result := (c(n) && n.Parent != nil && p(n.Parent))
+ return func(n base.Node) bool {
+ parent := n.Parent()
+ result := (c(n) && parent != nil && p(parent))
return result
}
}
// nameSelector generates selector for object key == k
func nameSelector(k string) Selector {
- return func(n *json.Node) bool {
- result := (n.Type == json.ElementNode && n.Data == k)
+ return func(n base.Node) bool {
+ result := (n.Type() == base.ElementNode && n.Data() == k)
return result
}
}
// recursiveSelector matches any node below which matches a
func recursiveSelector(a Selector) Selector {
- return func(n *json.Node) bool {
- if n.Type != json.ElementNode {
+ return func(n base.Node) bool {
+ if n.Type() != base.ElementNode {
return false
}
return hasRecursiveMatch(n, a)
}
}
-func hasRecursiveMatch(n *json.Node, a Selector) bool {
- for c := n.FirstChild; c != nil; c = c.NextSibling {
- if a(c) || (c.Type == json.ElementNode && hasRecursiveMatch(c, a)) {
+func hasRecursiveMatch(n base.Node, a Selector) bool {
+ for c := n.FirstChild(); c != nil; c = c.NextSibling() {
+ if a(c) || (c.Type() == base.ElementNode && hasRecursiveMatch(c, a)) {
return true
}
}
@@ -222,17 +216,18 @@ func hasRecursiveMatch(n *json.Node, a Selector) bool {
// arrayIndexSelector generates selector for node being idx index of parent
func arrayIndexSelector(idx int64) Selector {
- return func(n *json.Node) bool {
- if n.DataType != "arrayitem" {
+ return func(n base.Node) bool {
+ if n.DataType() != "arrayitem" {
return false
}
- if n.Parent == nil {
+ parent := n.Parent()
+
+ if parent == nil {
return false
}
- parent := n.Parent
i := int64(0)
- for c := parent.FirstChild; c != nil && i <= idx; c = c.NextSibling {
+ for c := parent.FirstChild(); c != nil && i <= idx; c = c.NextSibling() {
if i == idx && c == n {
return true
}
@@ -244,9 +239,9 @@ func arrayIndexSelector(idx int64) Selector {
// typeSelector matches a node with type t
func typeSelector(t string) Selector {
- return func(n *json.Node) bool {
+ return func(n base.Node) bool {
// FIXME
- if n.DataType == t {
+ if n.DataType() == t {
return true
}
return false
diff --git a/jsonpath/parser_test.go b/jsonpath/parser_test.go
index 2406651..dd86944 100644
--- a/jsonpath/parser_test.go
+++ b/jsonpath/parser_test.go
@@ -1,7 +1,6 @@
package jsonpath
import (
- //"fmt"
"strings"
"testing"
@@ -84,9 +83,9 @@ func TestParse(t *testing.T) {
}
if tt.expect == "" && actualText != "" {
- t.Fatalf("MatchFirst(%s) => %s, expected nothing", tt.src, actualText)
+ t.Fatalf("MatchFirst(%s, %s) => %s, expected nothing", tt.path, tt.src, actualText)
} else if actualText != tt.expect {
- t.Fatalf("MatchFirst(%s) => %s, expected %s", tt.src, actualText, tt.expect)
+ t.Fatalf("MatchFirst(%s, %s) => %s, expected %s", tt.path, tt.src, actualText, tt.expect)
}
}
}
diff --git a/jsonpath/selector.go b/jsonpath/selector.go
index 71c3ed2..4728566 100644
--- a/jsonpath/selector.go
+++ b/jsonpath/selector.go
@@ -1,23 +1,23 @@
package jsonpath
import (
- "src.userspace.com.au/query/json"
+ base "src.userspace.com.au/query"
)
-type Selector func(*json.Node) bool
+type Selector func(base.Node) bool
// MatchAll returns a slice of the nodes that match the selector,
// from n and its children.
-func (s Selector) MatchAll(n *json.Node) []*json.Node {
+func (s Selector) MatchAll(n base.Node) []base.Node {
return s.matchAllInto(n, nil)
}
-func (s Selector) matchAllInto(n *json.Node, storage []*json.Node) []*json.Node {
+func (s Selector) matchAllInto(n base.Node, storage []base.Node) []base.Node {
if s(n) {
storage = append(storage, n)
}
- for child := n.FirstChild; child != nil; child = child.NextSibling {
+ for child := n.FirstChild(); child != nil; child = child.NextSibling() {
storage = s.matchAllInto(child, storage)
}
@@ -25,17 +25,17 @@ func (s Selector) matchAllInto(n *json.Node, storage []*json.Node) []*json.Node
}
// Match returns true if the node matches the selector.
-func (s Selector) Match(n *json.Node) bool {
+func (s Selector) Match(n base.Node) bool {
return s(n)
}
// MatchFirst returns the first node that matches s, from n and its children.
-func (s Selector) MatchFirst(n *json.Node) *json.Node {
+func (s Selector) MatchFirst(n base.Node) base.Node {
if s.Match(n) {
return n
}
- for c := n.FirstChild; c != nil; c = c.NextSibling {
+ for c := n.FirstChild(); c != nil; c = c.NextSibling() {
m := s.MatchFirst(c)
if m != nil {
return m
@@ -45,7 +45,7 @@ func (s Selector) MatchFirst(n *json.Node) *json.Node {
}
// Filter returns the nodes in nodes that match the selector.
-func (s Selector) Filter(nodes []*json.Node) (result []*json.Node) {
+func (s Selector) Filter(nodes []base.Node) (result []base.Node) {
for _, n := range nodes {
if s(n) {
result = append(result, n)