-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops.go
More file actions
82 lines (75 loc) · 1.81 KB
/
Copy pathops.go
File metadata and controls
82 lines (75 loc) · 1.81 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
// Package any has general purpose utility functions for working with interfaces and generic types.
package anyutil
import (
"cmp"
"reflect"
)
// If returns the first item if cond is not a zero value (like false), or the second item if it is the zero value.
func If[T1 comparable, T any](cond T1, i1 T, i2 T) T {
if cond != Zero[T1]() {
return i1
} else {
return i2
}
}
// Or returns the first of its arguments that is not equal to the zero value.
// If no argument is non-zero, it returns the zero value.
func Or[T comparable](vals ...T) T {
return cmp.Or(vals...)
}
// Zero returns the zero value of a type.
func Zero[T any]() T {
var v T
return v
}
// IsNil is a safe test for nil for any kind of variable, and will not panic
// If i points to a nil object, IsNil will return true, as opposed to i==nil which will return false
func IsNil(i any) bool {
if i == nil {
return true
}
v := reflect.ValueOf(i)
k := v.Kind()
switch k {
case reflect.Chan:
fallthrough
case reflect.Func:
fallthrough
case reflect.Interface:
fallthrough
case reflect.Map:
fallthrough
case reflect.Ptr:
fallthrough
case reflect.Slice:
return v.IsNil()
}
return false
}
// IsInteger returns true if the given value is a variant of an integer type.
func IsInteger(val interface{}) bool {
t := reflect.TypeOf(val)
if t == nil {
return false
}
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
default:
return false
}
}
// IsFloat returns true if the given value is a float32 or float64.
func IsFloat(val interface{}) bool {
t := reflect.TypeOf(val)
if t == nil {
return false
}
switch t.Kind() {
case reflect.Float32, reflect.Float64:
return true
default:
return false
}
}