-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added path support #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package redisgraph | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Path struct { | ||
Nodes []*Node | ||
Edges []*Edge | ||
} | ||
|
||
func PathNew(nodes []interface{}, edges []interface{}) Path { | ||
Nodes := make([]*Node, len(nodes)) | ||
for i := 0; i < len(nodes); i++ { | ||
Nodes[i] = nodes[i].(*Node) | ||
} | ||
Edges := make([]*Edge, len(edges)) | ||
for i := 0; i < len(edges); i++ { | ||
Edges[i] = edges[i].(*Edge) | ||
} | ||
|
||
return Path{ | ||
Edges : Edges, | ||
Nodes : Nodes, | ||
} | ||
} | ||
|
||
func (p Path) GetNodes() []*Node { | ||
return p.Nodes | ||
} | ||
|
||
func (p Path) GetEdges() []*Edge { | ||
return p.Edges | ||
} | ||
|
||
func (p Path) GetNode(index int) *Node { | ||
return p.Nodes[index] | ||
} | ||
|
||
func (p Path) GetEdge(index int) *Edge{ | ||
return p.Edges[index] | ||
} | ||
|
||
func (p Path) FirstNode() *Node { | ||
return p.GetNode(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the Path is empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. execption |
||
} | ||
|
||
func (p Path) LastNode() *Node { | ||
return p.GetNode(p.NodesCount() - 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if NodeCount is 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exception |
||
} | ||
|
||
func (p Path) NodesCount() int { | ||
return len(p.Nodes) | ||
} | ||
|
||
func (p Path) EdgeCount() int { | ||
return len(p.Edges) | ||
} | ||
|
||
func (p Path) String() string { | ||
s := []string{"<"} | ||
edgeCount := p.EdgeCount() | ||
for i := 0; i < edgeCount; i++ { | ||
var node = p.GetNode(i) | ||
s = append(s, "(" , fmt.Sprintf("%v", node.ID) , ")") | ||
var edge = p.GetEdge(i) | ||
if node.ID == edge.srcNodeID { | ||
s = append(s, "-[" , fmt.Sprintf("%v", edge.ID) , "]->") | ||
} else { | ||
s= append(s, "<-[" , fmt.Sprintf("%v", edge.ID) , "]-") | ||
} | ||
} | ||
s = append(s, "(" , fmt.Sprintf("%v", p.GetNode(edgeCount).ID) , ")") | ||
s = append(s, ">") | ||
|
||
return strings.Join(s, "") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ const ( | |
VALUE_ARRAY | ||
VALUE_EDGE | ||
VALUE_NODE | ||
VALUE_PATH | ||
) | ||
|
||
type QueryResultHeader struct { | ||
|
@@ -217,6 +218,13 @@ func (qr *QueryResult) parseArray(cell interface{}) []interface{} { | |
return array | ||
} | ||
|
||
func (qr *QueryResult) parsePath(cell interface{}) Path { | ||
arrays := cell.([]interface{}) | ||
nodes := qr.parseScalar(arrays[0].([]interface{})) | ||
edges := qr.parseScalar(arrays[1].([]interface{})) | ||
return PathNew(nodes.([]interface{}), edges.([]interface{})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be an array |
||
} | ||
|
||
func (qr *QueryResult) parseScalar(cell []interface{}) interface{} { | ||
t, _ := redis.Int(cell[0], nil) | ||
v := cell[1] | ||
|
@@ -246,6 +254,9 @@ func (qr *QueryResult) parseScalar(cell []interface{}) interface{} { | |
case VALUE_NODE: | ||
s = qr.parseNode(v) | ||
|
||
case VALUE_PATH: | ||
s = qr.parsePath(v) | ||
|
||
case VALUE_UNKNOWN: | ||
panic("Unknown scalar type\n") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func PathNew(nodes []Node, edges []Edge) Path {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the constructor will convert from array of interface to array of node/edge