-
Notifications
You must be signed in to change notification settings - Fork 3
/
int.go
187 lines (165 loc) · 5.48 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
180
181
182
183
184
185
186
187
// Copyright (c) 2014 Dmitry Ponomarev <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gocast
import (
"reflect"
"strconv"
"strings"
)
// ReflectToInt64 returns int64 from reflection
func ReflectToInt64(v reflect.Value) int64 {
switch v.Kind() {
case reflect.String:
var val int64
var strVal = v.String()
if strings.Contains(strVal, ".") || strings.Contains(strVal, "e") || strings.Contains(strVal, "E") {
fval, _ := strconv.ParseFloat(strVal, 64)
val = int64(fval)
} else {
val, _ = strconv.ParseInt(strVal, 10, 64)
}
return val
case reflect.Bool:
if v.Bool() {
return 1
}
return 0
case reflect.Slice, reflect.Array:
switch v.Type().Elem().Kind() {
case reflect.Uint8:
var val int64
str := string(v.Interface().([]byte))
if strings.Contains(str, ".") || strings.Contains(str, "e") || strings.Contains(str, "E") {
fval, _ := strconv.ParseFloat(str, 64)
val = int64(fval)
} else {
val, _ = strconv.ParseInt(str, 10, 64)
}
return val
default:
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return int64(v.Uint())
case reflect.Float32, reflect.Float64:
return int64(v.Float())
}
return 0
}
// ToInt64 from any other basic types
//
// Deprecated: Use Number[int64](v) or Int64 instead
func ToInt64(v any) int64 {
return Number[int64](v)
}
// ToInt32 from any other basic types
//
// Deprecated: Use Number[int32](v) or Int32 instead
func ToInt32(v any) int32 {
return int32(ToInt64(v))
}
// ToInt16 from any other basic types
//
// Deprecated: Use Number[int16](v) or Int16 instead
func ToInt16(v any) int16 {
return int16(ToInt64(v))
}
// ToInt from any other basic types
//
// Deprecated: Use Number[int](v) or Int instead
func ToInt(v any) int {
return int(ToInt64(v))
}
// ToUint64ByReflect returns uint64 from reflection
func ToUint64ByReflect(v reflect.Value) uint64 {
switch v.Kind() {
case reflect.String:
var val uint64
if strings.Contains(v.Interface().(string), ".") {
fval, _ := strconv.ParseFloat(v.Interface().(string), 64)
val = uint64(fval)
} else {
val, _ = strconv.ParseUint(v.Interface().(string), 10, 64)
}
return val
case reflect.Bool:
if v.Bool() {
return 1
}
return 0
case reflect.Slice, reflect.Array:
switch v.Type().Elem().Kind() {
case reflect.Uint8:
var val uint64
str := string(v.Interface().([]byte))
if strings.Contains(str, ".") || strings.Contains(str, "e") || strings.Contains(str, "E") {
fval, _ := strconv.ParseFloat(str, 64)
val = uint64(fval)
} else {
val, _ = strconv.ParseUint(str, 10, 64)
}
return val
default:
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return uint64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uint64(v.Uint())
case reflect.Float32, reflect.Float64:
return uint64(v.Float())
}
return 0
}
// ToUint64 from any other basic types
//
// Deprecated: Use Number[uint64](v) or Uint64 instead
func ToUint64(v any) uint64 { return Number[uint64](v) }
// ToUint32 from any other basic types
//
// Deprecated: Use Number[uint32](v) or Uint32 instead
func ToUint32(v any) uint32 { return uint32(ToUint64(v)) }
// ToUint32 from any other basic types
//
// Deprecated: Use Number[uint16](v) or Uint16 instead
func ToUint16(v any) uint16 { return uint16(ToUint64(v)) }
// ToUint from any other basic types
//
// Deprecated: Use Number[uint](v) or Uint instead
func ToUint(v any) uint { return uint(ToUint64(v)) }
// Int from any other basic type
func Int(v any) int { return Number[int](v) }
// Int8 from any other basic type
func Int8(v any) int8 { return Number[int8](v) }
// Int16 from any other basic type
func Int16(v any) int16 { return Number[int16](v) }
// Int32 from any other basic type
func Int32(v any) int32 { return Number[int32](v) }
// Int64 from any other basic type
func Int64(v any) int64 { return Number[int64](v) }
// Uint from any other basic type
func Uint(v any) uint { return Number[uint](v) }
// Uint8 from any other basic type
func Uint8(v any) uint8 { return Number[uint8](v) }
// Uint16 from any other basic type
func Uint16(v any) uint16 { return Number[uint16](v) }
// Uint32 from any other basic type
func Uint32(v any) uint32 { return Number[uint32](v) }
// Uint64 from any other basic type
func Uint64(v any) uint64 { return Number[uint64](v) }