Debian Codegraph @

event/label.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package event
6
7 import (
8 "fmt"
9 "math"
10 "reflect"
11 "strconv"
12 "time"
13 "unsafe"
14 )
15
16 // Label is a named value.
17 type Label struct {
18 Name string
19
20 packed uint64
21 untyped interface{}
22 }
23
24 // stringptr is used in untyped when the Value is a string
25 type stringptr unsafe.Pointer
26
27 // bytesptr is used in untyped when the Value is a byte slice
28 type bytesptr unsafe.Pointer
29
30 // int64Kind is used in untyped when the Value is a signed integer
31 type int64Kind struct{}
32
33 // uint64Kind is used in untyped when the Value is an unsigned integer
34 type uint64Kind struct{}
35
36 // float64Kind is used in untyped when the Value is a floating point number
37 type float64Kind struct{}
38
39 // boolKind is used in untyped when the Value is a boolean
40 type boolKind struct{}
41
42 // durationKind is used in untyped when the Value is a time.Duration
43 type durationKind struct{}
44
45 // HasValue returns true if the value is set to any type.
46 func (l Label) HasValue() bool { return l.untyped != nil }
47
48 // Equal reports whether two labels are equal.
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 // Value returns a Label for the supplied value.
78 func Value(name string, value interface{}) Label {
79 return Label{Name: name, untyped: value}
80 }
81
82 // Interface returns the value.
83 // This will never panic, things that were not set using SetInterface will be
84 // unpacked and returned anyway.
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 // String returns a new Value for a string.
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 // String returns the value as a string.
110 // This does not panic if v's Kind is not String, instead, it returns a string
111 // representation of the value in all cases.
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 // not a string, convert to one
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 // IsString returns true if the value was built with StringOf.
136 func (v Label) IsString() bool {
137 _, ok := v.untyped.(stringptr)
138 return ok
139 }
140
141 // Bytes returns a new Value for a string.
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 // Bytes returns the value as a bytes array.
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 // IsBytes returns true if the value was built with BytesOf.
162 func (v Label) IsBytes() bool {
163 _, ok := v.untyped.(bytesptr)
164 return ok
165 }
166
167 // Int64 returns a new Value for a signed integer.
168 func Int64(name string, u int64) Label {
169 return Label{Name: name, packed: uint64(u), untyped: int64Kind{}}
170 }
171
172 // Int64 returns the int64 from a value that was set with SetInt64.
173 // It will panic for any value for which IsInt64 is not true.
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 // IsInt64 returns true if the value was built with SetInt64.
182 func (v Label) IsInt64() bool {
183 _, ok := v.untyped.(int64Kind)
184 return ok
185 }
186
187 // Uint64 returns a new Value for an unsigned integer.
188 func Uint64(name string, u uint64) Label {
189 return Label{Name: name, packed: u, untyped: uint64Kind{}}
190 }
191
192 // Uint64 returns the uint64 from a value that was set with SetUint64.
193 // It will panic for any value for which IsUint64 is not true.
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 // IsUint64 returns true if the value was built with SetUint64.
202 func (v Label) IsUint64() bool {
203 _, ok := v.untyped.(uint64Kind)
204 return ok
205 }
206
207 // Float64 returns a new Value for a floating point number.
208 func Float64(name string, f float64) Label {
209 return Label{Name: name, packed: math.Float64bits(f), untyped: float64Kind{}}
210 }
211
212 // Float64 returns the float64 from a value that was set with SetFloat64.
213 // It will panic for any value for which IsFloat64 is not true.
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 // IsFloat64 returns true if the value was built with SetFloat64.
222 func (v Label) IsFloat64() bool {
223 _, ok := v.untyped.(float64Kind)
224 return ok
225 }
226
227 // Bool returns a new Value for a bool.
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 // Bool returns the bool from a value that was set with SetBool.
236 // It will panic for any value for which IsBool is not true.
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 // IsBool returns true if the value was built with SetBool.
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 }