-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
154 lines (129 loc) · 2.92 KB
/
helper.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
package helpers
import (
"bytes"
"math/rand"
"reflect"
"regexp"
)
func Implode(slices []string, sep string) string {
result := ""
length := len(slices)
for i, item := range slices {
result += item
if i != length-1 {
result += sep
}
}
return result
}
func Explode(str string, sep string) []string {
result := []string{}
for _, item := range bytes.Split(([]byte)(str), ([]byte)(sep)) {
result = append(result, (string)(item))
}
return result
}
func SliceEqual(src []string, dist []string) bool {
if len(src) != len(dist) {
return false
}
for idx, value := range src {
if value != dist[idx] {
return false
}
}
return true
}
func InStrArray(array []string, item string) bool {
for _, ele := range array {
if ele == item {
return true
}
}
return false
}
func MergeStrArray(arr1 []string, arr2 []string) []string {
for _, item := range arr2 {
arr1 = append(arr1, item)
}
return arr1
}
func Keys(maps map[string]interface{}) []string {
result := []string{}
for key, _ := range maps {
result = append(result, key)
}
return result
}
func RandString(length int) string {
template := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return RandTemplateString(template, length)
}
func RandNumberString(length int) string {
template := "0123456789"
return RandTemplateString(template, length)
}
func RandLetterString(length int) string {
template := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return RandTemplateString(template, length)
}
func toHumpCase(src string) string {
under := regexp.MustCompile("_\\w")
result := under.ReplaceAllFunc([]byte(src), func(matched []byte) []byte {
return bytes.ToUpper(matched[1:2])
})
return string(result)
}
func toUnderline(src string) string {
hump := regexp.MustCompile("[A-Z]")
result := hump.ReplaceAllFunc([]byte(src), func(matched []byte) []byte {
return []byte("_" + string(bytes.ToLower(matched)))
})
return string(result)
}
func RandTemplateString(template string, length int) string {
runes := []rune(template)
b := make([]rune, length)
ler := len(runes)
for i := range b {
b[i] = runes[rand.Intn(ler)]
}
return string(b)
}
func InstanceOf(v interface{}, t interface{}) bool {
if v == nil || t == nil {
return false
}
tt := reflect.TypeOf(t)
tv := reflect.TypeOf(v)
if tt.Kind() == reflect.Ptr {
tt = tt.Elem()
}
if tv.Kind() == reflect.Ptr {
tv = tv.Elem()
}
if tt.Kind() == reflect.Interface {
if tv.Kind() != reflect.Interface {
return reflect.PtrTo(tv).Implements(tt)
} else {
return tv.Implements(tt)
}
} else {
return tt == tv || hasEmbedded(tv, tt)
}
}
func hasEmbedded(tv reflect.Type, tt reflect.Type) bool {
if tt.Kind() == reflect.Ptr {
tt = tt.Elem()
}
if tv.Kind() == reflect.Ptr {
tv = tv.Elem()
}
for i := 0; i < tv.NumField(); i++ {
tsf := tv.Field(i)
if tsf.Anonymous && (tt == tsf.Type || hasEmbedded(tsf.Type, tt)) {
return true
}
}
return false
}