-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoutil.go
279 lines (236 loc) · 5.73 KB
/
goutil.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
package goutil
import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"log"
"math"
"math/rand"
"os"
"reflect"
"regexp"
"runtime"
"strings"
"time"
"github.com/google/uuid"
)
// FuncName will return the current function's name.
// It can be used for a better log debug system.(I'm NOT sure.)
func FuncName() string {
pc, _, _, _ := runtime.Caller(1)
return runtime.FuncForPC(pc).Name()
}
// Limit the decimal value e.g. 2.34343434 will become 2.34 if precision is set to 2
func FloatPrecision(num float64, precision int) float64 {
p := math.Pow10(precision)
value := float64(int(num*p)) / p
return value
}
func FullName(firstName string, lastName string) string {
name := firstName
if len(lastName) > 0 {
name = name + " " + lastName
}
return name
}
type Name struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
// Get FirstName and LastName from the FullName
func ParseName(fullName string) Name {
nameParts := strings.Split(strings.TrimSpace(fullName), " ")
var name Name
if len(nameParts) > 0 {
name.FirstName = nameParts[0]
}
if len(nameParts) > 1 {
name.LastName = nameParts[1]
}
return name
}
// Env get key environment variable if exist otherwise return defalutValue
func Env(key, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}
func RandomNumber(length int) string {
rand.Seed(time.Now().UnixNano())
chars := []rune("123456789")
// length := 8
var b strings.Builder
for i := 0; i < length; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
}
str := b.String() // E.g. "ExcbsVQs"
return str
}
// Define n as number to limit the length of the random string
func RandString(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func RandMD5String() string {
secret := uuid.New().String()
key := []byte(secret)
hash := md5.Sum(key)
return hex.EncodeToString(hash[:])
}
// Convert any string to snakecase StudentID will become student_id
func SnakeCase(str string) string {
var matchFirstCap = regexp.MustCompile("([A-Z])([A-Z][a-z])")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
output := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
output = matchAllCap.ReplaceAllString(output, "${1}_${2}")
output = strings.ReplaceAll(output, "-", "_")
return strings.ToLower(output)
}
// Convert any struct to JSON String
func ToJSON(val interface{}) (string, error) {
b, err := json.Marshal(val)
if err != nil {
return "", err
}
return string(b), nil
}
// Convert any struct to JSON String with Pretty Print
func ToJSONIndent(val interface{}) (string, error) {
b, err := json.MarshalIndent(val, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
func PrintToJSON(val interface{}) {
b, err := ToJSONIndent(val)
if err != nil {
log.Println(err)
return
}
log.Println(b)
}
// Use Make the unused value used so golang will not give error while compiling
func Use(vals ...interface{}) {
for _, val := range vals {
_ = val
}
}
func StringIndexWithLowerCase(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == strings.ToLower(val) {
return i, true
}
}
return -1, false
}
func StringIndex(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func UintIndex(slice []uint, val uint) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func IntIndex(slice []int, val int) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func UUIDIndex(slice []uuid.UUID, val uuid.UUID) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func StringArrayToUUIDArray(val []string) []uuid.UUID {
var uids []uuid.UUID
for _, v := range val {
uids = append(uids, uuid.MustParse(v))
}
return uids
}
// Remove all special characters e.g. Aman C。Salcedo will become Aman C Salcedo
func CleanString(s string) string {
// Make a Regex to say we only want letters and numbers
reg, err := regexp.Compile("[^a-zA-Z0-9 ]+")
if err != nil {
// log.Fatal(err)
return ""
}
processedString := reg.ReplaceAllString(s, "")
return processedString
}
func HashString(val string) string {
key := []byte(val)
h := sha256.New()
h.Write(key)
sha1_hash := hex.EncodeToString(h.Sum(nil))
return sha1_hash
}
// Strip all email from strings e.g. My email is abc@demo.com. will become My email is.
func StripEmails(s string) string {
const regex = `\S*@\S*\s?`
r := regexp.MustCompile(regex)
return r.ReplaceAllString(s, "")
}
func GetTypeName(myvar interface{}) string {
valueOf := reflect.ValueOf(myvar)
if valueOf.Type().Kind() == reflect.Ptr {
return (reflect.Indirect(valueOf).Type().Name())
} else {
return (valueOf.Type().Name())
}
}
// Get Nested TypeName as String func to make the Preload Typesafe instead of hardcoded
// fmt.Println(util.GormTypeName(uxmpim_type.Item{}, uxmpim_type.ItemStatus{}))
// output: Item.ItemStatus
func GormTypeName(values ...interface{}) string {
var typeNames []string
for _, val := range values {
typeNames = append(typeNames, GetTypeName(val))
}
if len(typeNames) == 0 {
return ""
}
return strings.Join(typeNames, ".")
}
func TypeToString(data interface{}) string {
mdata, err := json.Marshal(data)
if err != nil {
return ""
}
return string(mdata)
}
func IsErrNil(err error) bool {
if err == nil {
return true
}
return false
}
func IsErrNotNil(err error) bool {
if err != nil {
return true
}
return false
}