-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
156 lines (147 loc) · 3.32 KB
/
float.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
148
149
150
151
152
153
154
155
156
package into
import (
"encoding"
"fmt"
"reflect"
"strconv"
)
// CanFloat returns true if the given value can be coerced to a float.
// See: [Float] for supported types.
func CanFloat(x any, options ...Option) bool {
switch x := x.(type) {
case float64, float32, *float64, *float32, nil:
return true
case string, *string, []byte, []rune, fmt.Stringer:
if !should(options, convertStrings) {
return false
}
if should(options, skipMarshalCheck) {
return true
}
str := String(x)
_, err := strconv.ParseFloat(str, 64)
return err == nil
case encoding.TextMarshaler:
if !should(options, convertStrings) {
return false
}
if should(options, skipMarshalCheck) {
return true
}
text, err := x.MarshalText()
if err != nil {
return false
}
_, err = strconv.ParseFloat(string(text), 64)
return err == nil
}
if !should(options, skipReflect) {
rv := reflect.ValueOf(x)
for rv.Kind() == reflect.Pointer {
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Float64, reflect.Float32:
return true
case reflect.String:
if !should(options, convertStrings) {
return false
}
if should(options, skipMarshalCheck) {
return true
}
_, err := strconv.ParseFloat(rv.String(), 64)
return err == nil
case reflect.Slice:
if !should(options, convertStrings) {
return false
}
switch rv.Type().Elem().Kind() {
case reflect.Uint8: // []byte
if should(options, skipMarshalCheck) {
return true
}
_, err := strconv.ParseFloat(string(rv.Bytes()), 64)
return err == nil
case reflect.Int32: // []rune
if should(options, skipMarshalCheck) {
return true
}
_, err := strconv.ParseFloat(string(rv.Convert(runesType).Interface().([]rune)), 64)
return err == nil
}
return false
}
}
return false
}
// Float coerces x into a float, supporting the following types:
// - float64, float32
// - *float64, *float32
// - types with an underlying float value or pointers to such types
// - given [WithConvertStrings], any string-like type supported by [String]
// - nil
//
// Float will panic with ErrInvalid if the value cannot be coerced.
func Float(x any, options ...Option) float64 {
switch x := x.(type) {
case float64:
return x
case float32:
return float64(x)
case *float64:
if x == nil {
goto fallback
}
return *x
case *float32:
if x == nil {
goto fallback
}
return float64(*x)
case nil:
goto fallback
}
if !should(options, skipReflect) {
rv := reflect.ValueOf(x)
for rv.Kind() == reflect.Pointer {
if rv.IsNil() {
goto fallback
}
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Float64, reflect.Float32:
return rv.Float()
}
}
if should(options, convertStrings) && CanString(x) {
var str string
if should(options, skipReflect) {
str = String(x, skipReflect)
} else {
str = String(x)
}
if str == "" {
goto fallback
}
n, err := strconv.ParseFloat(str, 64)
if err != nil {
panic(ErrInvalid{Value: x, Type: "float", Cause: err})
}
return n
}
panic(ErrInvalid{Value: x, Type: "float"})
fallback:
var null float64
for _, opt := range options {
if opt, ok := opt.(fallbackValue); ok {
null, ok = opt.x.(float64)
if !ok {
panic(fmt.Errorf("invalid fallback value: %v (type of %T), must be %s", opt, opt, "float64"))
}
break
}
}
return null
}