Skip to content

Support for []string within ToString() function #49

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
Jun 2, 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
44 changes: 44 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,47 @@ func TestUtils(t *testing.T) {
res = ToString(jsonMap)
assert.Equal(t, res, "{object: {foo: 1}}")
}

func TestNodeMapDatatype(t *testing.T) {
graph.Flush()
err := graph.Delete()
assert.Nil(t, err)

// Create 2 nodes connect via a single edge.
japan := NodeNew("Country", "j",
map[string]interface{}{
"name": "Japan",
"population": 126800000,
"states": []string{"Kanto", "Chugoku"},
})
john := NodeNew("Person", "p",
map[string]interface{}{
"name": "John Doe",
"age": 33,
"gender": "male",
"status": "single",
})
edge := EdgeNew("Visited", john, japan, map[string]interface{}{"year": 2017})
// Introduce entities to graph.
graph.AddNode(john)
graph.AddNode(japan)
graph.AddEdge(edge)

// Flush graph to DB.
res, err := graph.Commit()
assert.Nil(t, err)
assert.Equal(t, 2, res.NodesCreated(), "Expecting 2 node created")
assert.Equal(t, 0, res.NodesDeleted(), "Expecting 0 nodes deleted")
assert.Equal(t, 8, res.PropertiesSet(), "Expecting 8 properties set")
assert.Equal(t, 1, res.RelationshipsCreated(), "Expecting 1 relationships created")
assert.Equal(t, 0, res.RelationshipsDeleted(), "Expecting 0 relationships deleted")
assert.Greater(t, res.InternalExecutionTime(), 0.0, "Expecting internal execution time not to be 0.0")
assert.Equal(t, true, res.Empty(), "Expecting empty resultset")
res, err = graph.Query("MATCH p = (:Person)-[:Visited]->(:Country) RETURN p")
assert.Nil(t, err)
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
assert.Equal(t, false, res.Empty(), "Expecting resultset to have records")
res, err = graph.Query("MATCH ()-[r]-() DELETE r")
assert.Nil(t, err)
assert.Equal(t, 1, res.RelationshipsDeleted(), "Expecting 1 relationships deleted")
}
14 changes: 14 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func arrayToString(arr []interface{}) string {
return "[" + strings.Join(strArray, ",") + "]"
}

// go array to string is [1 2 3] for [1, 2, 3] array
// cypher expects comma separated array
func strArrayToString(arr []string) string {
var arrayLength = len(arr)
strArray := []string{}
for i := 0; i < arrayLength; i++ {
strArray = append(strArray, ToString(arr[i]))
}
return "[" + strings.Join(strArray, ",") + "]"
}

func mapToString(data map[string]interface{}) string {
pairsArray := []string{}
for k, v := range data {
Expand Down Expand Up @@ -47,6 +58,9 @@ func ToString(i interface{}) string {
case map[string]interface{}:
data := i.(map[string]interface{})
return mapToString(data)
case []string:
arr := i.([]string)
return strArrayToString(arr)
default:
panic("Unrecognized type to convert to string")
}
Expand Down