-
Notifications
You must be signed in to change notification settings - Fork 69
/
helpers.go
260 lines (226 loc) · 5.97 KB
/
helpers.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
package moira
import (
"bytes"
"math"
"strings"
"time"
"github.com/go-playground/validator/v10"
)
// BytesScanner allows to scan for subslices separated by separator.
type BytesScanner struct {
source []byte
index int
separator byte
emitEmptySlice bool
}
// HasNext checks if next subslice available or not.
func (it *BytesScanner) HasNext() bool {
return it.index < len(it.source) || it.emitEmptySlice
}
// Next returns available subslice and advances the scanner to next slice.
func (it *BytesScanner) Next() (result []byte) {
if it.emitEmptySlice {
it.emitEmptySlice = false
result = make([]byte, 0)
return result
}
scannerIndex := it.index
separatorIndex := bytes.IndexByte(it.source[scannerIndex:], it.separator)
if separatorIndex < 0 {
result = it.source[scannerIndex:]
it.index = len(it.source)
} else {
separatorIndex += scannerIndex
result = it.source[scannerIndex:separatorIndex]
if separatorIndex == len(it.source)-1 {
it.emitEmptySlice = true
}
it.index = separatorIndex + 1
}
return result
}
// NewBytesScanner slices bytes into all subslices separated by separator and returns a scanner
// which allows scanning for these subslices.
func NewBytesScanner(bytes []byte, separator byte) *BytesScanner {
return &BytesScanner{
source: bytes,
index: 0,
separator: separator,
emitEmptySlice: false,
}
}
// Int64ToTime returns time.Time from int64.
func Int64ToTime(timeStamp int64) time.Time {
return time.Unix(timeStamp, 0).UTC()
}
// UseString gets pointer value of string or default string if pointer is nil.
func UseString(str *string) string {
if str == nil {
return ""
}
return *str
}
// UseFloat64 gets pointer value of float64 or default float64 if pointer is nil.
func UseFloat64(f *float64) float64 {
if f == nil {
return 0
}
return *f
}
// IsFiniteNumber checks float64 for Inf and NaN. If it is then float64 is not valid.
func IsFiniteNumber(val float64) bool {
return !(math.IsNaN(val) || math.IsInf(val, 0))
}
// Subset return whether first is a subset of second.
func Subset(first, second []string) bool {
set := make(map[string]bool)
for _, value := range second {
set[value] = true
}
for _, value := range first {
if !set[value] {
return false
}
}
return true
}
// GetStringListsDiff returns the members of the set resulting from the difference between the first set and all the successive lists.
func GetStringListsDiff(stringLists ...[]string) []string {
if len(stringLists) == 0 {
return []string{}
}
leftValues := make(map[string]bool)
for _, value := range stringLists[0] {
leftValues[value] = true
}
for _, stringList := range stringLists[1:] {
for _, value := range stringList {
delete(leftValues, value)
}
}
result := make([]string, 0)
for _, value := range stringLists[0] {
if _, ok := leftValues[value]; ok {
result = append(result, value)
}
}
return result
}
// GetStringListsUnion returns the union set of stringLists.
func GetStringListsUnion(stringLists ...[]string) []string {
if len(stringLists) == 0 {
return []string{}
}
values := make([]string, 0)
uniqueValues := make(map[string]bool)
for _, stringList := range stringLists {
for _, value := range stringList {
if _, ok := uniqueValues[value]; !ok {
values = append(values, value)
uniqueValues[value] = true
}
}
}
return values
}
// GetTriggerListsDiff returns the members of the set resulting from the difference between the first set and all the successive lists.
func GetTriggerListsDiff(triggerLists ...[]*Trigger) []*Trigger {
if len(triggerLists) == 0 {
return []*Trigger{}
}
leftValues := make(map[string]bool)
for _, value := range triggerLists[0] {
if value != nil {
leftValues[value.ID] = true
}
}
for _, triggerList := range triggerLists[1:] {
for _, trigger := range triggerList {
if trigger != nil {
delete(leftValues, trigger.ID)
}
}
}
result := make([]*Trigger, 0)
for _, value := range triggerLists[0] {
if value == nil {
continue
}
if _, ok := leftValues[value.ID]; ok {
result = append(result, value)
}
}
return result
}
// ChunkSlice gets slice of strings and chunks it to a given size. It returns a batch of chunked lists.
func ChunkSlice(original []string, chunkSize int) (divided [][]string) {
if chunkSize < 1 {
return
}
for i := 0; i < len(original); i += chunkSize {
end := i + chunkSize
if end > len(original) {
end = len(original)
}
divided = append(divided, original[i:end])
}
return
}
func RoundToNearestRetention(ts, retention int64) int64 {
return (ts + retention/2) / retention * retention
}
func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
// ReplaceSubstring removes one substring between the beginning and end substrings and replaces it with a replaced.
func ReplaceSubstring(str, begin, end, replaced string) string {
result := str
startIndex := strings.Index(str, begin)
if startIndex != -1 {
startIndex += len(begin)
endIndex := strings.Index(str[startIndex:], end)
if endIndex != -1 {
endIndex += len(str[:startIndex])
result = str[:startIndex] + replaced + str[endIndex:]
}
}
return result
}
type Comparable interface {
Less(other Comparable) (bool, error)
}
// Merge is a generic function that performs a merge of two sorted arrays into one sorted array.
func MergeToSorted[T Comparable](arr1, arr2 []T) ([]T, error) {
merged := make([]T, 0, len(arr1)+len(arr2))
i, j := 0, 0
for i < len(arr1) && j < len(arr2) {
less, err := arr1[i].Less(arr2[j])
if err != nil {
return nil, err
}
if less {
merged = append(merged, arr1[i])
i++
} else {
merged = append(merged, arr2[j])
j++
}
}
for i < len(arr1) {
merged = append(merged, arr1[i])
i++
}
for j < len(arr2) {
merged = append(merged, arr2[j])
j++
}
return merged, nil
}
// ValidateStruct is a default generic function that uses a validator to validate structure fields.
func ValidateStruct(s any) error {
validator := validator.New()
return validator.Struct(s)
}