Skip to content

Commit f30d765

Browse files
author
DvirDukhan
committed
added path support
1 parent 1b74aa6 commit f30d765

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ func main() {
8686
p_age, _ := r.Get("p.age")
8787
fmt.Printf("\nAge: %d\n", p_age)
8888
}
89+
90+
// Path matching example.
91+
query = "MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
92+
result, _ = graph.Query(query)
93+
res.Next()
94+
r := res.Record()
95+
p, ok := r.GetByIndex(0).(Path)
96+
fmt.Printf("%v", p.Encode())
8997
}
9098
```
9199

client_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,44 @@ func TestArray(t *testing.T) {
215215
assert.Equal(t, b.GetProperty("age"), resB.GetProperty("age"), "Unexpected property value.")
216216
assert.Equal(t, b.GetProperty("array"), resB.GetProperty("array"), "Unexpected property value.")
217217
}
218+
219+
func TestPath(t *testing.T) {
220+
createGraph()
221+
q := "MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
222+
res, err := graph.Query(q)
223+
if err != nil {
224+
t.Error(err)
225+
}
226+
227+
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
228+
229+
res.Next()
230+
r := res.Record()
231+
232+
p, ok := r.GetByIndex(0).(Path)
233+
assert.True(t, ok, "First column should contain path.")
234+
235+
assert.Equal(t, 2, p.NodesCount(), "Path should contain two nodes")
236+
assert.Equal(t, 1, p.EdgeCount(), "Path should contain one edge")
237+
238+
s := p.FirstNode()
239+
e := p.GetEdge(0)
240+
d := p.LastNode()
241+
242+
assert.Equal(t, s.Label, "Person", "Node should be of type 'Person'")
243+
assert.Equal(t, e.Relation, "Visited", "Edge should be of relation type 'Visited'")
244+
assert.Equal(t, d.Label, "Country", "Node should be of type 'Country'")
245+
246+
assert.Equal(t, len(s.Properties), 4, "Person node should have 4 properties")
247+
248+
assert.Equal(t, s.GetProperty("name"), "John Doe", "Unexpected property value.")
249+
assert.Equal(t, s.GetProperty("age"), 33, "Unexpected property value.")
250+
assert.Equal(t, s.GetProperty("gender"), "male", "Unexpected property value.")
251+
assert.Equal(t, s.GetProperty("status"), "single", "Unexpected property value.")
252+
253+
assert.Equal(t, e.GetProperty("year"), 2017, "Unexpected property value.")
254+
255+
assert.Equal(t, d.GetProperty("name"), "Japan", "Unexpected property value.")
256+
assert.Equal(t, d.GetProperty("population"), 126800000, "Unexpected property value.")
257+
258+
}

path.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package redisgraph
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type Path struct {
9+
Nodes []interface{}
10+
Edges []interface{}
11+
}
12+
13+
func PathNew(nodes []interface{}, edges []interface{}) Path {
14+
return Path{
15+
Nodes: nodes,
16+
Edges: edges,
17+
}
18+
}
19+
20+
func (p Path) GetNodes() []interface{} {
21+
return p.Nodes
22+
}
23+
24+
func (p Path) GetEdges() []interface{} {
25+
return p.Edges
26+
}
27+
28+
func (p Path) GetNode(index int) *Node {
29+
return p.Nodes[index].(*Node)
30+
}
31+
32+
func (p Path) GetEdge(index int) *Edge{
33+
return p.Edges[index].(*Edge);
34+
}
35+
36+
func (p Path) FirstNode() *Node {
37+
return p.GetNode(0)
38+
}
39+
40+
func (p Path) LastNode() *Node {
41+
return p.GetNode(p.NodesCount() - 1)
42+
}
43+
44+
func (p Path) NodesCount() int {
45+
return len(p.Nodes)
46+
}
47+
48+
func (p Path) EdgeCount() int {
49+
return len(p.Edges)
50+
}
51+
52+
func (p Path) Encode() string {
53+
s := []string{"<"}
54+
edgeCount := p.EdgeCount()
55+
for i := 0; i < edgeCount; i++ {
56+
var node = p.GetNode(i)
57+
s = append(s, "(" , fmt.Sprintf("%v", node.ID) , ")")
58+
var edge = p.GetEdge(i)
59+
if node.ID == edge.srcNodeID {
60+
s = append(s, "-[" , fmt.Sprintf("%v", edge.ID) , "]->")
61+
} else {
62+
s= append(s, "<-[" , fmt.Sprintf("%v", edge.ID) , "]-")
63+
}
64+
}
65+
s = append(s, "(" , fmt.Sprintf("%v", p.GetNode(edgeCount).ID) , ")")
66+
s = append(s, ">")
67+
68+
return strings.Join(s, "")
69+
}

query_result.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
VALUE_ARRAY
4242
VALUE_EDGE
4343
VALUE_NODE
44+
VALUE_PATH
4445
)
4546

4647
type QueryResultHeader struct {
@@ -217,6 +218,13 @@ func (qr *QueryResult) parseArray(cell interface{}) []interface{} {
217218
return array
218219
}
219220

221+
func (qr *QueryResult) parsePath(cell interface{}) Path {
222+
arrays := cell.([]interface{})
223+
nodes := qr.parseScalar(arrays[0].([]interface{}))
224+
edges := qr.parseScalar(arrays[1].([]interface{}))
225+
return PathNew(nodes.([]interface{}), edges.([]interface{}))
226+
}
227+
220228
func (qr *QueryResult) parseScalar(cell []interface{}) interface{} {
221229
t, _ := redis.Int(cell[0], nil)
222230
v := cell[1]
@@ -246,6 +254,9 @@ func (qr *QueryResult) parseScalar(cell []interface{}) interface{} {
246254
case VALUE_NODE:
247255
s = qr.parseNode(v)
248256

257+
case VALUE_PATH:
258+
s = qr.parsePath(v)
259+
249260
case VALUE_UNKNOWN:
250261
panic("Unknown scalar type\n")
251262
}

0 commit comments

Comments
 (0)