-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.go
436 lines (387 loc) · 9.6 KB
/
tool.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Package tool Useful general purpose tool
package tool
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
stdlog "log"
"math/big"
"path/filepath"
"reflect"
"runtime"
"strings"
"text/template"
"time"
"golang.org/x/exp/slices"
"golang.org/x/exp/constraints"
)
type (
StdLogger interface {
Println(...any)
Panicln(...any)
Printf(string, ...any)
Print(...any)
}
LogRus interface {
StdLogger
WithError(error) LogRus
Errorln(...any)
}
logger struct {
l StdLogger
}
Varchar string
catchableError struct {
error
}
)
// Unwrap Returns the wrapped error
func (e catchableError) Unwrap() error { return e.error }
// tooloLog Package level logger, defaults to log.Default()
var tooloLog = &logger{l: stdlog.Default()}
func getRelativePath(filePath string) string {
relPath, err := filepath.Rel(filepath.Dir(findRootCaller()), filePath)
if err != nil {
return filePath // return the original file path if error
}
return relPath
}
// Console Prints %+v of arguments, great to debug stuff
func Console(obj ...interface{}) {
pc, _, line, ok := runtime.Caller(1)
if !ok {
tooloLog.LogError(errors.New("unable to get caller information"))
return
}
fn := runtime.FuncForPC(pc)
if fn == nil {
tooloLog.LogError(errors.New("unable to get function information"))
return
}
pkg := strings.Split(fn.Name(), "/")
pkgName := strings.Join(pkg[0:len(pkg)-1], "/") + "/"
pkgName += strings.Split(pkg[len(pkg)-1:][0], ".")[0]
prefix := fmt.Sprintf("[%s:%d]>", pkgName, line)
tooloLog.LogDeep(append([]interface{}{prefix}, obj...)...)
}
// SetLogger Sets tool package logger, pass nil to disable logging
func SetLogger(l StdLogger) {
tooloLog = &logger{l: l}
}
// Try Probes the error and returns bool, optionally logs the message.
func Try(err error, verbose ...bool) bool {
if err != nil {
if len(verbose) > 0 && verbose[0] {
tooloLog.LogError(err)
}
return true
}
return false
}
// Must Tolerates no errors.
func Must(err error, verbose ...bool) {
if err != nil {
if len(verbose) > 0 && verbose[0] {
tooloLog.LogError(err)
}
panic(catchableError{err})
}
}
// Return Ignores errors, returns value.
func Return[T any](val T, _ error) T {
return val
}
// MultiMute Ignores errors, returns slice of results.
func MultiMute[T any](a ...T) []T {
if len(a) == 0 {
return nil
}
val := reflect.ValueOf(a[len(a)-1])
lastInterface := val.Interface()
if reflect.TypeOf(lastInterface).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
a = a[:len(a)-1]
}
if len(a) == 0 {
return nil
}
return a
}
// MustReturn Tolerates no errors, returns value.
func MustReturn[T any](val T, err error) T {
Must(err)
return val
}
// Err Returns the last argument if it is an error, otherwise nil
func Err(args ...any) error {
var err error
if len(args) > 0 {
err, _ = args[len(args)-1].(error)
}
return err
}
// Catch Recovers from panic and callbacks with error
// If error is not catchableError, it will panic again
// May be used as defer, coupled with MustReturn or Must, to override named return values
//
// Usage:
//
// func example() (val *http.Request, err error) {
// defer tool.Catch(func(caught error) {
// err = caught
// })
//
// val = tool.MustReturn(funcThatReturnsValAndErr()) // <- this will be caught if err!=nil
// panic(errors.New("some error")) // <- this will not be caught
// return
// }
func Catch(fn func(err error)) {
e := recover()
if e == nil {
return
}
if iamError, ok := e.(*catchableError); ok {
fn(iamError.Unwrap())
return
}
panic(e)
}
// RandInt Return a random number in specified range.
func RandInt[num constraints.Signed](min, max num) num {
bInt, err := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
Must(err)
bInt = bInt.Add(bInt, big.NewInt(int64(min)))
return num(bInt.Int64())
}
// Ptr Return a pointer for any passed object
func Ptr[T any](n T) *T {
return &n
}
// In Checks if element is in a slice
func In[T comparable](needle T, haystack ...T) bool {
return slices.Contains(haystack, needle)
}
// RetryFunc Re-runs function if error returned
func RetryFunc[num constraints.Signed](attempts num, sleep time.Duration, f func() error) error {
var retryErr error
for {
retryErr = f()
if !Try(retryErr) {
return nil
}
if attempts == 0 {
break
}
attempts--
time.Sleep(sleep)
tooloLog.LogError(retryErr, "retrying after error")
}
return retryErr
}
// Recoverer Recovers job from panic, if maxPanics<0 then infinitely
func Recoverer[num constraints.Integer](maxPanics num, f func(), jobID ...string) (recovErr error) {
defer func() {
if err := recover(); err != nil {
panicErr := fmt.Errorf(`job %spanics with message: %s, %s`, strings.Join(jobID, " ")+" ", err, identifyPanic())
tooloLog.LogError(panicErr)
if maxPanics != 0 {
recovErr = Recoverer(maxPanics-1, f, jobID...)
}
if recovErr == nil {
recovErr = panicErr
}
return
}
}()
f()
return
}
// Jsonify Returns Varchar implementation of the serialized value, returns empty on error
func Jsonify(s any) Varchar {
b, err := json.Marshal(s)
if Try(err, true) {
return ""
}
return Varchar(b)
}
// Objectify Unmarshalls value to the target pointer value
func Objectify[T ~[]byte | ~string](in T, target any) bool {
return !Try(json.Unmarshal([]byte(in), target), true)
}
// Strtr Replaces all old string occurrences with new string in subject
func Strtr(subject string, oldToNew map[string]string) string {
if len(oldToNew) == 0 || len(subject) == 0 {
return subject
}
for old, news := range oldToNew {
if old == "" || old == news {
continue
}
subject = strings.ReplaceAll(subject, old, news)
}
return subject
}
// NonZero Returns first non-zero value or zero value if all values are zero
func NonZero[T comparable](ts ...T) T {
var zeroValue T
if len(ts) == 0 {
return zeroValue
}
for _, t := range ts {
if t == zeroValue {
continue
}
return t
}
return zeroValue
}
// IsZero Checks if value is zero
func IsZero[T comparable](v T) bool {
var zero T
return v == zero
}
// identifyPanic Helper function to get user-friendly call stack message.
func identifyPanic() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
switch {
case name != "":
return fmt.Sprintf("%v:%v", name, line)
case file != "":
return fmt.Sprintf("%v:%v", file, line)
}
return fmt.Sprintf("pc:%x", pc)
}
// Bytes Return Varchar as Bytes slice
func (s Varchar) Bytes() []byte {
return []byte(s)
}
// String Return Varchar as string
func (s Varchar) String() string {
return string(s)
}
func (s *Varchar) MarshalJSON() ([]byte, error) {
if len(s.Bytes()) == 0 {
return s.Bytes(), nil
}
if res := Jsonify(s.Bytes()).Bytes(); len(res) > 0 {
return res, nil
}
return nil, fmt.Errorf("failed to marshal varchar")
}
// Log Logs anything
func (l *logger) Log(msgs ...any) {
if l.l == nil {
return
}
l.l.Println(msgs)
}
// LogDeep Printf version to log objects deeply
func (l *logger) LogDeep(obj ...any) {
if l.l == nil {
return
}
var buf strings.Builder
for _, subj := range obj {
buf.WriteString(fmt.Sprintf("%+v ", subj))
}
str := buf.String()[:buf.Len()-1]
str = strings.ReplaceAll(strings.ReplaceAll(str, "\r", "\\r"), "\n", "\\n")
l.l.Println(str)
}
// LogError Loose function to log error
func (l *logger) LogError(err error, msgs ...string) {
if l.l == nil {
return
}
if isrus, ok := l.l.(LogRus); ok {
isrus.WithError(err).Errorln(strings.Join(msgs, ": "))
return
}
if len(msgs) > 0 {
msgs = append(msgs, "") // add final colon
}
l.l.Println(errors.New(strings.Join(msgs, ": ") + err.Error()))
}
// PanicOnError Loose function to panic with error
func (l *logger) PanicOnError(err error, msgs ...string) {
if l.l == nil {
return
}
l.LogError(err, msgs...)
panic(err)
}
func ExecTemplate(templateText string, templateVars any) string {
tpl, err := template.New("ez").Parse(templateText)
tpl.Option("missingkey=zero")
if Try(err) {
return ""
}
var buf strings.Builder
err = tpl.Execute(&buf, templateVars)
if Try(err) {
return ""
}
return buf.String()
}
// ConvertSlice Return a new slice as `[]dstTypedValue.(type)` cast from the `srcSlice`
func ConvertSlice[T any, Y any](srcSlice []T, destTypedValue Y) []Y {
srcReflectType := reflect.TypeOf(srcSlice)
if srcReflectType.Kind() != reflect.Slice {
panic("srcSlice is not a slice")
}
if srcSlice == nil {
return nil
} else if len(srcSlice) == 0 {
return []Y{}
}
destType := reflect.TypeOf(destTypedValue)
destSlice := reflect.MakeSlice(reflect.SliceOf(destType), len(srcSlice), len(srcSlice))
for i := range srcSlice {
srcVal := reflect.Indirect(reflect.ValueOf(srcSlice[i]))
destVal := reflect.New(destType).Elem()
switch {
case srcVal.Type().ConvertibleTo(destType):
destVal = srcVal.Convert(destType)
case srcVal.Type().AssignableTo(destType):
destVal = srcVal
default:
for j := 0; j < srcVal.NumField(); j++ {
srcField := srcVal.Type().Field(j)
destField := destVal.FieldByName(srcField.Name)
if destField.IsValid() && srcField.Type.AssignableTo(destField.Type()) {
destField.Set(srcVal.Field(j))
}
}
}
destSlice.Index(i).Set(destVal)
}
return destSlice.Interface().([]Y)
}
// findRootCaller Finds the root caller filepath of the application
func findRootCaller() string {
const MaxDepth = 32
for i := 0; i < MaxDepth; i++ {
_, file, _, ok := runtime.Caller(i)
if !ok {
break
}
if i == MaxDepth-1 || !strings.Contains(file, "runtime/") {
return file
}
}
return ""
}