-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
169 lines (146 loc) · 3.48 KB
/
util.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
package util
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"regexp"
"runtime/debug"
"strings"
"time"
"github.com/barmoury/barmoury-go/cache"
)
func CacheWriteAlong[T any](bufferSize uint64, dateLastFlushed time.Time, cache cache.Cache[T], entry T) bool {
cache.Cache(entry)
diff := DateDiffInMinutes(dateLastFlushed, time.Now())
return bufferSize >= cache.MaxBufferSize() || diff >= cache.IntervalBeforeFlush()
}
func DateDiffInMinutes(a time.Time, b time.Time) uint64 {
d := b.Sub(a)
return uint64(d.Minutes())
}
func StrFormat(str string, args ...any) string {
return fmt.Sprintf(str, args...)
}
func GetOrDefault[T any](v T, d T) T {
if reflect.ValueOf(v).IsZero() {
return d
}
return v
}
func TranverseIterable(m interface{}, fn func(any, any)) {
for k, v := range m.(map[string]map[string]any) {
fn(k, v)
}
}
func ValueInSlice[T comparable](list []T, a T) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func SplitByRegex(str string, regex string) []string {
re := regexp.MustCompile(regex)
sp := re.Split(str, -1)
set := []string{}
for i := range sp {
set = append(set, sp[i])
}
return set
}
func SlicesInterception[T string | uint64](t1 []T, t2 []T) []T {
rs := []T{}
cm := map[T]struct{}{}
for _, a := range t1 {
cm[a] = struct{}{}
}
for _, a := range t2 {
if _, ok := cm[a]; ok {
rs = append(rs, a)
}
}
return rs
}
func SlicesIntercepts[T string | uint64](t1 []T, t2 []T) bool {
cm := map[T]struct{}{}
for _, a := range t1 {
cm[a] = struct{}{}
}
for _, a := range t2 {
if _, ok := cm[a]; ok {
return true
}
}
return false
}
func If[T any](c bool, t T, f T) T {
if c {
return t
}
return f
}
func ReplaceByRegex(s string, r string, v string) string {
var re = regexp.MustCompile(r)
s = re.ReplaceAllString(s, v)
return s
}
func PatternToRegex(p string) *regexp.Regexp {
t := If(strings.HasSuffix(p, "**"), ``, `$`)
p = ReplaceByRegex(p, `:([\w])+`, `(\\w)+`)
f := ReplaceByRegex(ReplaceByRegex(ReplaceByRegex(p, `\*\*`, `(.)+`), `\?`, `(.)`), `\*`, `((?!(\/)).)+`) + t
r, _ := regexp.Compile(f)
return r
}
func StringToInterface(s string) interface{} {
return reflect.ValueOf(s).Interface()
}
func InterfaceToString(s interface{}) string {
return fmt.Sprintf("%s", reflect.ValueOf(s).Interface())
}
func StackTrace(skip int) []string {
st := []string{}
ds := strings.Split(StrFormat("%s", debug.Stack()), "\n")
st = append(st, ds[0])
st = append(st, ds[(skip*2)+1:]...)
return st
}
func StackTraceAsString(skip int) string {
c := ""
st := StackTrace(skip)
for _, s := range st {
c += s + "\n"
}
return c
}
func RequestForResponse[T any](method string, url string, body []byte, headers map[string]string) (int, T) {
r, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
panic(err)
}
for k, v := range headers {
r.Header.Add(k, v)
}
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
panic(err)
}
defer res.Body.Close()
var resBody T
derr := json.NewDecoder(res.Body).Decode(&resBody)
if derr != nil {
panic(derr)
}
return res.StatusCode, resBody
}
func PostRequestForResponse[T any](url string, body []byte, headers map[string]string) (int, T) {
return RequestForResponse[T]("POST", url, body, headers)
}
func ToSnakeCase(str string) string {
s := regexp.MustCompile("(.)([A-Z][a-z]+)").ReplaceAllString(str, "${1}_${2}")
s = regexp.MustCompile("([a-z0-9])([A-Z])").ReplaceAllString(s, "${1}_${2}")
return strings.ToLower(s)
}