Skip to content

Add support for map datatype #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

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,35 @@ func TestArray(t *testing.T) {
assert.Equal(t, b.GetProperty("array"), resB.GetProperty("array"), "Unexpected property value.")
}

func TestMap(t *testing.T) {
createGraph()

q := "RETURN {val_1: 5, val_2: 'str', inner: {x: [1]}}"
res, err := graph.Query(q)
if err != nil {
t.Error(err)
}
res.Next()
r := res.Record()
mapval := r.GetByIndex(0).(map[string]interface{})

inner_map := map[string]interface{}{"x": []interface{}{1}}
expected := map[string]interface{}{"val_1": 5, "val_2": "str", "inner": inner_map}
assert.Equal(t, mapval, expected, "expecting a map literal")

q = "MATCH (a:Country) RETURN a { .name }"
res, err = graph.Query(q)
if err != nil {
t.Error(err)
}
res.Next()
r = res.Record()
mapval = r.GetByIndex(0).(map[string]interface{})

expected = map[string]interface{}{"name": "Japan"}
assert.Equal(t, mapval, expected, "expecting a map projection")
}

func TestPath(t *testing.T) {
createGraph()
q := "MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
Expand All @@ -228,7 +257,7 @@ func TestPath(t *testing.T) {

res.Next()
r := res.Record()

p, ok := r.GetByIndex(0).(Path)
assert.True(t, ok, "First column should contain path.")

Expand Down
17 changes: 17 additions & 0 deletions query_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
VALUE_EDGE
VALUE_NODE
VALUE_PATH
VALUE_MAP
)

type QueryResultHeader struct {
Expand Down Expand Up @@ -227,6 +228,19 @@ func (qr *QueryResult) parsePath(cell interface{}) Path {
return PathNew(nodes.([]interface{}), edges.([]interface{}))
}

func (qr *QueryResult) parseMap(cell interface{}) map[string]interface{} {
var raw_map = cell.([]interface{})
var mapLength = len(raw_map)
var parsed_map = make(map[string]interface{})

for i := 0; i < mapLength; i += 2 {
key, _ := redis.String(raw_map[i], nil)
parsed_map[key] = qr.parseScalar(raw_map[i+1].([]interface{}))
}

return parsed_map
}

func (qr *QueryResult) parseScalar(cell []interface{}) interface{} {
t, _ := redis.Int(cell[0], nil)
v := cell[1]
Expand Down Expand Up @@ -259,6 +273,9 @@ func (qr *QueryResult) parseScalar(cell []interface{}) interface{} {
case VALUE_PATH:
s = qr.parsePath(v)

case VALUE_MAP:
s = qr.parseMap(v)

case VALUE_UNKNOWN:
panic("Unknown scalar type\n")
}
Expand Down