-
Notifications
You must be signed in to change notification settings - Fork 32
/
value.go
230 lines (199 loc) · 3.61 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
package stats
import (
"math"
"strconv"
"time"
)
type Value struct {
typ Type
pad int32
bits uint64
}
func ValueOf(v interface{}) Value {
switch x := v.(type) {
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:
panic("stats.ValueOf received a value of unsupported type")
}
}
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)}
}
func (v Value) Type() Type {
return v.typ
}
func (v Value) Bool() bool {
return v.bits != 0
}
func (v Value) Int() int64 {
return int64(v.bits)
}
func (v Value) Uint() uint64 {
return v.bits
}
func (v Value) Float() float64 {
return math.Float64frombits(v.bits)
}
func (v Value) Duration() time.Duration {
return time.Duration(v.bits)
}
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")
}
}
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 Type int32
const (
Null Type = iota
Bool
Int
Uint
Float
Duration
)
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>"
}
}
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
}