Skip to content

Commit 2a1d063

Browse files
Support for []string within ToString() function (#49)
* [add] Added support for []string on ToString() * [add] Included test case of issue #30
1 parent 1c6d7db commit 2a1d063

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

client_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,47 @@ func TestUtils(t *testing.T) {
429429
res = ToString(jsonMap)
430430
assert.Equal(t, res, "{object: {foo: 1}}")
431431
}
432+
433+
func TestNodeMapDatatype(t *testing.T) {
434+
graph.Flush()
435+
err := graph.Delete()
436+
assert.Nil(t, err)
437+
438+
// Create 2 nodes connect via a single edge.
439+
japan := NodeNew("Country", "j",
440+
map[string]interface{}{
441+
"name": "Japan",
442+
"population": 126800000,
443+
"states": []string{"Kanto", "Chugoku"},
444+
})
445+
john := NodeNew("Person", "p",
446+
map[string]interface{}{
447+
"name": "John Doe",
448+
"age": 33,
449+
"gender": "male",
450+
"status": "single",
451+
})
452+
edge := EdgeNew("Visited", john, japan, map[string]interface{}{"year": 2017})
453+
// Introduce entities to graph.
454+
graph.AddNode(john)
455+
graph.AddNode(japan)
456+
graph.AddEdge(edge)
457+
458+
// Flush graph to DB.
459+
res, err := graph.Commit()
460+
assert.Nil(t, err)
461+
assert.Equal(t, 2, res.NodesCreated(), "Expecting 2 node created")
462+
assert.Equal(t, 0, res.NodesDeleted(), "Expecting 0 nodes deleted")
463+
assert.Equal(t, 8, res.PropertiesSet(), "Expecting 8 properties set")
464+
assert.Equal(t, 1, res.RelationshipsCreated(), "Expecting 1 relationships created")
465+
assert.Equal(t, 0, res.RelationshipsDeleted(), "Expecting 0 relationships deleted")
466+
assert.Greater(t, res.InternalExecutionTime(), 0.0, "Expecting internal execution time not to be 0.0")
467+
assert.Equal(t, true, res.Empty(), "Expecting empty resultset")
468+
res, err = graph.Query("MATCH p = (:Person)-[:Visited]->(:Country) RETURN p")
469+
assert.Nil(t, err)
470+
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
471+
assert.Equal(t, false, res.Empty(), "Expecting resultset to have records")
472+
res, err = graph.Query("MATCH ()-[r]-() DELETE r")
473+
assert.Nil(t, err)
474+
assert.Equal(t, 1, res.RelationshipsDeleted(), "Expecting 1 relationships deleted")
475+
}

utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ func arrayToString(arr []interface{}) string {
1818
return "[" + strings.Join(strArray, ",") + "]"
1919
}
2020

21+
// go array to string is [1 2 3] for [1, 2, 3] array
22+
// cypher expects comma separated array
23+
func strArrayToString(arr []string) string {
24+
var arrayLength = len(arr)
25+
strArray := []string{}
26+
for i := 0; i < arrayLength; i++ {
27+
strArray = append(strArray, ToString(arr[i]))
28+
}
29+
return "[" + strings.Join(strArray, ",") + "]"
30+
}
31+
2132
func mapToString(data map[string]interface{}) string {
2233
pairsArray := []string{}
2334
for k, v := range data {
@@ -47,6 +58,9 @@ func ToString(i interface{}) string {
4758
case map[string]interface{}:
4859
data := i.(map[string]interface{})
4960
return mapToString(data)
61+
case []string:
62+
arr := i.([]string)
63+
return strArrayToString(arr)
5064
default:
5165
panic("Unrecognized type to convert to string")
5266
}

0 commit comments

Comments
 (0)