Skip to content

Commit

Permalink
changed toString function
Browse files Browse the repository at this point in the history
  • Loading branch information
DvirDukhan committed Nov 25, 2019
1 parent 908e451 commit 11fc22a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
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
34 changes: 16 additions & 18 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,40 @@ import (
"crypto/rand"
"fmt"
"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 := arr.([]interface{})
var arrayLength = len(v)
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[i])))
strArray = append(strArray, fmt.Sprintf("%v", ToString(arr[i])))
}
return "[" + strings.Join(strArray[:], ",") + "]"
}

func ToString(i interface{}) interface{} {
func ToString(i interface{}) string {
if(i == nil) {
return "null"
}

switch i.(type) {
case string:
s := i.(string)
if len(s) == 0 {
return "\"\""
}
if s[0] != '"' {
s = "\"" + s
}
if s[len(s)-1] != '"' {
s += "\""
}
return s
case []interface {} :
return arrayToString(i)
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

0 comments on commit 11fc22a

Please sign in to comment.