Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / mvdan.cc / gofumpt@v0.0.0-20200802201014-ab5a8192947d / gofumports / internal / telemetry / event / key.go
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         "io"
10         "math"
11         "strconv"
12 )
13
14 var (
15         // Msg is a key used to add message strings to tag lists.
16         Msg = NewStringKey("message", "a readable message")
17         // Name is used for things like traces that have a name.
18         Name = NewStringKey("name", "an entity name")
19         // Err is a key used to add error values to tag lists.
20         Err = NewErrorKey("error", "an error that occurred")
21 )
22
23 // Key is used as the identity of a Tag.
24 // Keys are intended to be compared by pointer only, the name should be unique
25 // for communicating with external systems, but it is not required or enforced.
26 type Key interface {
27         // Name returns the key name.
28         Name() string
29         // Description returns a string that can be used to describe the value.
30         Description() string
31
32         // Format is used in formatting to append the value of the tag to the
33         // supplied buffer.
34         // The formatter may use the supplied buf as a scratch area to avoid
35         // allocations.
36         Format(w io.Writer, buf []byte, tag Tag)
37 }
38
39 // ValueKey represents a key for untyped values.
40 type ValueKey struct {
41         name        string
42         description string
43 }
44
45 // NewKey creates a new Key for untyped values.
46 func NewKey(name, description string) *ValueKey {
47         return &ValueKey{name: name, description: description}
48 }
49
50 func (k *ValueKey) Name() string        { return k.name }
51 func (k *ValueKey) Description() string { return k.description }
52
53 func (k *ValueKey) Format(w io.Writer, buf []byte, tag Tag) {
54         fmt.Fprint(w, k.From(tag))
55 }
56
57 // Get can be used to get a tag for the key from a TagMap.
58 func (k *ValueKey) Get(tags TagMap) interface{} {
59         if t := tags.Find(k); t.Valid() {
60                 return k.From(t)
61         }
62         return nil
63 }
64
65 // From can be used to get a value from a Tag.
66 func (k *ValueKey) From(t Tag) interface{} { return t.UnpackValue() }
67
68 // Of creates a new Tag with this key and the supplied value.
69 func (k *ValueKey) Of(value interface{}) Tag { return TagOfValue(k, value) }
70
71 // IntKey represents a key
72 type IntKey struct {
73         name        string
74         description string
75 }
76
77 // NewIntKey creates a new Key for int values.
78 func NewIntKey(name, description string) *IntKey {
79         return &IntKey{name: name, description: description}
80 }
81
82 func (k *IntKey) Name() string        { return k.name }
83 func (k *IntKey) Description() string { return k.description }
84
85 func (k *IntKey) Format(w io.Writer, buf []byte, tag Tag) {
86         w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
87 }
88
89 // Of creates a new Tag with this key and the supplied value.
90 func (k *IntKey) Of(v int) Tag { return TagOf64(k, uint64(v)) }
91
92 // Get can be used to get a tag for the key from a TagMap.
93 func (k *IntKey) Get(tags TagMap) int {
94         if t := tags.Find(k); t.Valid() {
95                 return k.From(t)
96         }
97         return 0
98 }
99
100 // From can be used to get a value from a Tag.
101 func (k *IntKey) From(t Tag) int { return int(t.Unpack64()) }
102
103 // Int8Key represents a key
104 type Int8Key struct {
105         name        string
106         description string
107 }
108
109 // NewInt8Key creates a new Key for int8 values.
110 func NewInt8Key(name, description string) *Int8Key {
111         return &Int8Key{name: name, description: description}
112 }
113
114 func (k *Int8Key) Name() string        { return k.name }
115 func (k *Int8Key) Description() string { return k.description }
116
117 func (k *Int8Key) Format(w io.Writer, buf []byte, tag Tag) {
118         w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
119 }
120
121 // Of creates a new Tag with this key and the supplied value.
122 func (k *Int8Key) Of(v int8) Tag { return TagOf64(k, uint64(v)) }
123
124 // Get can be used to get a tag for the key from a TagMap.
125 func (k *Int8Key) Get(tags TagMap) int8 {
126         if t := tags.Find(k); t.Valid() {
127                 return k.From(t)
128         }
129         return 0
130 }
131
132 // From can be used to get a value from a Tag.
133 func (k *Int8Key) From(t Tag) int8 { return int8(t.Unpack64()) }
134
135 // Int16Key represents a key
136 type Int16Key struct {
137         name        string
138         description string
139 }
140
141 // NewInt16Key creates a new Key for int16 values.
142 func NewInt16Key(name, description string) *Int16Key {
143         return &Int16Key{name: name, description: description}
144 }
145
146 func (k *Int16Key) Name() string        { return k.name }
147 func (k *Int16Key) Description() string { return k.description }
148
149 func (k *Int16Key) Format(w io.Writer, buf []byte, tag Tag) {
150         w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
151 }
152
153 // Of creates a new Tag with this key and the supplied value.
154 func (k *Int16Key) Of(v int16) Tag { return TagOf64(k, uint64(v)) }
155
156 // Get can be used to get a tag for the key from a TagMap.
157 func (k *Int16Key) Get(tags TagMap) int16 {
158         if t := tags.Find(k); t.Valid() {
159                 return k.From(t)
160         }
161         return 0
162 }
163
164 // From can be used to get a value from a Tag.
165 func (k *Int16Key) From(t Tag) int16 { return int16(t.Unpack64()) }
166
167 // Int32Key represents a key
168 type Int32Key struct {
169         name        string
170         description string
171 }
172
173 // NewInt32Key creates a new Key for int32 values.
174 func NewInt32Key(name, description string) *Int32Key {
175         return &Int32Key{name: name, description: description}
176 }
177
178 func (k *Int32Key) Name() string        { return k.name }
179 func (k *Int32Key) Description() string { return k.description }
180
181 func (k *Int32Key) Format(w io.Writer, buf []byte, tag Tag) {
182         w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
183 }
184
185 // Of creates a new Tag with this key and the supplied value.
186 func (k *Int32Key) Of(v int32) Tag { return TagOf64(k, uint64(v)) }
187
188 // Get can be used to get a tag for the key from a TagMap.
189 func (k *Int32Key) Get(tags TagMap) int32 {
190         if t := tags.Find(k); t.Valid() {
191                 return k.From(t)
192         }
193         return 0
194 }
195
196 // From can be used to get a value from a Tag.
197 func (k *Int32Key) From(t Tag) int32 { return int32(t.Unpack64()) }
198
199 // Int64Key represents a key
200 type Int64Key struct {
201         name        string
202         description string
203 }
204
205 // NewInt64Key creates a new Key for int64 values.
206 func NewInt64Key(name, description string) *Int64Key {
207         return &Int64Key{name: name, description: description}
208 }
209
210 func (k *Int64Key) Name() string        { return k.name }
211 func (k *Int64Key) Description() string { return k.description }
212
213 func (k *Int64Key) Format(w io.Writer, buf []byte, tag Tag) {
214         w.Write(strconv.AppendInt(buf, k.From(tag), 10))
215 }
216
217 // Of creates a new Tag with this key and the supplied value.
218 func (k *Int64Key) Of(v int64) Tag { return TagOf64(k, uint64(v)) }
219
220 // Get can be used to get a tag for the key from a TagMap.
221 func (k *Int64Key) Get(tags TagMap) int64 {
222         if t := tags.Find(k); t.Valid() {
223                 return k.From(t)
224         }
225         return 0
226 }
227
228 // From can be used to get a value from a Tag.
229 func (k *Int64Key) From(t Tag) int64 { return int64(t.Unpack64()) }
230
231 // UIntKey represents a key
232 type UIntKey struct {
233         name        string
234         description string
235 }
236
237 // NewUIntKey creates a new Key for uint values.
238 func NewUIntKey(name, description string) *UIntKey {
239         return &UIntKey{name: name, description: description}
240 }
241
242 func (k *UIntKey) Name() string        { return k.name }
243 func (k *UIntKey) Description() string { return k.description }
244
245 func (k *UIntKey) Format(w io.Writer, buf []byte, tag Tag) {
246         w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
247 }
248
249 // Of creates a new Tag with this key and the supplied value.
250 func (k *UIntKey) Of(v uint) Tag { return TagOf64(k, uint64(v)) }
251
252 // Get can be used to get a tag for the key from a TagMap.
253 func (k *UIntKey) Get(tags TagMap) uint {
254         if t := tags.Find(k); t.Valid() {
255                 return k.From(t)
256         }
257         return 0
258 }
259
260 // From can be used to get a value from a Tag.
261 func (k *UIntKey) From(t Tag) uint { return uint(t.Unpack64()) }
262
263 // UInt8Key represents a key
264 type UInt8Key struct {
265         name        string
266         description string
267 }
268
269 // NewUInt8Key creates a new Key for uint8 values.
270 func NewUInt8Key(name, description string) *UInt8Key {
271         return &UInt8Key{name: name, description: description}
272 }
273
274 func (k *UInt8Key) Name() string        { return k.name }
275 func (k *UInt8Key) Description() string { return k.description }
276
277 func (k *UInt8Key) Format(w io.Writer, buf []byte, tag Tag) {
278         w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
279 }
280
281 // Of creates a new Tag with this key and the supplied value.
282 func (k *UInt8Key) Of(v uint8) Tag { return TagOf64(k, uint64(v)) }
283
284 // Get can be used to get a tag for the key from a TagMap.
285 func (k *UInt8Key) Get(tags TagMap) uint8 {
286         if t := tags.Find(k); t.Valid() {
287                 return k.From(t)
288         }
289         return 0
290 }
291
292 // From can be used to get a value from a Tag.
293 func (k *UInt8Key) From(t Tag) uint8 { return uint8(t.Unpack64()) }
294
295 // UInt16Key represents a key
296 type UInt16Key struct {
297         name        string
298         description string
299 }
300
301 // NewUInt16Key creates a new Key for uint16 values.
302 func NewUInt16Key(name, description string) *UInt16Key {
303         return &UInt16Key{name: name, description: description}
304 }
305
306 func (k *UInt16Key) Name() string        { return k.name }
307 func (k *UInt16Key) Description() string { return k.description }
308
309 func (k *UInt16Key) Format(w io.Writer, buf []byte, tag Tag) {
310         w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
311 }
312
313 // Of creates a new Tag with this key and the supplied value.
314 func (k *UInt16Key) Of(v uint16) Tag { return TagOf64(k, uint64(v)) }
315
316 // Get can be used to get a tag for the key from a TagMap.
317 func (k *UInt16Key) Get(tags TagMap) uint16 {
318         if t := tags.Find(k); t.Valid() {
319                 return k.From(t)
320         }
321         return 0
322 }
323
324 // From can be used to get a value from a Tag.
325 func (k *UInt16Key) From(t Tag) uint16 { return uint16(t.Unpack64()) }
326
327 // UInt32Key represents a key
328 type UInt32Key struct {
329         name        string
330         description string
331 }
332
333 // NewUInt32Key creates a new Key for uint32 values.
334 func NewUInt32Key(name, description string) *UInt32Key {
335         return &UInt32Key{name: name, description: description}
336 }
337
338 func (k *UInt32Key) Name() string        { return k.name }
339 func (k *UInt32Key) Description() string { return k.description }
340
341 func (k *UInt32Key) Format(w io.Writer, buf []byte, tag Tag) {
342         w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
343 }
344
345 // Of creates a new Tag with this key and the supplied value.
346 func (k *UInt32Key) Of(v uint32) Tag { return TagOf64(k, uint64(v)) }
347
348 // Get can be used to get a tag for the key from a TagMap.
349 func (k *UInt32Key) Get(tags TagMap) uint32 {
350         if t := tags.Find(k); t.Valid() {
351                 return k.From(t)
352         }
353         return 0
354 }
355
356 // From can be used to get a value from a Tag.
357 func (k *UInt32Key) From(t Tag) uint32 { return uint32(t.Unpack64()) }
358
359 // UInt64Key represents a key
360 type UInt64Key struct {
361         name        string
362         description string
363 }
364
365 // NewUInt64Key creates a new Key for uint64 values.
366 func NewUInt64Key(name, description string) *UInt64Key {
367         return &UInt64Key{name: name, description: description}
368 }
369
370 func (k *UInt64Key) Name() string        { return k.name }
371 func (k *UInt64Key) Description() string { return k.description }
372
373 func (k *UInt64Key) Format(w io.Writer, buf []byte, tag Tag) {
374         w.Write(strconv.AppendUint(buf, k.From(tag), 10))
375 }
376
377 // Of creates a new Tag with this key and the supplied value.
378 func (k *UInt64Key) Of(v uint64) Tag { return TagOf64(k, v) }
379
380 // Get can be used to get a tag for the key from a TagMap.
381 func (k *UInt64Key) Get(tags TagMap) uint64 {
382         if t := tags.Find(k); t.Valid() {
383                 return k.From(t)
384         }
385         return 0
386 }
387
388 // From can be used to get a value from a Tag.
389 func (k *UInt64Key) From(t Tag) uint64 { return t.Unpack64() }
390
391 // Float32Key represents a key
392 type Float32Key struct {
393         name        string
394         description string
395 }
396
397 // NewFloat32Key creates a new Key for float32 values.
398 func NewFloat32Key(name, description string) *Float32Key {
399         return &Float32Key{name: name, description: description}
400 }
401
402 func (k *Float32Key) Name() string        { return k.name }
403 func (k *Float32Key) Description() string { return k.description }
404
405 func (k *Float32Key) Format(w io.Writer, buf []byte, tag Tag) {
406         w.Write(strconv.AppendFloat(buf, float64(k.From(tag)), 'E', -1, 32))
407 }
408
409 // Of creates a new Tag with this key and the supplied value.
410 func (k *Float32Key) Of(v float32) Tag {
411         return TagOf64(k, uint64(math.Float32bits(v)))
412 }
413
414 // Get can be used to get a tag for the key from a TagMap.
415 func (k *Float32Key) Get(tags TagMap) float32 {
416         if t := tags.Find(k); t.Valid() {
417                 return k.From(t)
418         }
419         return 0
420 }
421
422 // From can be used to get a value from a Tag.
423 func (k *Float32Key) From(t Tag) float32 {
424         return math.Float32frombits(uint32(t.Unpack64()))
425 }
426
427 // Float64Key represents a key
428 type Float64Key struct {
429         name        string
430         description string
431 }
432
433 // NewFloat64Key creates a new Key for int64 values.
434 func NewFloat64Key(name, description string) *Float64Key {
435         return &Float64Key{name: name, description: description}
436 }
437
438 func (k *Float64Key) Name() string        { return k.name }
439 func (k *Float64Key) Description() string { return k.description }
440
441 func (k *Float64Key) Format(w io.Writer, buf []byte, tag Tag) {
442         w.Write(strconv.AppendFloat(buf, k.From(tag), 'E', -1, 64))
443 }
444
445 // Of creates a new Tag with this key and the supplied value.
446 func (k *Float64Key) Of(v float64) Tag {
447         return TagOf64(k, math.Float64bits(v))
448 }
449
450 // Get can be used to get a tag for the key from a TagMap.
451 func (k *Float64Key) Get(tags TagMap) float64 {
452         if t := tags.Find(k); t.Valid() {
453                 return k.From(t)
454         }
455         return 0
456 }
457
458 // From can be used to get a value from a Tag.
459 func (k *Float64Key) From(t Tag) float64 {
460         return math.Float64frombits(t.Unpack64())
461 }
462
463 // StringKey represents a key
464 type StringKey struct {
465         name        string
466         description string
467 }
468
469 // NewStringKey creates a new Key for int64 values.
470 func NewStringKey(name, description string) *StringKey {
471         return &StringKey{name: name, description: description}
472 }
473
474 func (k *StringKey) Name() string        { return k.name }
475 func (k *StringKey) Description() string { return k.description }
476
477 func (k *StringKey) Format(w io.Writer, buf []byte, tag Tag) {
478         w.Write(strconv.AppendQuote(buf, k.From(tag)))
479 }
480
481 // Of creates a new Tag with this key and the supplied value.
482 func (k *StringKey) Of(v string) Tag { return TagOfString(k, v) }
483
484 // Get can be used to get a tag for the key from a TagMap.
485 func (k *StringKey) Get(tags TagMap) string {
486         if t := tags.Find(k); t.Valid() {
487                 return k.From(t)
488         }
489         return ""
490 }
491
492 // From can be used to get a value from a Tag.
493 func (k *StringKey) From(t Tag) string { return t.UnpackString() }
494
495 // BooleanKey represents a key
496 type BooleanKey struct {
497         name        string
498         description string
499 }
500
501 // NewBooleanKey creates a new Key for bool values.
502 func NewBooleanKey(name, description string) *BooleanKey {
503         return &BooleanKey{name: name, description: description}
504 }
505
506 func (k *BooleanKey) Name() string        { return k.name }
507 func (k *BooleanKey) Description() string { return k.description }
508
509 func (k *BooleanKey) Format(w io.Writer, buf []byte, tag Tag) {
510         w.Write(strconv.AppendBool(buf, k.From(tag)))
511 }
512
513 // Of creates a new Tag with this key and the supplied value.
514 func (k *BooleanKey) Of(v bool) Tag {
515         if v {
516                 return TagOf64(k, 1)
517         }
518         return TagOf64(k, 0)
519 }
520
521 // Get can be used to get a tag for the key from a TagMap.
522 func (k *BooleanKey) Get(tags TagMap) bool {
523         if t := tags.Find(k); t.Valid() {
524                 return k.From(t)
525         }
526         return false
527 }
528
529 // From can be used to get a value from a Tag.
530 func (k *BooleanKey) From(t Tag) bool { return t.Unpack64() > 0 }
531
532 // ErrorKey represents a key
533 type ErrorKey struct {
534         name        string
535         description string
536 }
537
538 // NewErrorKey creates a new Key for int64 values.
539 func NewErrorKey(name, description string) *ErrorKey {
540         return &ErrorKey{name: name, description: description}
541 }
542
543 func (k *ErrorKey) Name() string        { return k.name }
544 func (k *ErrorKey) Description() string { return k.description }
545
546 func (k *ErrorKey) Format(w io.Writer, buf []byte, tag Tag) {
547         io.WriteString(w, k.From(tag).Error())
548 }
549
550 // Of creates a new Tag with this key and the supplied value.
551 func (k *ErrorKey) Of(v error) Tag { return TagOfValue(k, v) }
552
553 // Get can be used to get a tag for the key from a TagMap.
554 func (k *ErrorKey) Get(tags TagMap) error {
555         if t := tags.Find(k); t.Valid() {
556                 return k.From(t)
557         }
558         return nil
559 }
560
561 // From can be used to get a value from a Tag.
562 func (k *ErrorKey) From(t Tag) error {
563         err, _ := t.UnpackValue().(error)
564         return err
565 }