forked from mindstand/go-cypherdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
118 lines (93 loc) · 2.35 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package go_cypherdsl
import (
"errors"
"fmt"
"reflect"
"strings"
)
func cypherizeInterface(i interface{}) (string, error) {
if i == nil {
return "NULL", nil
}
//get interface kind
t := reflect.TypeOf(i)
k := t.Kind()
if t == reflect.TypeOf(ParamString("")) {
s := i.(ParamString)
return string(s), nil
}
if t == reflect.TypeOf(FuncString("")) {
s := i.(FuncString)
return string(s), nil
}
//check string
if k == reflect.String {
return fmt.Sprintf("'%s'", i.(string)), nil
}
//check if primitive numeric type
if k == reflect.Int || k == reflect.Int8 || k == reflect.Int16 || k == reflect.Int32 || k == reflect.Int64 ||
k == reflect.Uint || k == reflect.Uint8 || k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 ||
k == reflect.Float32 || k == reflect.Float64 {
return fmt.Sprintf("%v", i), nil
}
//check bool
if k == reflect.Bool {
return fmt.Sprintf("%t", i.(bool)), nil
}
if k == reflect.Slice {
q := ""
is, ok := i.([]interface{})
if !ok {
return "", errors.New("kind check failed, this should not have happened")
}
for _, iface := range is {
s, err := cypherizeInterface(iface)
if err != nil {
return "", nil
}
q += fmt.Sprintf("%s,", s)
}
return fmt.Sprintf("[%s]", strings.TrimSuffix(q, ",")), nil
}
return "", errors.New("invalid type " + k.String())
}
func RowsToStringArray(data [][]interface{}) ([]string, error) {
//check to make sure its not empty
if data == nil || len(data) == 0 || len(data[0]) == 0 {
return []string{}, nil
}
_, ok := data[0][0].(string)
if !ok {
return nil, errors.New("does not contain array of strings")
}
toReturn := make([]string, len(data))
for i, v := range data {
if len(v) == 0 {
return nil, errors.New("index %v is empty")
}
temp, ok := v[0].(string)
if !ok {
return nil, errors.New("unable to cast to string")
}
toReturn[i] = temp
}
return toReturn, nil
}
func RowsTo2dStringArray(data [][]interface{}) ([][]string, error) {
if len(data) != 0 && len(data[0]) != 0 {
toReturn := make([][]string, len(data))
var ok bool
for i, v := range data {
toReturn[i] = make([]string, len(v))
for j, v1 := range v {
toReturn[i][j], ok = v1.(string)
if !ok {
return nil, errors.New("failed to cast value to string")
}
}
}
return toReturn, nil
} else {
return [][]string{}, nil
}
}