| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package event |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"math" |
| 10 |
"reflect" |
| 11 |
"strconv" |
| 12 |
"time" |
| 13 |
"unsafe" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
type Label struct { |
| 18 |
Name string |
| 19 |
|
| 20 |
packed uint64 |
| 21 |
untyped interface{} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
type stringptr unsafe.Pointer |
| 26 |
|
| 27 |
|
| 28 |
type bytesptr unsafe.Pointer |
| 29 |
|
| 30 |
|
| 31 |
type int64Kind struct{} |
| 32 |
|
| 33 |
|
| 34 |
type uint64Kind struct{} |
| 35 |
|
| 36 |
|
| 37 |
type float64Kind struct{} |
| 38 |
|
| 39 |
|
| 40 |
type boolKind struct{} |
| 41 |
|
| 42 |
|
| 43 |
type durationKind struct{} |
| 44 |
|
| 45 |
|
| 46 |
func (l Label) HasValue() bool { return l.untyped != nil } |
| 47 |
|
| 48 |
|
| 49 |
func (l Label) Equal(l2 Label) bool { |
| 50 |
if l.Name != l2.Name { |
| 51 |
return false |
| 52 |
} |
| 53 |
if !l.HasValue() { |
| 54 |
return !l2.HasValue() |
| 55 |
} |
| 56 |
if !l2.HasValue() { |
| 57 |
return false |
| 58 |
} |
| 59 |
switch { |
| 60 |
case l.IsString(): |
| 61 |
return l2.IsString() && l.String() == l2.String() |
| 62 |
case l.IsInt64(): |
| 63 |
return l2.IsInt64() && l.packed == l2.packed |
| 64 |
case l.IsUint64(): |
| 65 |
return l2.IsUint64() && l.packed == l2.packed |
| 66 |
case l.IsFloat64(): |
| 67 |
return l2.IsFloat64() && l.Float64() == l2.Float64() |
| 68 |
case l.IsBool(): |
| 69 |
return l2.IsBool() && l.packed == l2.packed |
| 70 |
case l.IsDuration(): |
| 71 |
return l2.IsDuration() && l.packed == l2.packed |
| 72 |
default: |
| 73 |
return l.untyped == l2.untyped |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func Value(name string, value interface{}) Label { |
| 79 |
return Label{Name: name, untyped: value} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func (v Label) Interface() interface{} { |
| 86 |
switch { |
| 87 |
case v.IsString(): |
| 88 |
return v.String() |
| 89 |
case v.IsInt64(): |
| 90 |
return v.Int64() |
| 91 |
case v.IsUint64(): |
| 92 |
return v.Uint64() |
| 93 |
case v.IsFloat64(): |
| 94 |
return v.Float64() |
| 95 |
case v.IsBool(): |
| 96 |
return v.Bool() |
| 97 |
case v.IsDuration(): |
| 98 |
return v.Duration() |
| 99 |
default: |
| 100 |
return v.untyped |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func String(name string, s string) Label { |
| 106 |
return Label{Name: name, packed: uint64(len(s)), untyped: stringptr(unsafe.StringData(s))} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func (v Label) String() string { |
| 113 |
if sp, ok := v.untyped.(stringptr); ok { |
| 114 |
return unsafe.String((*byte)(unsafe.Pointer(sp)), int(v.packed)) |
| 115 |
} |
| 116 |
|
| 117 |
switch { |
| 118 |
case v.IsInt64(): |
| 119 |
return strconv.FormatInt(v.Int64(), 10) |
| 120 |
case v.IsUint64(): |
| 121 |
return strconv.FormatUint(v.Uint64(), 10) |
| 122 |
case v.IsFloat64(): |
| 123 |
return strconv.FormatFloat(v.Float64(), 'g', -1, 64) |
| 124 |
case v.IsBool(): |
| 125 |
if v.Bool() { |
| 126 |
return "true" |
| 127 |
} else { |
| 128 |
return "false" |
| 129 |
} |
| 130 |
default: |
| 131 |
return fmt.Sprint(v.Interface()) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (v Label) IsString() bool { |
| 137 |
_, ok := v.untyped.(stringptr) |
| 138 |
return ok |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func Bytes(name string, data []byte) Label { |
| 143 |
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&data)) |
| 144 |
return Label{Name: name, packed: uint64(hdr.Len), untyped: bytesptr(hdr.Data)} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func (v Label) Bytes() []byte { |
| 149 |
bp, ok := v.untyped.(bytesptr) |
| 150 |
if !ok { |
| 151 |
panic("Bytes called on non []byte value") |
| 152 |
} |
| 153 |
var buf []byte |
| 154 |
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) |
| 155 |
hdr.Data = uintptr(bp) |
| 156 |
hdr.Len = int(v.packed) |
| 157 |
hdr.Cap = hdr.Len |
| 158 |
return buf |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func (v Label) IsBytes() bool { |
| 163 |
_, ok := v.untyped.(bytesptr) |
| 164 |
return ok |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
func Int64(name string, u int64) Label { |
| 169 |
return Label{Name: name, packed: uint64(u), untyped: int64Kind{}} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func (v Label) Int64() int64 { |
| 175 |
if !v.IsInt64() { |
| 176 |
panic("Int64 called on non int64 value") |
| 177 |
} |
| 178 |
return int64(v.packed) |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
func (v Label) IsInt64() bool { |
| 183 |
_, ok := v.untyped.(int64Kind) |
| 184 |
return ok |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
func Uint64(name string, u uint64) Label { |
| 189 |
return Label{Name: name, packed: u, untyped: uint64Kind{}} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
func (v Label) Uint64() uint64 { |
| 195 |
if !v.IsUint64() { |
| 196 |
panic("Uint64 called on non uint64 value") |
| 197 |
} |
| 198 |
return v.packed |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func (v Label) IsUint64() bool { |
| 203 |
_, ok := v.untyped.(uint64Kind) |
| 204 |
return ok |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func Float64(name string, f float64) Label { |
| 209 |
return Label{Name: name, packed: math.Float64bits(f), untyped: float64Kind{}} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
func (v Label) Float64() float64 { |
| 215 |
if !v.IsFloat64() { |
| 216 |
panic("Float64 called on non float64 value") |
| 217 |
} |
| 218 |
return math.Float64frombits(v.packed) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
func (v Label) IsFloat64() bool { |
| 223 |
_, ok := v.untyped.(float64Kind) |
| 224 |
return ok |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
func Bool(name string, b bool) Label { |
| 229 |
if b { |
| 230 |
return Label{Name: name, packed: 1, untyped: boolKind{}} |
| 231 |
} |
| 232 |
return Label{Name: name, packed: 0, untyped: boolKind{}} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
func (v Label) Bool() bool { |
| 238 |
if !v.IsBool() { |
| 239 |
panic("Bool called on non bool value") |
| 240 |
} |
| 241 |
if v.packed != 0 { |
| 242 |
return true |
| 243 |
} |
| 244 |
return false |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
func (v Label) IsBool() bool { |
| 249 |
_, ok := v.untyped.(boolKind) |
| 250 |
return ok |
| 251 |
} |
| 252 |
|
| 253 |
func Duration(name string, d time.Duration) Label { |
| 254 |
return Label{Name: name, packed: uint64(d), untyped: durationKind{}} |
| 255 |
} |
| 256 |
|
| 257 |
func (v Label) Duration() time.Duration { |
| 258 |
if !v.IsDuration() { |
| 259 |
panic("Duration called on non-Duration value") |
| 260 |
} |
| 261 |
return time.Duration(v.packed) |
| 262 |
} |
| 263 |
|
| 264 |
func (v Label) IsDuration() bool { |
| 265 |
_, ok := v.untyped.(durationKind) |
| 266 |
return ok |
| 267 |
} |