-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.go
147 lines (125 loc) · 4.13 KB
/
util.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
package jsonserialization
import (
"fmt"
"math"
"reflect"
)
type numericRange struct {
min float64
max float64
allowDecimal bool
}
var (
numericTypeRanges = map[reflect.Kind]numericRange{
reflect.Int8: {math.MinInt8, math.MaxInt8, false},
reflect.Uint8: {0, math.MaxUint8, false},
reflect.Int16: {math.MinInt16, math.MaxInt16, false},
reflect.Uint16: {0, math.MaxUint16, false},
reflect.Int32: {math.MinInt32, math.MaxInt32, false},
reflect.Uint32: {0, math.MaxUint32, false},
reflect.Int64: {math.MinInt64, math.MaxInt64, false},
reflect.Uint64: {0, math.MaxUint64, false},
reflect.Float32: {-math.MaxFloat32, math.MaxFloat32, true},
reflect.Float64: {-math.MaxFloat64, math.MaxFloat64, true},
}
)
type number interface {
int | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | float32 | float64
}
// isCompatible checks if the value is compatible with the type tp.
// It intentionally excludes checking if types are pointers to allow for possibility.
func isCompatible(value interface{}, tp reflect.Type) bool {
// Can't join with lower, number types are always "convertible" just not losslessly.
if isNumericType(value) && isNumericType(tp) {
//NOTE: no need to check if number is compatible with another, always yes, just overflows
//Check if number value is TRULY compatible
return isCompatibleInt(value, tp)
}
return reflect.TypeOf(value).ConvertibleTo(tp)
}
// isNil checks if a value is nil or a nil interface, including nested pointers.
func isNil(a interface{}) bool {
if a == nil {
return true
}
val := reflect.ValueOf(a)
for val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
if val.IsNil() {
return true
}
val = val.Elem()
}
switch val.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
return val.IsNil()
}
return false
}
// as converts the value to the type T.
func as[T any](in interface{}, out T) error {
// No point in trying anything if already nil
if isNil(in) {
return nil
}
// Make sure nothing is a pointer
valValue := reflect.ValueOf(in)
for valValue.Kind() == reflect.Ptr {
valValue = valValue.Elem()
in = valValue.Interface()
}
outVal := reflect.ValueOf(out)
if outVal.Kind() != reflect.Pointer || isNil(out) {
return fmt.Errorf("out is not pointer or is nil")
}
nestedOutVal := outVal.Elem()
// Handle the case where out is a pointer to an interface
if nestedOutVal.Kind() == reflect.Interface && !nestedOutVal.IsNil() {
nestedOutVal = nestedOutVal.Elem()
}
outType := nestedOutVal.Type()
if !isCompatible(in, outType) {
return fmt.Errorf("value '%v' is not compatible with type %T", in, nestedOutVal.Interface())
}
outVal.Elem().Set(valValue.Convert(outType))
return nil
}
// isNumericType checks if the given type is a numeric type.
func isNumericType(in interface{}) bool {
if in == nil {
return false
}
tp, ok := in.(reflect.Type)
if !ok {
tp = reflect.TypeOf(in)
}
switch tp.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return true
default:
return false
}
}
// isCompatibleInt checks if the given value is compatible with the specified integer type.
// It returns true if the value falls within the valid range for the type and has no decimal places.
// Otherwise, it returns false.
func isCompatibleInt(in interface{}, tp reflect.Type) bool {
if !isNumericType(in) || !isNumericType(tp) {
return false
}
inFloat := reflect.ValueOf(in).Convert(reflect.TypeOf(float64(0))).Float()
hasDecimal := hasDecimalPlace(inFloat)
if rangeInfo, ok := numericTypeRanges[tp.Kind()]; ok {
if inFloat >= rangeInfo.min && inFloat <= rangeInfo.max {
return rangeInfo.allowDecimal || !hasDecimal
}
}
return false
}
// hasDecimalPlace checks if the given float64 value has a decimal place.
// It returns true if the fractional part of the value is greater than 0.0 (indicating a decimal).
// Otherwise, it returns false.
func hasDecimalPlace(value float64) bool {
return value != float64(int64(value))
}