diff options
| author | Felix Hanley <felix@userspace.com.au> | 2018-11-21 02:54:33 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2018-11-21 02:54:33 +0000 |
| commit | 668a847465567bef7d77b7a47506ff09c0f951e2 (patch) | |
| tree | bb93e1917463ef051cd62c70d92e8199581f3a35 /json | |
| parent | 12f344f96a8225c93873aafa0f110babae267a0a (diff) | |
| download | query-668a847465567bef7d77b7a47506ff09c0f951e2.tar.gz query-668a847465567bef7d77b7a47506ff09c0f951e2.tar.bz2 | |
Add jsonpath and extract lexer
Diffstat (limited to 'json')
| -rw-r--r-- | json/node.go | 36 | ||||
| -rw-r--r-- | json/node_test.go | 8 |
2 files changed, 22 insertions, 22 deletions
diff --git a/json/node.go b/json/node.go index 56d0c64..c592969 100644 --- a/json/node.go +++ b/json/node.go @@ -47,6 +47,24 @@ func (n *Node) Type() base.NodeType { return base.NodeType(n.nodeType) } func (n *Node) DataType() string { return n.dataType } func (n *Node) Attr() []base.Attribute { return nil } +// Data gets the value of the node and all its child nodes. +func (n *Node) Data() string { + return n.data +} + +// InnerText gets the value of the node and all its child nodes. +func (n *Node) InnerText() string { + var buf bytes.Buffer + if n.nodeType == base.TextNode { + buf.WriteString(n.data) + } else { + for child := n.firstChild; child != nil; child = child.nextSibling { + buf.WriteString(child.InnerText()) + } + } + return buf.String() +} + func (n Node) String() string { return fmt.Sprintf("[%s] %s(%s)", base.NodeNames[n.nodeType], n.dataType, n.data) } @@ -70,24 +88,6 @@ func (n *Node) ChildNodes() []*Node { return a } -// Data gets the value of the node and all its child nodes. -func (n *Node) Data() string { - return n.data -} - -// InnerText gets the value of the node and all its child nodes. -func (n *Node) InnerText() string { - var buf bytes.Buffer - if n.nodeType == base.TextNode { - buf.WriteString(n.data) - } else { - for child := n.firstChild; child != nil; child = child.nextSibling { - buf.WriteString(child.InnerText()) - } - } - return buf.String() -} - // SelectElement finds the first of child elements with the // specified name. func (n *Node) SelectElement(name string) *Node { diff --git a/json/node_test.go b/json/node_test.go index 8a5e196..f321b9b 100644 --- a/json/node_test.go +++ b/json/node_test.go @@ -49,7 +49,7 @@ func TestParseJsonObject(t *testing.T) { // <city>New York</city> m := make(map[string]string) for _, n := range doc.ChildNodes() { - m[n.Data] = n.InnerText() + m[n.Data()] = n.InnerText() } expected := []struct { name, value string @@ -72,7 +72,7 @@ func TestParseJsonObjectArray(t *testing.T) { { "name":"Fiat", "models":[ "500", "Panda" ] } ]` doc, err := parseString(s) - doc.PrintTree(0) + //doc.PrintTree(0) if err != nil { t.Fatal(err) } @@ -104,7 +104,7 @@ func TestParseJsonObjectArray(t *testing.T) { var name string var models []string for _, e := range n.ChildNodes() { - if e.Data == "name" { + if e.data == "name" { // a name node. name = e.InnerText() } else { @@ -151,7 +151,7 @@ func TestParseJson(t *testing.T) { if n == nil { t.Fatal("n is nil") } - if n.NextSibling != nil { + if n.NextSibling() != nil { t.Fatal("next sibling shoud be nil") } if e, g := "John", n.InnerText(); e != g { |
