-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.go
193 lines (161 loc) · 5.47 KB
/
misc.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Defense Unicorns
package helpers
import (
"context"
"errors"
"fmt"
"maps"
"math"
"reflect"
"regexp"
"strings"
"time"
)
// BoolPtr returns a pointer to a bool.
func BoolPtr(b bool) *bool {
return &b
}
// RetryWithContext retries a function until it succeeds, the timeout is reached, or the context is done.
// The delay between attempts increases exponentially as (2^(attempt-1)) * delay.
// For example, with a delay of one second and three attempts, the timing would be:
// - First attempt: immediate
// - Second attempt: after one second
// - Third attempt: after two seconds
func RetryWithContext(ctx context.Context, fn func() error, attempts int, delay time.Duration, logger func(format string, args ...any)) error {
if attempts < 1 {
return errors.New("invalid number of attempts, must be at least 1")
}
var err error
timer := time.NewTimer(0)
defer timer.Stop()
for r := 0; r < attempts; r++ {
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
err = fn()
if err == nil {
return nil
}
logger("Attempt (%d/%d) failed with: %s", r+1, attempts, err.Error())
// No reason to wait when we aren't going to retry again
if r+1 == attempts {
return err
}
pow := math.Pow(2, float64(r))
backoff := delay * time.Duration(pow)
logger("Retrying in %s", backoff)
timer.Reset(backoff)
}
}
return err
}
// Retry retries a function until it succeeds, the timeout is reached, or the context is done.
// The delay between attempts increases exponentially as (2^(attempt-1)) * delay.
// For example, with a delay of one second and three attempts, the timing would be:
// - First attempt: immediate
// - Second attempt: after one second
// - Third attempt: after two seconds
func Retry(fn func() error, attempts int, delay time.Duration, logger func(format string, args ...any)) error {
return RetryWithContext(context.Background(), fn, attempts, delay, logger)
}
// TransformMapKeys takes a map and transforms its keys using the provided function.
func TransformMapKeys[T any](m map[string]T, transform func(string) string) (r map[string]T) {
r = map[string]T{}
for key, value := range m {
r[transform(key)] = value
}
return r
}
// TransformAndMergeMap transforms keys in both maps then merges map m2 with m1 overwriting common values with m2's values.
func TransformAndMergeMap[T any](m1, m2 map[string]T, transform func(string) string) (r map[string]T) {
r = TransformMapKeys(m1, transform)
mt2 := TransformMapKeys(m2, transform)
maps.Copy(r, mt2)
return r
}
// MergeMapRecursive recursively (nestedly) merges map m2 with m1 overwriting common values with m2's values.
func MergeMapRecursive(m1, m2 map[string]interface{}) (r map[string]interface{}) {
r = map[string]interface{}{}
for key, value := range m1 {
r[key] = value
}
for key, value := range m2 {
if value, ok := value.(map[string]interface{}); ok {
if nestedValue, ok := r[key]; ok {
if nestedValue, ok := nestedValue.(map[string]interface{}); ok {
r[key] = MergeMapRecursive(nestedValue, value)
continue
}
}
}
r[key] = value
}
return r
}
// MatchRegex wraps a get function around a substring match.
func MatchRegex(regex *regexp.Regexp, str string) (func(string) string, error) {
// Validate the string.
matches := regex.FindStringSubmatch(str)
// Parse the string into its components.
get := func(name string) string {
return matches[regex.SubexpIndex(name)]
}
if len(matches) == 0 {
return get, fmt.Errorf("unable to match against %s", str)
}
return get, nil
}
// IsNotZeroAndNotEqual is used to test if a struct has zero values or is equal values with another struct
func IsNotZeroAndNotEqual[T any](given T, equal T) bool {
givenValue := reflect.ValueOf(given)
equalValue := reflect.ValueOf(equal)
if givenValue.NumField() != equalValue.NumField() {
return true
}
for i := 0; i < givenValue.NumField(); i++ {
if !givenValue.Field(i).IsZero() &&
givenValue.Field(i).CanInterface() &&
givenValue.Field(i).Interface() != equalValue.Field(i).Interface() {
return true
}
}
return false
}
// MergeNonZero is used to merge non-zero overrides from one struct into another of the same type
func MergeNonZero[T any](original T, overrides T) T {
originalValue := reflect.ValueOf(&original)
overridesValue := reflect.ValueOf(&overrides)
for i := 0; i < originalValue.Elem().NumField(); i++ {
if !overridesValue.Elem().Field(i).IsZero() &&
overridesValue.Elem().Field(i).CanSet() {
overrideField := overridesValue.Elem().Field(i)
originalValue.Elem().Field(i).Set(overrideField)
}
}
return originalValue.Elem().Interface().(T)
}
// MergePathAndValueIntoMap takes a path in dot notation as a string and a value (also as a string for simplicity),
// then merges this into the provided map. The value can be any type.
func MergePathAndValueIntoMap(m map[string]any, path string, value any) error {
pathParts := strings.Split(path, ".")
current := m
for i, part := range pathParts {
if i == len(pathParts)-1 {
// Set the value at the last key in the path.
current[part] = value
} else {
if _, exists := current[part]; !exists {
// If the part does not exist, create a new map for it.
current[part] = make(map[string]any)
}
nextMap, ok := current[part].(map[string]any)
if !ok {
return fmt.Errorf("conflict at %q, expected map but got %T", strings.Join(pathParts[:i+1], "."), current[part])
}
current = nextMap
}
}
return nil
}