Skip to content

Commit 3f36af3

Browse files
authored
Merge pull request #38 from RedisGraph/map-datatype
Add support for map datatype
2 parents aa0feaa + cdc830a commit 3f36af3

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

client_test.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,35 @@ func TestArray(t *testing.T) {
216216
assert.Equal(t, b.GetProperty("array"), resB.GetProperty("array"), "Unexpected property value.")
217217
}
218218

219+
func TestMap(t *testing.T) {
220+
createGraph()
221+
222+
q := "RETURN {val_1: 5, val_2: 'str', inner: {x: [1]}}"
223+
res, err := graph.Query(q)
224+
if err != nil {
225+
t.Error(err)
226+
}
227+
res.Next()
228+
r := res.Record()
229+
mapval := r.GetByIndex(0).(map[string]interface{})
230+
231+
inner_map := map[string]interface{}{"x": []interface{}{1}}
232+
expected := map[string]interface{}{"val_1": 5, "val_2": "str", "inner": inner_map}
233+
assert.Equal(t, mapval, expected, "expecting a map literal")
234+
235+
q = "MATCH (a:Country) RETURN a { .name }"
236+
res, err = graph.Query(q)
237+
if err != nil {
238+
t.Error(err)
239+
}
240+
res.Next()
241+
r = res.Record()
242+
mapval = r.GetByIndex(0).(map[string]interface{})
243+
244+
expected = map[string]interface{}{"name": "Japan"}
245+
assert.Equal(t, mapval, expected, "expecting a map projection")
246+
}
247+
219248
func TestPath(t *testing.T) {
220249
createGraph()
221250
q := "MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
@@ -228,7 +257,7 @@ func TestPath(t *testing.T) {
228257

229258
res.Next()
230259
r := res.Record()
231-
260+
232261
p, ok := r.GetByIndex(0).(Path)
233262
assert.True(t, ok, "First column should contain path.")
234263

query_result.go

+17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
VALUE_EDGE
4545
VALUE_NODE
4646
VALUE_PATH
47+
VALUE_MAP
4748
)
4849

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

231+
func (qr *QueryResult) parseMap(cell interface{}) map[string]interface{} {
232+
var raw_map = cell.([]interface{})
233+
var mapLength = len(raw_map)
234+
var parsed_map = make(map[string]interface{})
235+
236+
for i := 0; i < mapLength; i += 2 {
237+
key, _ := redis.String(raw_map[i], nil)
238+
parsed_map[key] = qr.parseScalar(raw_map[i+1].([]interface{}))
239+
}
240+
241+
return parsed_map
242+
}
243+
230244
func (qr *QueryResult) parseScalar(cell []interface{}) interface{} {
231245
t, _ := redis.Int(cell[0], nil)
232246
v := cell[1]
@@ -259,6 +273,9 @@ func (qr *QueryResult) parseScalar(cell []interface{}) interface{} {
259273
case VALUE_PATH:
260274
s = qr.parsePath(v)
261275

276+
case VALUE_MAP:
277+
s = qr.parseMap(v)
278+
262279
case VALUE_UNKNOWN:
263280
panic("Unknown scalar type\n")
264281
}

0 commit comments

Comments
 (0)