Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
442 lines (370 loc) · 9.26 KB
/
Copy pathutil.go
File metadata and controls
442 lines (370 loc) · 9.26 KB
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
437
438
439
440
441
442
package sugar
import (
"context"
"fmt"
"time"
)
// Must wraps a function call to panics if second argument is error or false, returns the value otherwise.
func Must[T any](val T, err error) T {
if err != nil {
panic(err)
}
return val
}
// Must0 has the same behavior as Must, but callback returns no variable.
func Must0(err error) {
if err != nil {
panic(err)
}
}
// Must1 is an alias to Must
func Must1[T any](val T, err error) T {
return Must(val, err)
}
// Must2 has the same behavior as Must, but callback returns 2 variables.
func Must2[T any, R any](val1 T, val2 R, err error) (T, R) {
if err != nil {
panic(err)
}
return val1, val2
}
// Must3 has the same behavior as Must, but callback returns 3 variables.
func Must3[T any, R any, S any](val1 T, val2 R, val3 S, err error) (T, R, S) {
if err != nil {
panic(err)
}
return val1, val2, val3
}
// TryFunc calls the function and return false in case of error and panic.
func TryFunc(callback func() error) bool {
defer func() {
recover()
}()
return callback() == nil
}
// Try1 calls the function and return false in case of error and panic.
func Try1[T any](callback func() (T, error)) bool {
defer func() {
recover()
}()
_, err := callback()
return err == nil
}
// Try2 calls the function and return false in case of error and panic.
func Try2[T, R any](callback func() (T, R, error)) bool {
defer func() {
recover()
}()
_, _, err := callback()
return err == nil
}
// TryOr calls the function and return a default value in case of error and on panic.
func TryOr[T any](callback func() (T, error), fallback T) T {
defer func() {
recover()
}()
value, err := callback()
if err != nil {
return fallback
}
return value
}
// TryOr1 calls the function and return a default value in case of error and on panic.
func TryOr1[T any](callback func() (T, error), fallback T) T {
return TryOr(callback, fallback)
}
// TryOr2 calls the function and return a default value in case of error and on panic.
func TryOr2[T, R any](callback func() (T, R, error), fallback1 T, fallback2 R) (T, R) {
defer func() {
recover()
}()
val1, val2, err := callback()
if err != nil {
return fallback1, fallback2
}
return val1, val2
}
// TryCatch calls the function and calls the catch function in case of error.
func TryCatch(callback func() error, catch func()) bool {
defer func() {
if r := recover(); r != nil {
catch()
}
}()
if err := callback(); err != nil {
catch()
return false
}
return true
}
// TryCatchWithErrorValue calls the function and calls the catch function in case of error.
func TryCatchWithErrorValue(callback func() error, catch func(any)) bool {
defer func() {
if r := recover(); r != nil {
catch(r)
}
}()
if err := callback(); err != nil {
catch(err)
return false
}
return true
}
// Validate calls the predicate function and returns an error if false.
func Validate(ok bool, format string, args ...any) error {
if !ok {
return fmt.Errorf(format, args...)
}
return nil
}
// ToPtr returns a pointer to the value.
func ToPtr[T any](x T) *T {
return &x
}
// FromPtr returns the value pointed to by the pointer.
// Returns zero value if the pointer is nil.
func FromPtr[T any](x *T) T {
if x == nil {
var zero T
return zero
}
return *x
}
// FromPtrOr returns the value pointed to by the pointer or fallback.
func FromPtrOr[T any](x *T, fallback T) T {
if x == nil {
return fallback
}
return *x
}
// IsNil checks if a value is nil or if it's an interface holding a nil value.
func IsNil(x any) bool {
return x == nil
}
// IsNotNil checks if a value is not nil.
func IsNotNil(x any) bool {
return !IsNil(x)
}
// Empty returns a zero value of type T.
func Empty[T any]() T {
var t T
return t
}
// CoalesceOrEmpty returns the first non-empty value.
func CoalesceOrEmpty[T comparable](values ...T) T {
var zero T
for _, v := range values {
if v != zero {
return v
}
}
return zero
}
// Coalesce returns the first non-zero value.
func Coalesce[T comparable](values ...T) (T, bool) {
var zero T
for _, v := range values {
if v != zero {
return v, true
}
}
return zero, false
}
// IfThen returns the result of the callback if condition is true, otherwise returns the zero value.
func IfThen[T any](condition bool, callback func() T) *Optional[T] {
if condition {
return &Optional[T]{value: callback(), present: true}
}
return &Optional[T]{present: false}
}
// IfF returns the result of the callback if condition is true, otherwise returns the zero value.
func IfF[T any](condition bool, callback func() T) T {
if condition {
return callback()
}
var zero T
return zero
}
// SwitchCase creates a switch-case like structure.
func SwitchCase[T comparable, R any](predicate T) *SwitchCaseStruct[T, R] {
var result R
return &SwitchCaseStruct[T, R]{
predicate: predicate,
result: result,
found: false,
}
}
// AsyncRun runs given function in a goroutine and returns the result in a channel.
func AsyncRun[T any](callback func() T) <-chan T {
ch := make(chan T, 1)
go func() {
defer close(ch)
ch <- callback()
}()
return ch
}
// AsyncErr runs given function in a goroutine and returns the result and error in channels.
func AsyncErr[T any](callback func() (T, error)) (<-chan T, <-chan error) {
ch := make(chan T, 1)
errCh := make(chan error, 1)
go func() {
defer func() {
close(ch)
close(errCh)
}()
result, err := callback()
ch <- result
errCh <- err
}()
return ch, errCh
}
// DebounceFunc creates a debounced function that delays invoking the callback.
func DebounceFunc[T any](callback func(...T), delay time.Duration) func(...T) {
var timer *time.Timer
return func(args ...T) {
if timer != nil {
timer.Stop()
}
timer = time.AfterFunc(delay, func() {
callback(args...)
})
}
}
// Throttle creates a throttled function that only invokes the callback at most once per duration.
func Throttle[T any](callback func(...T), duration time.Duration) func(...T) {
var lastCall time.Time
return func(args ...T) {
now := time.Now()
if now.Sub(lastCall) >= duration {
lastCall = now
callback(args...)
}
}
}
// Times invokes the callback n times, returning an array of the results.
func Times[T any](count int, callback func(int) T) []T {
if count <= 0 {
return []T{}
}
result := make([]T, count)
for i := 0; i < count; i++ {
result[i] = callback(i)
}
return result
}
// Repeat builds a slice with N copies of initial value.
func Repeat[T any](count int, initial T) []T {
if count <= 0 {
return []T{}
}
result := make([]T, count)
for i := range result {
result[i] = initial
}
return result
}
// Optional represents an optional value.
type Optional[T any] struct {
value T
present bool
}
// IsPresent returns true if the optional contains a value.
func (o *Optional[T]) IsPresent() bool {
return o.present
}
// IsEmpty returns true if the optional is empty.
func (o *Optional[T]) IsEmpty() bool {
return !o.present
}
// Get returns the value if present, otherwise returns zero value.
func (o *Optional[T]) Get() T {
return o.value
}
// OrElse returns the value if present, otherwise returns the fallback.
func (o *Optional[T]) OrElse(fallback T) T {
if o.present {
return o.value
}
return fallback
}
// OrElseF returns the value if present, otherwise calls the fallback function.
func (o *Optional[T]) OrElseF(callback func() T) T {
if o.present {
return o.value
}
return callback()
}
// SwitchCaseStruct represents a switch-case structure.
type SwitchCaseStruct[T comparable, R any] struct {
predicate T
result R
found bool
}
// Case adds a case to the switch.
func (s *SwitchCaseStruct[T, R]) Case(value T, result R) *SwitchCaseStruct[T, R] {
if !s.found && s.predicate == value {
s.result = result
s.found = true
}
return s
}
// CaseF adds a case with a function to the switch.
func (s *SwitchCaseStruct[T, R]) CaseF(value T, callback func() R) *SwitchCaseStruct[T, R] {
if !s.found && s.predicate == value {
s.result = callback()
s.found = true
}
return s
}
// Default returns the result or the default value.
func (s *SwitchCaseStruct[T, R]) Default(result R) R {
if s.found {
return s.result
}
return result
}
// DefaultF returns the result or calls the default function.
func (s *SwitchCaseStruct[T, R]) DefaultF(callback func() R) R {
if s.found {
return s.result
}
return callback()
}
// WaitFor waits for a condition to be true.
func WaitFor(condition func() bool, timeout time.Duration, interval time.Duration) bool {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
if condition() {
return true
}
select {
case <-ctx.Done():
return false
case <-ticker.C:
continue
}
}
}
// WaitForWithContext waits for a condition to be true with context.
func WaitForWithContext(ctx context.Context, condition func() bool, timeout time.Duration, interval time.Duration) (iterations int, duration time.Duration, ok bool) {
start := time.Now()
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
iterations++
if condition() {
return iterations, time.Since(start), true
}
select {
case <-timeoutCtx.Done():
return iterations, time.Since(start), false
case <-ticker.C:
continue
}
}
}