-
Notifications
You must be signed in to change notification settings - Fork 9
/
common.go
executable file
·210 lines (188 loc) · 4.25 KB
/
common.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
// Code generated by dol build. Only Generate by tools if not existed.
// source: app.go
package util
import (
"fmt"
"math/rand"
"net/http"
"os"
"reflect"
"runtime"
"strings"
"time"
"github.com/2637309949/dolphin/platform/util/slice"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
)
// M defined
type M map[string]interface{}
// RandType defined rand type
type RandType int
// RandType defined
const (
RandNum RandType = iota
RandNumUperChar
RandNumChar
)
// RandString defined random char
func RandString(randLen int, randType RandType) string {
randUint := make([]byte, randLen)
rand.Seed(time.Now().UnixNano())
switch randType {
case RandNum:
for i := 0; i < randLen; i++ {
randUint[i] = uint8(48 + rand.Intn(10))
}
case RandNumUperChar:
for i := 0; i < randLen; i++ {
r := rand.Intn(36)
switch {
case r < 10:
randUint[i] = uint8(48 + r)
case r < 36:
randUint[i] = uint8(55 + r)
}
}
case RandNumChar:
for i := 0; i < randLen; i++ {
r := rand.Intn(62)
switch {
case r < 10:
randUint[i] = uint8(48 + r)
case r < 36:
randUint[i] = uint8(55 + r)
case r < 62:
randUint[i] = uint8(61 + r)
}
}
}
return string(randUint)
}
// RandomInt generates a random int, based on a min and max values
func RandomInt(min, max int) int {
return min + rand.Intn(max-min)
}
// EnsureLeft defined return left
func EnsureLeft(left interface{}, err error) interface{} {
if err != nil {
logrus.Fatal(fmt.Errorf("%v", string(errors.Wrap(err, 2).Stack())))
}
return left
}
// EnsureRight defined return right
func EnsureRight(err error, right interface{}) interface{} {
if err != nil {
logrus.Fatal(fmt.Errorf("%v", string(errors.Wrap(err, 2).Stack())))
}
return right
}
// Ensure defined
func Ensure(err error) {
if err != nil {
logrus.Fatal(fmt.Errorf("%v\n%v", err, string(errors.Wrap(err, 2).Stack())))
}
}
// SetFormatter defined
func SetFormatter(isTerminal bool) {
if !isTerminal {
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: "02/01 15:04:05",
})
} else {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
DisableQuote: true,
ForceColors: true,
TimestampFormat: "02/01 15:04:05",
})
}
}
// UserHomeDir defined
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
// DeepSearch scans deep maps, following the key indexes listed in the
func DeepSearch(m map[string]interface{}, path []string) map[string]interface{} {
for _, k := range path {
m2, ok := m[k]
if !ok {
// intermediate key does not exist
// => create it and continue from there
m3 := make(map[string]interface{})
m[k] = m3
m = m3
continue
}
m3, ok := m2.(map[string]interface{})
if !ok {
m3 = make(map[string]interface{})
m[k] = m3
}
m = m3
}
return m
}
// SomeOne defined
func SomeOne(srv interface{}, def interface{}) interface{} {
sliceValue := reflect.Indirect(reflect.ValueOf(srv))
if sliceValue.Kind() == reflect.Slice {
size := sliceValue.Len()
if size <= 0 {
return def
}
return sliceValue.Index(0).Interface()
}
return def
}
// LastChar defined
func LastChar(str string) uint8 {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
}
// JoinObject defined TODO
func JoinObject(arr interface{}, format ...string) (str string) {
if !slice.IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
var (
ft = "%v"
)
if len(format) > 0 {
ft = format[0]
}
var (
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
)
kind := arrType.Kind()
if kind == reflect.Slice || kind == reflect.Array {
for i := 0; i < arrValue.Len(); i++ {
if str != "" {
str += ","
}
str += fmt.Sprintf(ft, arrValue.Index(i).Interface())
}
}
return str
}
// JsonCheck defined TODO
func JsonCheck(req *http.Request) bool {
jsonType := "application/json"
if req.ContentLength > 0 && strings.Contains(req.Header.Get("Content-Type"), jsonType) {
return true
}
return false
}
// QueryCheck defined TODO
func QueryCheck(req *http.Request) bool {
return req.Method == http.MethodGet || req.Method == http.MethodDelete
}