-
Notifications
You must be signed in to change notification settings - Fork 32
/
value.go
258 lines (226 loc) · 4.89 KB
/
value.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package stats
import (
"math"
"strconv"
"time"
)
// Value is a wrapper type which is used to encapsulate underlying types (nil, bool, int, uintptr, float)
// in a single pseudo-generic type.
type Value struct {
typ Type
pad int32
bits uint64
}
// MustValueOf asserts that v's underlying Type is valid, otherwise it panics.
func MustValueOf(v Value) Value {
if v.Type() == Invalid {
panic("stats.MustValueOf received a value of unsupported type")
}
return v
}
// ValueOf inspects v's underlying type and returns a Value which encapsulates this type.
// If the underlying type of v is not supported by Value's encapsulation its Type() will return stats.Invalid.
func ValueOf(v interface{}) Value {
switch x := v.(type) {
case Value:
return x
case nil:
return Value{}
case bool:
return boolValue(x)
case int:
return intValue(x)
case int8:
return int8Value(x)
case int16:
return int16Value(x)
case int32:
return int32Value(x)
case int64:
return int64Value(x)
case uint:
return uintValue(x)
case uint8:
return uint8Value(x)
case uint16:
return uint16Value(x)
case uint32:
return uint32Value(x)
case uint64:
return uint64Value(x)
case uintptr:
return uintptrValue(x)
case float32:
return float32Value(x)
case float64:
return float64Value(x)
case time.Duration:
return durationValue(x)
default:
return Value{typ: Invalid}
}
}
func boolValue(v bool) Value {
return Value{typ: Bool, bits: boolBits(v)}
}
func intValue(v int) Value {
return int64Value(int64(v))
}
func int8Value(v int8) Value {
return int64Value(int64(v))
}
func int16Value(v int16) Value {
return int64Value(int64(v))
}
func int32Value(v int32) Value {
return int64Value(int64(v))
}
func int64Value(v int64) Value {
return Value{typ: Int, bits: uint64(v)}
}
func uintValue(v uint) Value {
return uint64Value(uint64(v))
}
func uint8Value(v uint8) Value {
return uint64Value(uint64(v))
}
func uint16Value(v uint16) Value {
return uint64Value(uint64(v))
}
func uint32Value(v uint32) Value {
return uint64Value(uint64(v))
}
func uint64Value(v uint64) Value {
return Value{typ: Uint, bits: v}
}
func uintptrValue(v uintptr) Value {
return uint64Value(uint64(v))
}
func float32Value(v float32) Value {
return float64Value(float64(v))
}
func float64Value(v float64) Value {
return Value{typ: Float, bits: math.Float64bits(v)}
}
func durationValue(v time.Duration) Value {
return Value{typ: Duration, bits: uint64(v)}
}
// Type returns the Type of this value.
func (v Value) Type() Type {
return v.typ
}
// Bool returns a bool if the underlying data for this value is zero.
func (v Value) Bool() bool {
return v.bits != 0
}
// Int returns an new int64 representation of this Value.
func (v Value) Int() int64 {
return int64(v.bits)
}
// Uint returns a uint64 representation of this Value.
func (v Value) Uint() uint64 {
return v.bits
}
// Float returns a new float64 representation of this Value.
func (v Value) Float() float64 {
return math.Float64frombits(v.bits)
}
// Duration returns a new time.Duration representation of this Value.
func (v Value) Duration() time.Duration {
return time.Duration(v.bits)
}
// Interface returns an new interface{} representation of this value.
// However, if the underlying Type is unsupported it panics.
func (v Value) Interface() interface{} {
switch v.Type() {
case Null:
return nil
case Bool:
return v.Bool()
case Int:
return v.Int()
case Uint:
return v.Uint()
case Float:
return v.Float()
case Duration:
return v.Duration()
default:
panic("unknown type found in a stats.Value")
}
}
// String returns a string representation of the underling value.
func (v Value) String() string {
switch v.Type() {
case Null:
return "<nil>"
case Bool:
return strconv.FormatBool(v.Bool())
case Int:
return strconv.FormatInt(v.Int(), 10)
case Uint:
return strconv.FormatUint(v.Uint(), 10)
case Float:
return strconv.FormatFloat(v.Float(), 'g', -1, 64)
case Duration:
return v.Duration().String()
default:
return "<unknown>"
}
}
// Type is an int32 type alias used to denote a values underlying type.
type Type int32
// Underlying Types.
const (
Null Type = iota
Bool
Int
Uint
Float
Duration
Invalid
)
// String returns the string representation of a type.
func (t Type) String() string {
switch t {
case Null:
return "<nil>"
case Bool:
return "bool"
case Int:
return "int64"
case Uint:
return "uint64"
case Float:
return "float64"
case Duration:
return "time.Duration"
default:
return "<unknown>"
}
}
// GoString implements the GoStringer interface.
func (t Type) GoString() string {
switch t {
case Null:
return "stats.Null"
case Bool:
return "stats.Bool"
case Int:
return "stats.Int"
case Uint:
return "stats.Uint"
case Float:
return "stats.Float"
case Duration:
return "stats.Duration"
default:
return "stats.Type(" + strconv.Itoa(int(t)) + ")"
}
}
func boolBits(v bool) uint64 {
if v {
return 1
}
return 0
}