-
Notifications
You must be signed in to change notification settings - Fork 7
/
utilities.go
214 lines (196 loc) · 5.28 KB
/
utilities.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package utility
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"math/rand"
"reflect"
"strconv"
"strings"
"time"
)
// IsEqualJSON returns true if the 2 supplied strings contain JSON data
// that is semantically equal.
func IsEqualJSON(s1, s2 string) (bool, error) {
var o1 interface{}
var o2 interface{}
var err error
err = json.Unmarshal([]byte(s1), &o1)
if err != nil {
return false, fmt.Errorf("Error unmarshalling string 1 :: %s", err.Error())
}
err = json.Unmarshal([]byte(s2), &o2)
if err != nil {
return false, fmt.Errorf("Error unmarshalling string 2 :: %s", err.Error())
}
o1 = deepConvertRFC3339(o1)
o2 = deepConvertRFC3339(o2)
return reflect.DeepEqual(o1, o2), nil
}
func deepConvertRFC3339(o interface{}) interface{} {
// detect strings that are timestamps
s, ok := o.(string)
if ok {
t, err := time.Parse(time.RFC3339, s)
if err == nil {
// convert all timestamps to UTC so different timezones won't mess up the comparison
return t.UTC()
}
return s
}
// traverse maps
m, ok := o.(map[string]interface{})
if ok {
for key, value := range m {
m[key] = deepConvertRFC3339(value)
}
return m
}
// traverse slices
a, ok := o.([]interface{})
if ok {
for i, value := range a {
a[i] = deepConvertRFC3339(value)
}
return a
}
return o
}
// GetString lets you specify a path to the value that you want
// (e.g. "aaa.bbb.ccc") and have it extracted from the data structure.
func GetString(v interface{}, path string) string {
for _, key := range strings.Split(path, ".") {
iKey, err := strconv.ParseInt(key, 10, 32)
if err == nil {
// If the key is a number, we assume the structure is an array
arr, ok := v.([]interface{})
if !ok {
return ""
}
v = arr[iKey]
} else {
// If the key isn't a number, we assume the structure is a map
m, ok := v.(map[string]interface{})
if !ok {
return ""
}
v = m[key]
}
}
return fmt.Sprintf("%v", v)
}
// RunInTransaction runs the given function inside a database transaction.
// Does rollback if the function returns an error or panics.
// Otherwise, does commit.
func RunInTransaction(db *sql.DB, txFunc func(*sql.Tx) error) (err error) {
tx, err := db.Begin()
if err != nil {
return err
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback() // err is non-nil; don't change it
} else {
err = tx.Commit() // err is nil; if Commit returns error update err
}
}()
err = txFunc(tx)
return err
}
// RunStatementsInTransaction runs all the given statements inside a database transaction.
// If a statement fails, the transaction is rolled back.
// That way either all of them take effect or none of them do.
func RunStatementsInTransaction(db *sql.DB, statements []string, args ...interface{}) error {
return RunInTransaction(db, func(tx *sql.Tx) error {
for _, st := range statements {
if _, err := tx.Exec(st, args...); err != nil {
log.Printf("Statement that failed: %s", st)
return err
}
}
return nil
})
}
// RandomStringID returns a string of 32 characters,
// Each character is from the set [A-Za-z0-9].
func RandomStringID() string {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
var b strings.Builder
for i := 0; i < 32; i++ {
j := rand.Intn(len(charset))
b.WriteString(charset[j : j+1])
}
return b.String()
}
// BuildUpdateStatement is a helper function that builds an SQL UPDATE statement dynamically.
func BuildUpdateStatement(tableName string, columnValues map[string]interface{},
whereColumn string, whereValue interface{}) (string, []interface{}) {
var sql strings.Builder
sql.WriteString("UPDATE ")
sql.WriteString(tableName)
sql.WriteString(" SET ")
params := make([]interface{}, len(columnValues)+1)
index := 1
for column, value := range columnValues {
if index > 1 {
sql.WriteString(",")
}
sql.WriteString(column)
sql.WriteString("=$")
sql.WriteString(strconv.Itoa(index))
params[index-1] = value
index++
}
sql.WriteString(" WHERE ")
sql.WriteString(whereColumn)
sql.WriteString("=$")
sql.WriteString(strconv.Itoa(index))
params[len(params)-1] = whereValue
return sql.String(), params
}
// BuildInsertStatement is a helper function that builds an SQL INSERT statement dynamically.
func BuildInsertStatement(tableName string, columnValues map[string]interface{}) (string, []interface{}) {
var sql strings.Builder
sql.WriteString("INSERT INTO ")
sql.WriteString(tableName)
sql.WriteString("(")
params := make([]interface{}, len(columnValues))
i := 0
for column, value := range columnValues {
if i > 0 {
sql.WriteString(",")
}
sql.WriteString(column)
params[i] = value
i++
}
sql.WriteString(") VALUES(")
for i := 0; i < len(params); i++ {
if i > 0 {
sql.WriteString(",")
}
sql.WriteString("$")
sql.WriteString(strconv.Itoa(i + 1))
}
sql.WriteString(")")
return sql.String(), params
}
// RemoveDuplicateStrings returns a new slice with the unique strings from the original array,
// i.e. duplicate elements have been removed.
func RemoveDuplicateStrings(array []string) []string {
m := make(map[string]bool, len(array))
for _, s := range array {
m[s] = true
}
newArray := make([]string, len(m))
i := 0
for str := range m {
newArray[i] = str
i++
}
return newArray
}