-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
179 lines (170 loc) · 3.74 KB
/
int.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package into
import (
"encoding"
"fmt"
"reflect"
"strconv"
)
// CanInt returns true if the given value can be coerced to a signed integer.
// [Int] will succeed without panicking if CanInt returns true.
//
// See: [Int] for supported types.
func CanInt(x any, options ...Option) bool {
switch x := x.(type) {
case int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8, 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.Atoi(str)
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.Atoi(string(text))
return err == nil
}
if !should(options, skipReflect) {
rv := reflect.ValueOf(x)
for rv.Kind() == reflect.Pointer {
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return true
case reflect.String:
if !should(options, convertStrings) {
return false
}
if should(options, skipMarshalCheck) {
return true
}
_, err := strconv.Atoi(rv.String())
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.Atoi(string(rv.Bytes()))
return err == nil
case reflect.Int32: // []rune
if should(options, skipMarshalCheck) {
return true
}
_, err := strconv.Atoi(string(rv.Convert(runesType).Interface().([]rune)))
return err == nil
}
return false
}
}
return false
}
// Int coerces x into a signed integer, supporting the following types:
// - int, int64, int32 (and rune), int16, int8
// - *int, *int64, *int32 (and *rune), *int16, *int8
// - types with an underlying signed integer value or pointers to such types
// - given [WithConvertStrings], any string-like type supported by [String]
// - nil
//
// Int will panic with ErrInvalid if the value cannot be coerced.
func Int(x any, options ...Option) int {
switch x := x.(type) {
case int:
return x
case int64:
return int(x)
case int32:
return int(x)
case int16:
return int(x)
case int8:
return int(x)
case *int:
if x == nil {
goto fallback
}
return *x
case *int64:
if x == nil {
goto fallback
}
return int(*x)
case *int32:
if x == nil {
goto fallback
}
return int(*x)
case *int16:
if x == nil {
goto fallback
}
return int(*x)
case *int8:
if x == nil {
goto fallback
}
return int(*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.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return int(rv.Int())
}
}
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.Atoi(str)
if err != nil {
panic(ErrInvalid{Value: x, Type: "int", Cause: err})
}
return n
}
panic(ErrInvalid{Value: x, Type: "int"})
fallback:
var null int
for _, opt := range options {
if opt, ok := opt.(fallbackValue); ok {
null, ok = opt.x.(int)
if !ok {
panic(fmt.Errorf("invalid fallback value: %v (type of %T), must be %s", opt, opt, "int"))
}
break
}
}
return null
}