-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric.go
216 lines (205 loc) · 4.98 KB
/
numeric.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package senml
import (
"fmt"
"time"
)
// Numeric represents a numeric value. This can be any integer or floating point
// type, or a Decimal fraction.
// Numeric is equal to the empty interface, but using it for anything other than
// those types will result in a panic.
type Numeric interface{}
// sumNumeric adds two Numeric types and returns the sum.
func sumNumeric(a, b Numeric) Numeric {
switch a.(type) {
case nil:
return b
case int, int8, int16, int32, int64:
return addToNumericInt(a, b)
case uint, uint8, uint16, uint32, uint64:
return addToNumericUint(a, b)
case float32, float64, Decimal, *Decimal:
return numericToFloat64(a) + numericToFloat64(b)
default:
panic(fmt.Sprintf("invalid value type: %T", a))
}
}
// addToNumericInt sums two numeric values, the first of which is an integer type.
// An attempt is made to preserve integer types, but this is not guaranteed.
func addToNumericInt(a, b Numeric) Numeric {
switch b.(type) {
case nil:
return a
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return numericToInt64(a) + numericToInt64(b)
case float32, float64, Decimal, *Decimal:
return numericToFloat64(a) + numericToFloat64(b)
default:
panic(fmt.Sprintf("invalid value type: %T", b))
}
}
// addToNumericInt sums two numeric values, the first of which is an unsigned integer type.
// An attempt is made to preserve unsigned integer types, but this is not guaranteed.
func addToNumericUint(a, b Numeric) Numeric {
switch b.(type) {
case nil:
return a
case int, int8, int16, int32, int64:
return numericToInt64(a) + numericToInt64(b)
case uint, uint8, uint16, uint32, uint64:
return numericToUint64(a) + numericToUint64(b)
case float32, float64, Decimal, *Decimal:
return numericToFloat64(a) + numericToFloat64(b)
default:
panic(fmt.Sprintf("invalid value type: %T", b))
}
}
// timeToNumeric converts a time.Time to a Numeric value.
func timeToNumeric(t time.Time) Numeric {
if t.IsZero() {
return nil
}
if t.Nanosecond() == 0 {
return t.Unix()
}
return float64(t.UnixNano()) / 1e9
}
// numericToTime converts a Numeric value to a time.Time.
func numericToTime(v Numeric) time.Time {
switch v.(type) {
case nil:
return time.Time{}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return intToTime(numericToInt64(v))
case float32, float64, Decimal, *Decimal:
return floatToTime(numericToFloat64(v))
default:
panic("unsupported type")
}
}
// numericToDuration converts a Numeric value to a duration.
func numericToDuration(v Numeric) time.Duration {
switch v.(type) {
case nil:
return 0
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return time.Duration(numericToInt64(v)) * time.Second
case float32, float64, Decimal, *Decimal:
return floatToDuration(numericToFloat64(v))
default:
panic("unsupported type")
}
}
// numericToInt64 converts a numeric value to a 64-bit integer.
// Fractional parts will be lost in the conversion.
func numericToInt64(v Numeric) int64 {
switch i := v.(type) {
case nil:
return 0
case int:
return int64(i)
case int8:
return int64(i)
case int16:
return int64(i)
case int32:
return int64(i)
case int64:
return i
case uint:
return int64(i)
case uint8:
return int64(i)
case uint16:
return int64(i)
case uint32:
return int64(i)
case uint64:
return int64(i)
case float32:
return int64(i)
case float64:
return int64(i)
case Decimal:
return int64(i.Int())
case *Decimal:
return int64(i.Int())
default:
panic(fmt.Sprintf("invalid value type: %T", i))
}
}
// numericToUint64 converts a Numeric value to a 64-bit unsigned integer.
// Fractional parts and signs will be lost in the conversion.
func numericToUint64(v Numeric) uint64 {
switch i := v.(type) {
case nil:
return 0
case int:
return uint64(i)
case int8:
return uint64(i)
case int16:
return uint64(i)
case int32:
return uint64(i)
case int64:
return uint64(i)
case uint:
return uint64(i)
case uint8:
return uint64(i)
case uint16:
return uint64(i)
case uint32:
return uint64(i)
case uint64:
return i
case float32:
return uint64(i)
case float64:
return uint64(i)
case Decimal:
return uint64(i.Int())
case *Decimal:
return uint64(i.Int())
default:
panic(fmt.Sprintf("invalid value type: %T", i))
}
}
// numericToFloat64 converts a Numeric value to a 64-bit floating point value.
// Precision may be lost in the conversion.
func numericToFloat64(v Numeric) float64 {
switch f := v.(type) {
case nil:
return 0
case int:
return float64(f)
case int8:
return float64(f)
case int16:
return float64(f)
case int32:
return float64(f)
case int64:
return float64(f)
case uint:
return float64(f)
case uint8:
return float64(f)
case uint16:
return float64(f)
case uint32:
return float64(f)
case uint64:
return float64(f)
case float32:
return float64(f)
case float64:
return f
case Decimal:
return f.Float()
case *Decimal:
return f.Float()
default:
panic(fmt.Sprintf("invalid value type: %T", f))
}
}