forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
128 lines (110 loc) · 2.7 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
package utils
import (
"database/sql/driver"
"fmt"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"unicode"
)
var gormSourceDir string
func init() {
_, file, _, _ := runtime.Caller(0)
// compatible solution to get gorm source directory with various operating systems
gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
}
// FileWithLineNum return the file name and line number of the current file
func FileWithLineNum() string {
// the second caller usually from gorm internal, so set i start from 2
for i := 2; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
return file + ":" + strconv.FormatInt(int64(line), 10)
}
}
return ""
}
func IsValidDBNameChar(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && c != '@'
}
func CheckTruth(val interface{}) bool {
if v, ok := val.(bool); ok {
return v
}
if v, ok := val.(string); ok {
v = strings.ToLower(v)
return v != "false"
}
return !reflect.ValueOf(val).IsZero()
}
func ToStringKey(values ...interface{}) string {
results := make([]string, len(values))
for idx, value := range values {
if valuer, ok := value.(driver.Valuer); ok {
value, _ = valuer.Value()
}
switch v := value.(type) {
case string:
results[idx] = v
case []byte:
results[idx] = string(v)
case uint:
results[idx] = strconv.FormatUint(uint64(v), 10)
default:
results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
}
}
return strings.Join(results, "_")
}
func AssertEqual(src, dst interface{}) bool {
if !reflect.DeepEqual(src, dst) {
if valuer, ok := src.(driver.Valuer); ok {
src, _ = valuer.Value()
}
if valuer, ok := dst.(driver.Valuer); ok {
dst, _ = valuer.Value()
}
return reflect.DeepEqual(src, dst)
}
return true
}
func ToString(value interface{}) string {
switch v := value.(type) {
case string:
return v
case int:
return strconv.FormatInt(int64(v), 10)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
}
return ""
}
func ExistsIn(a string, list *[]string) bool {
if list == nil {
return false
}
for _, b := range *list {
if b == a {
return true
}
}
return false
}