Skip to content

added support for string array #22

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
Nov 25, 2019
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
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestPath(t *testing.T) {

func TestParameterizedQuery(t *testing.T) {
createGraph()
params := []interface{}{1, 2.3, "str", true, false, nil, []interface {}{0, 1, 2}}
params := []interface{}{1, 2.3, "str", true, false, nil, []interface {}{0, 1, 2}, []interface {}{"0", "1", "2"}}
q := "RETURN $param"
params_map := make(map[string]interface{})
for index, param := range params {
Expand Down
2 changes: 1 addition & 1 deletion graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (g *Graph) CallProcedure(procedure string, yield []string, args ...interfac

tmp := make([]string, 0, len(args))
for arg := range args {
tmp = append(tmp, ToString(arg).(string))
tmp = append(tmp, ToString(arg))
}
q += fmt.Sprintf("%s)", strings.Join(tmp, ","))

Expand Down
47 changes: 21 additions & 26 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,41 @@ package redisgraph
import (
"crypto/rand"
"fmt"
"reflect"
"strings"
"strconv"
)

// go array to string is [1 2 3] for [1, 2, 3] array
// cypher expects comma separated array
func arrayToString(arr interface{}) interface{} {
v := reflect.ValueOf(arr)
var arrayLength = v.Len()
func arrayToString(arr []interface{}) string {
var arrayLength = len(arr)
strArray := []string{}
for i := 0; i < arrayLength; i++ {
strArray = append(strArray, fmt.Sprintf("%v", ToString(v.Index(i))))
strArray = append(strArray, ToString(arr[i]))
}
return "[" + strings.Join(strArray[:], ",") + "]"
return "[" + strings.Join(strArray, ",") + "]"
}

func ToString(i interface{}) interface{} {
func ToString(i interface{}) string {
if(i == nil) {
return "null"
}
v := reflect.ValueOf(i)
switch reflect.TypeOf(i).Kind() {
case reflect.String:
s := v.String()
if len(s) == 0 {
return "\"\""
}
if s[0] != '"' {
s = "\"" + s
}
if s[len(s)-1] != '"' {
s += "\""
}
return s
case reflect.Slice:
return arrayToString(i)
case reflect.Array:
return arrayToString(i)

switch i.(type) {
case string:
s := i.(string)
return strconv.Quote(s)
case int:
return strconv.Itoa(i.(int))
case float64:
return strconv.FormatFloat(i.(float64), 'f', -1, 64)
case bool:
return strconv.FormatBool(i.(bool))
case []interface {}:
arr := i.([]interface{})
return arrayToString(arr)
default:
return i
panic("Unrecognized type to convert to string")
}
}

Expand Down