/
utils.go
440 lines (349 loc) · 8.51 KB
/
utils.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"strconv"
"strings"
rs "technopark-db/response"
mysql "github.com/go-sql-driver/mysql"
)
type InputRequest struct {
method string
url string
path string
json map[string]interface{}
query map[string][]string
}
func (ir *InputRequest) parse(r *http.Request) {
ir.method = r.Method
ir.url = fmt.Sprintf("%v", r.URL)
ir.path = r.URL.EscapedPath()
// POST JSON
body, err := ioutil.ReadAll(r.Body) // ReadAll reads from r until an error or EOF and returns the data it read
if err != nil {
log.Panic(err)
}
var parsed map[string]interface{}
json.Unmarshal([]byte(body), &parsed)
ir.json = parsed
// GET Query
ir.query = r.URL.Query()
}
func createResponse(code int, response rs.RespStruct) string {
content := map[string]interface{}{
"code": code,
"response": response,
}
str, err := json.Marshal(content)
if err != nil {
log.Println("Error encoding JSON")
log.Panic(err)
}
return string(str)
}
func createResponseFromArray(code int, response []interface{}) string {
cacheContent := map[string]interface{}{
"code": code,
"response": response,
}
str, err := json.Marshal(cacheContent)
if err != nil {
log.Println("Error encoding JSON")
log.Panic(err)
}
return string(str)
}
func errorExecParse(err error) (int, string) {
log.Println("Error:\t", err)
if driverErr, ok := err.(*mysql.MySQLError); ok { // Now the error number is accessible directly
var responseCode int
var errorMessage string
switch driverErr.Number {
case 1062:
responseCode = 5
errorMessage = "Exist"
// Error 1452: Cannot add or update a child row: a foreign key constraint fails
case 1452:
responseCode = 5
errorMessage = "Exist [Error 1452]"
default:
// fmt.Println("errorExecParse() default")
panic(err.Error())
responseCode = 4
errorMessage = "Unknown Error"
}
return responseCode, errorMessage
}
panic(err.Error()) // proper error handling instead of panic in your app
}
func createErrorResponse(err error) string {
responseCode, msg := errorExecParse(err)
errorMessage := &rs.ErrorMsg{
Msg: msg,
}
return createResponse(responseCode, errorMessage)
}
func createInvalidQuery() string {
responseCode := 3
errorMessage := &rs.ErrorMsg{
Msg: "Invalid query",
}
return createResponse(responseCode, errorMessage)
}
func createInvalidJsonResponse(inputRequest *InputRequest) string {
responseCode := 3
errorMessage := &rs.ErrorMsg{
Msg: "Invalid json",
}
log.Println("Invalid JSON:\turl=\tjson=", inputRequest.url, inputRequest.json)
return createResponse(responseCode, errorMessage)
}
func checkError1062(err error) bool {
if driverErr, ok := err.(*mysql.MySQLError); ok { // Now the error number is accessible directly
if driverErr.Number == 1062 {
return true
}
}
return false
}
func createInvalidResponse() string {
responseCode := 2
errorMessage := &rs.ErrorMsg{
Msg: "Invalid",
}
return createResponse(responseCode, errorMessage)
}
func createNotExistResponse() string {
responseCode := 1
errorMessage := &rs.ErrorMsg{
Msg: "Not exist",
}
return createResponse(responseCode, errorMessage)
}
// KOSTYL` API
func becauseAPI() string {
kostyl := make(map[string]interface{})
content := map[string]interface{}{
"code": 0,
"response": kostyl,
}
str, err := json.Marshal(content)
if err != nil {
log.Println("Error encoding JSON")
log.Panic(err)
}
return string(str)
}
func validateJson(ir *InputRequest, args ...string) bool {
for _, value := range args {
if reflect.TypeOf(ir.json[value]) == nil {
return false
}
}
return true
}
func validateBoolParams(json map[string]interface{}, args *Args, params ...string) bool {
for _, value := range params {
if json[value] == nil {
args.append(false)
} else {
if reflect.TypeOf(json[value]).Kind() != reflect.Bool {
return false
}
args.append(json[value])
}
}
return true
}
func clearQuery(query *map[string][]string) {
for k := range *query {
delete(*query, k)
}
}
type Args struct {
data []interface{}
}
func (args *Args) generateFromJson(json *map[string]interface{}, params ...string) {
for _, value := range params {
args.data = append(args.data, (*json)[value])
}
}
func (args *Args) addFromJson(json *map[string]interface{}, params ...string) {
for _, value := range params {
args.data = append(args.data, (*json)[value])
}
}
func (args *Args) append(newData ...interface{}) {
for data := range newData {
args.data = append(args.data, newData[data])
}
}
func (args *Args) clear() { args.data = args.data[0:0] }
// ======================
// Database queries here
// ======================
type ExecResponse struct {
lastId int64
rowCount int64
}
func execQuery(query string, args *[]interface{}, db *sql.DB) (*ExecResponse, error) {
resp := new(ExecResponse)
tx, err := db.Begin()
if err != nil {
return nil, err
}
defer tx.Commit()
stmt, err := tx.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
res, err := stmt.Exec(*args...)
if err != nil {
return nil, err
}
lastId, err := res.LastInsertId()
if err != nil {
return nil, err
}
rowCnt, err := res.RowsAffected()
if err != nil {
return nil, err
}
// log.Printf("ID = %d, affected = %d\n", lastId, rowCnt)
resp.lastId = lastId
resp.rowCount = rowCnt
return resp, nil
}
type SelectResponse struct {
rows int
columns []string
values []map[string]string
}
func selectQuery(query string, args *[]interface{}, db *sql.DB) *SelectResponse {
resp := new(SelectResponse)
rows, err := db.Query(query, *args...)
if err != nil {
log.Panic(err)
}
defer rows.Close()
// Get column names
columns, err := rows.Columns()
if err != nil {
log.Panic(err)
}
// Make a slice for the values
values := make([]sql.RawBytes, len(columns))
// rows.Scan wants '[]interface{}' as an argument, so we must copy the
// references into such a slice
// See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details
scanArgs := make([]interface{}, len(values))
for i := range values {
scanArgs[i] = &values[i]
}
var respValues []map[string]string
// Fetch rows
for rows.Next() {
x := make(map[string]string)
// get RawBytes from data
err = rows.Scan(scanArgs...)
if err != nil {
log.Panic(err)
}
// Now do something with the data.
// Here we just print each column as a string.
var value string
for i, col := range values {
// Here we can check if the value is nil (NULL value)
if col == nil {
value = "NULL"
} else {
value = string(col)
}
x[columns[i]] = value
// x = append(x, value)
}
respValues = append(respValues, x)
}
resp.columns = columns
resp.values = respValues
resp.rows = len(resp.values)
if err = rows.Err(); err != nil {
log.Panic(err)
}
return resp
}
// =================
// Utils here
// =================
func floatToString(inputNum float64) string { return strconv.FormatFloat(inputNum, 'f', 6, 64) }
func int64ToString(inputNum int64) string { return strconv.FormatInt(inputNum, 10) }
func checkFloat64Type(inputNum interface{}) bool {
if reflect.TypeOf(inputNum).Kind() != reflect.Float64 {
return false
}
return true
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func stringToInt(inputStr string) int {
result, err := strconv.Atoi(inputStr)
if err != nil {
log.Panic(err)
}
return result
}
func stringToInt64(inputStr string) int64 { return int64(stringToInt(inputStr)) }
func toBase92(value int) string {
BASE92 := ` !"#$&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^` + "`" + `abcdefghijklmnopqrstuvwxyz{|}~`
length := 5
base := len(BASE92)
result := make([]byte, 5)
for i := 0; i < length; i++ {
result[i] = BASE92[0]
}
if value == 0 {
return string(result)
}
counter := 0
for value != 0 {
mod := value % base
result[length-counter-1] = BASE92[mod]
counter++
value = value / base
}
return string(result)
}
func fromBase92(value string) int {
BASE92 := ` !"#$&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^` + "`" + `abcdefghijklmnopqrstuvwxyz{|}~`
length := 5
base := len(BASE92)
var result int
counter := 0
step := 1
for i := 0; i < length; i++ {
index := strings.Index(BASE92, string(value[length-counter-1]))
counter++
result = result + index*step
step = step * base
}
return result
}
func stringToBool(inputString string) bool {
result, err := strconv.ParseBool(inputString)
if err != nil {
log.Panic(err)
}
return result
}