-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
109 lines (98 loc) · 2.91 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
package drivers
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
)
func BackQuotes(arg any) string {
var tmp []string
if v, ok := arg.(string); ok {
split := strings.Split(v, " ")
split2 := strings.Split(split[0], ".")
if len(split2) > 1 {
if split2[1] == "*" {
tmp = append(tmp, fmt.Sprintf("`%s`.%s", split2[0], split2[1]))
} else {
tmp = append(tmp, fmt.Sprintf("`%s`.`%s`", split2[0], split2[1]))
}
} else {
tmp = append(tmp, fmt.Sprintf("`%s`", split2[len(split2)-1]))
}
tmp = append(tmp, split[1:]...)
}
return strings.Join(tmp, " ")
}
func ToSlice(arg any) []any {
ref := reflect.Indirect(reflect.ValueOf(arg))
var res []any
switch ref.Kind() {
case reflect.Slice:
l := ref.Len()
v := ref.Slice(0, l)
for i := 0; i < l; i++ {
res = append(res, v.Index(i).Interface())
}
default:
res = append(res, ref.Interface())
}
return res
}
func NamedSprintf(format string, a ...any) string {
return strings.TrimSpace(regexp.MustCompile(`\s{2,}`).ReplaceAllString(fmt.Sprintf(regexp.MustCompile(`:\w+`).ReplaceAllString(format, "%s"), a...), " "))
}
func TrimPrefixAndOr(s string) string {
return regexp.MustCompile(`(?i)^\s*(and|or)\s+`).ReplaceAllString(s, "")
}
func assertsEqual(t *testing.T, expect, real any) {
marshal, err := json.Marshal(expect)
assertsError(t, err)
bytes, err := json.Marshal(real)
assertsError(t, err)
if string(marshal) != string(bytes) {
methodName, file, line := getCallerInfo()
t.Errorf("[%s] Error\n\t Trace - %s:%v\n\tExpect - %T %s\n\t Got - %T %s\n------------------------------------------------------", methodName, file, line, expect, marshal, real, bytes)
}
}
func assertsError(t *testing.T, err error) {
if err != nil {
methodName, file, line := getCallerInfo()
t.Errorf("[%s] Error\n\t Trace - %s:%v\n\t%s\n------------------------------------------------------", methodName, file, line, err.Error())
}
}
func getCallerInfo() (string, string, int) {
pc := make([]uintptr, 10)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
var i int
for frame, more := frames.Next(); more; frame, more = frames.Next() {
i++
if i == 1 {
continue
}
//fmt.Printf("Method: %s\nFile: %s\nLine: %d\n\n", frame.Function, frame.File, frame.Line)
lastDotIndex := strings.LastIndex(frame.Function, ".")
methodName := frame.Function[lastDotIndex+1:]
//t.Logf("[%s] errors on file:line: \n\t\t -> %s:%v\n", methodName, frame.File, frame.Line)
if i == 2 {
return methodName, frame.File, frame.Line
}
//break
}
return "", "", 0
}
func jsonLog(t *testing.T, data any) {
marshal, err := json.Marshal(data)
assertsError(t, err)
t.Logf("json data: %s", marshal)
}
func Map[Data any, Datas ~[]Data, Result any](datas Datas, mapper func(Data) Result) []Result {
results := make([]Result, 0, len(datas))
for _, data := range datas {
results = append(results, mapper(data))
}
return results
}