-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers.go
66 lines (54 loc) · 1.43 KB
/
pointers.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
package conv
// PtrFrom gets pointer from any value or constant v.
func PtrFrom[T any](v T) *T {
return &v
}
// FromPtrOr dereferences pointer p or use fallback value ifNil if pointer is nil.
func FromPtrOr[T any](p *T, ifNil T) T {
if p != nil {
return *p
}
return ifNil
}
// FromPtrOrFunc dereferences pointer p or use fallback function ifNilFn call result if pointer p is nil.
//
// The main difference from [FromPtrOr] - the fallback value is lazy initialized here.
func FromPtrOrFunc[T any](p *T, ifNilFn func() T) T {
if p != nil {
return *p
}
return ifNilFn()
}
// OmitEmpty returns nil pointer if value *p is empty (or default).
//
// Is used to get nil pointer instead of empty string or zero integer.
func OmitEmpty[T comparable](p *T) *T {
if p != nil {
var EMPTY T
if *p == EMPTY {
return nil
}
}
return p // as is
}
// FirstNonNil gets first non-nil pointer.
// It works similar to SQL COALESCE function.
func FirstNonNil[T any](pp ...*T) *T {
return Coalesce(pp...)
}
// Coalesce gets first non-empty value.
// Similar to [FirstNonNil] but works with any types, not just pointers.
func Coalesce[T comparable](vv ...T) T {
out, _ := CoalesceEx(vv...)
return out // ignore OK status
}
// CoalesceEx gets first non-empty value with OK status.
func CoalesceEx[T comparable](vv ...T) (out T, ok bool) {
for _, v := range vv {
if v == out {
continue // is empty
}
return v, true
}
return // out, false
}