diff --git a/client_test.go b/client_test.go index 2aafc7c..9fbfc79 100644 --- a/client_test.go +++ b/client_test.go @@ -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" @@ -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.") diff --git a/query_result.go b/query_result.go index 1d048e3..e7abb23 100644 --- a/query_result.go +++ b/query_result.go @@ -44,6 +44,7 @@ const ( VALUE_EDGE VALUE_NODE VALUE_PATH + VALUE_MAP ) type QueryResultHeader struct { @@ -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] @@ -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") }