-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
53 lines (47 loc) · 1.58 KB
/
value.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
package truthy
import (
"reflect"
)
// ValueAny returns the truthy value of anything.
// If the value's type has a Bool() bool method, the method is called and returned.
// If the type has an IsZero() bool method, the opposite value is returned.
// Slices and maps are truthy if they have a length greater than zero.
// All other types are truthy if they are not their zero value.
//
// Note that the usual Go type system caveats apply around a nil pointer value not being a nil interface value.
//
// In benchmark testing,
// ValueAny is approximately 25 times slower than Value,
// and 50 times slower than native Go comparisons.
func ValueAny[T any](v T) bool {
switch m := any(v).(type) {
case interface{ Bool() bool }:
return m.Bool()
case interface{ IsZero() bool }:
return !m.IsZero()
}
return reflectValue(&v)
}
func reflectValue(vp any) bool {
switch rv := reflect.ValueOf(vp).Elem(); rv.Kind() {
case reflect.Map, reflect.Slice:
return rv.Len() != 0
default:
return !rv.IsZero()
}
}
// ValueSlice returns true if the length of the slice is greater than 0.
// Note that it does not distinguish nil slices from empty slices.
func ValueSlice[T any, S ~[]T](v S) bool {
return len(v) > 0
}
// ValueMap returns true if the length of the map is greater than 0.
// Note that it does not distinguish nil maps from empty maps.
func ValueMap[K comparable, V any, M ~map[K]V](v M) bool {
return len(v) > 0
}
// Value returns the truthy value of comparable types.
// Values are truthy if they are not equal to the zero value for the type.
func Value[T comparable](v T) bool {
return v != *new(T)
}