-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
76 lines (62 loc) · 1.43 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
package main
import "reflect"
func contains(key string, slice []string) bool {
for _, v := range slice {
if v == key {
return true
}
}
return false
}
func extractKeys(data []map[string]interface{}) (res []string) {
for _, m := range data {
for k, _ := range m {
if !contains(k, res) {
res = append(res, k)
}
}
}
return res
}
func quoteForDriver(driver string) string {
if driver == "mysql" {
return "`"
}
return `"`
}
func surroundKeysWithQuotes(keys []string, driver string) []string {
res := make([]string, len(keys))
quote := quoteForDriver(driver)
for i, key := range keys {
res[i] = quote + key + quote
}
return res
}
func surroundKeyWithQuote(key, driver string) string {
quote := quoteForDriver(driver)
return quote + key + quote
}
// from stretchr/testify/assert/assertions
func ObjectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
if reflect.DeepEqual(expected, actual) {
return true
}
return false
}
func ObjectsAreEqualValues(expected, actual interface{}) bool {
if ObjectsAreEqual(expected, actual) {
return true
}
actualType := reflect.TypeOf(actual)
expectedValue := reflect.ValueOf(expected)
if expectedValue.Type().ConvertibleTo(actualType) {
// Attempt comparison after type conversion
if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) {
return true
}
}
return false
}