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.
13 "golang.org/x/tools/internal/event/label"
16 // Value represents a key for untyped values.
22 // New creates a new Key for untyped values.
23 func New(name, description string) *Value {
24 return &Value{name: name, description: description}
27 func (k *Value) Name() string { return k.name }
28 func (k *Value) Description() string { return k.description }
30 func (k *Value) Format(w io.Writer, buf []byte, l label.Label) {
31 fmt.Fprint(w, k.From(l))
34 // Get can be used to get a label for the key from a label.Map.
35 func (k *Value) Get(lm label.Map) interface{} {
36 if t := lm.Find(k); t.Valid() {
42 // From can be used to get a value from a Label.
43 func (k *Value) From(t label.Label) interface{} { return t.UnpackValue() }
45 // Of creates a new Label with this key and the supplied value.
46 func (k *Value) Of(value interface{}) label.Label { return label.OfValue(k, value) }
48 // Tag represents a key for tagging labels that have no value.
49 // These are used when the existence of the label is the entire information it
50 // carries, such as marking events to be of a specific kind, or from a specific
57 // NewTag creates a new Key for tagging labels.
58 func NewTag(name, description string) *Tag {
59 return &Tag{name: name, description: description}
62 func (k *Tag) Name() string { return k.name }
63 func (k *Tag) Description() string { return k.description }
65 func (k *Tag) Format(w io.Writer, buf []byte, l label.Label) {}
67 // New creates a new Label with this key.
68 func (k *Tag) New() label.Label { return label.OfValue(k, nil) }
70 // Int represents a key
76 // NewInt creates a new Key for int values.
77 func NewInt(name, description string) *Int {
78 return &Int{name: name, description: description}
81 func (k *Int) Name() string { return k.name }
82 func (k *Int) Description() string { return k.description }
84 func (k *Int) Format(w io.Writer, buf []byte, l label.Label) {
85 w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
88 // Of creates a new Label with this key and the supplied value.
89 func (k *Int) Of(v int) label.Label { return label.Of64(k, uint64(v)) }
91 // Get can be used to get a label for the key from a label.Map.
92 func (k *Int) Get(lm label.Map) int {
93 if t := lm.Find(k); t.Valid() {
99 // From can be used to get a value from a Label.
100 func (k *Int) From(t label.Label) int { return int(t.Unpack64()) }
102 // Int8 represents a key
108 // NewInt8 creates a new Key for int8 values.
109 func NewInt8(name, description string) *Int8 {
110 return &Int8{name: name, description: description}
113 func (k *Int8) Name() string { return k.name }
114 func (k *Int8) Description() string { return k.description }
116 func (k *Int8) Format(w io.Writer, buf []byte, l label.Label) {
117 w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
120 // Of creates a new Label with this key and the supplied value.
121 func (k *Int8) Of(v int8) label.Label { return label.Of64(k, uint64(v)) }
123 // Get can be used to get a label for the key from a label.Map.
124 func (k *Int8) Get(lm label.Map) int8 {
125 if t := lm.Find(k); t.Valid() {
131 // From can be used to get a value from a Label.
132 func (k *Int8) From(t label.Label) int8 { return int8(t.Unpack64()) }
134 // Int16 represents a key
140 // NewInt16 creates a new Key for int16 values.
141 func NewInt16(name, description string) *Int16 {
142 return &Int16{name: name, description: description}
145 func (k *Int16) Name() string { return k.name }
146 func (k *Int16) Description() string { return k.description }
148 func (k *Int16) Format(w io.Writer, buf []byte, l label.Label) {
149 w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
152 // Of creates a new Label with this key and the supplied value.
153 func (k *Int16) Of(v int16) label.Label { return label.Of64(k, uint64(v)) }
155 // Get can be used to get a label for the key from a label.Map.
156 func (k *Int16) Get(lm label.Map) int16 {
157 if t := lm.Find(k); t.Valid() {
163 // From can be used to get a value from a Label.
164 func (k *Int16) From(t label.Label) int16 { return int16(t.Unpack64()) }
166 // Int32 represents a key
172 // NewInt32 creates a new Key for int32 values.
173 func NewInt32(name, description string) *Int32 {
174 return &Int32{name: name, description: description}
177 func (k *Int32) Name() string { return k.name }
178 func (k *Int32) Description() string { return k.description }
180 func (k *Int32) Format(w io.Writer, buf []byte, l label.Label) {
181 w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
184 // Of creates a new Label with this key and the supplied value.
185 func (k *Int32) Of(v int32) label.Label { return label.Of64(k, uint64(v)) }
187 // Get can be used to get a label for the key from a label.Map.
188 func (k *Int32) Get(lm label.Map) int32 {
189 if t := lm.Find(k); t.Valid() {
195 // From can be used to get a value from a Label.
196 func (k *Int32) From(t label.Label) int32 { return int32(t.Unpack64()) }
198 // Int64 represents a key
204 // NewInt64 creates a new Key for int64 values.
205 func NewInt64(name, description string) *Int64 {
206 return &Int64{name: name, description: description}
209 func (k *Int64) Name() string { return k.name }
210 func (k *Int64) Description() string { return k.description }
212 func (k *Int64) Format(w io.Writer, buf []byte, l label.Label) {
213 w.Write(strconv.AppendInt(buf, k.From(l), 10))
216 // Of creates a new Label with this key and the supplied value.
217 func (k *Int64) Of(v int64) label.Label { return label.Of64(k, uint64(v)) }
219 // Get can be used to get a label for the key from a label.Map.
220 func (k *Int64) Get(lm label.Map) int64 {
221 if t := lm.Find(k); t.Valid() {
227 // From can be used to get a value from a Label.
228 func (k *Int64) From(t label.Label) int64 { return int64(t.Unpack64()) }
230 // UInt represents a key
236 // NewUInt creates a new Key for uint values.
237 func NewUInt(name, description string) *UInt {
238 return &UInt{name: name, description: description}
241 func (k *UInt) Name() string { return k.name }
242 func (k *UInt) Description() string { return k.description }
244 func (k *UInt) Format(w io.Writer, buf []byte, l label.Label) {
245 w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
248 // Of creates a new Label with this key and the supplied value.
249 func (k *UInt) Of(v uint) label.Label { return label.Of64(k, uint64(v)) }
251 // Get can be used to get a label for the key from a label.Map.
252 func (k *UInt) Get(lm label.Map) uint {
253 if t := lm.Find(k); t.Valid() {
259 // From can be used to get a value from a Label.
260 func (k *UInt) From(t label.Label) uint { return uint(t.Unpack64()) }
262 // UInt8 represents a key
268 // NewUInt8 creates a new Key for uint8 values.
269 func NewUInt8(name, description string) *UInt8 {
270 return &UInt8{name: name, description: description}
273 func (k *UInt8) Name() string { return k.name }
274 func (k *UInt8) Description() string { return k.description }
276 func (k *UInt8) Format(w io.Writer, buf []byte, l label.Label) {
277 w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
280 // Of creates a new Label with this key and the supplied value.
281 func (k *UInt8) Of(v uint8) label.Label { return label.Of64(k, uint64(v)) }
283 // Get can be used to get a label for the key from a label.Map.
284 func (k *UInt8) Get(lm label.Map) uint8 {
285 if t := lm.Find(k); t.Valid() {
291 // From can be used to get a value from a Label.
292 func (k *UInt8) From(t label.Label) uint8 { return uint8(t.Unpack64()) }
294 // UInt16 represents a key
300 // NewUInt16 creates a new Key for uint16 values.
301 func NewUInt16(name, description string) *UInt16 {
302 return &UInt16{name: name, description: description}
305 func (k *UInt16) Name() string { return k.name }
306 func (k *UInt16) Description() string { return k.description }
308 func (k *UInt16) Format(w io.Writer, buf []byte, l label.Label) {
309 w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
312 // Of creates a new Label with this key and the supplied value.
313 func (k *UInt16) Of(v uint16) label.Label { return label.Of64(k, uint64(v)) }
315 // Get can be used to get a label for the key from a label.Map.
316 func (k *UInt16) Get(lm label.Map) uint16 {
317 if t := lm.Find(k); t.Valid() {
323 // From can be used to get a value from a Label.
324 func (k *UInt16) From(t label.Label) uint16 { return uint16(t.Unpack64()) }
326 // UInt32 represents a key
332 // NewUInt32 creates a new Key for uint32 values.
333 func NewUInt32(name, description string) *UInt32 {
334 return &UInt32{name: name, description: description}
337 func (k *UInt32) Name() string { return k.name }
338 func (k *UInt32) Description() string { return k.description }
340 func (k *UInt32) Format(w io.Writer, buf []byte, l label.Label) {
341 w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
344 // Of creates a new Label with this key and the supplied value.
345 func (k *UInt32) Of(v uint32) label.Label { return label.Of64(k, uint64(v)) }
347 // Get can be used to get a label for the key from a label.Map.
348 func (k *UInt32) Get(lm label.Map) uint32 {
349 if t := lm.Find(k); t.Valid() {
355 // From can be used to get a value from a Label.
356 func (k *UInt32) From(t label.Label) uint32 { return uint32(t.Unpack64()) }
358 // UInt64 represents a key
364 // NewUInt64 creates a new Key for uint64 values.
365 func NewUInt64(name, description string) *UInt64 {
366 return &UInt64{name: name, description: description}
369 func (k *UInt64) Name() string { return k.name }
370 func (k *UInt64) Description() string { return k.description }
372 func (k *UInt64) Format(w io.Writer, buf []byte, l label.Label) {
373 w.Write(strconv.AppendUint(buf, k.From(l), 10))
376 // Of creates a new Label with this key and the supplied value.
377 func (k *UInt64) Of(v uint64) label.Label { return label.Of64(k, v) }
379 // Get can be used to get a label for the key from a label.Map.
380 func (k *UInt64) Get(lm label.Map) uint64 {
381 if t := lm.Find(k); t.Valid() {
387 // From can be used to get a value from a Label.
388 func (k *UInt64) From(t label.Label) uint64 { return t.Unpack64() }
390 // Float32 represents a key
391 type Float32 struct {
396 // NewFloat32 creates a new Key for float32 values.
397 func NewFloat32(name, description string) *Float32 {
398 return &Float32{name: name, description: description}
401 func (k *Float32) Name() string { return k.name }
402 func (k *Float32) Description() string { return k.description }
404 func (k *Float32) Format(w io.Writer, buf []byte, l label.Label) {
405 w.Write(strconv.AppendFloat(buf, float64(k.From(l)), 'E', -1, 32))
408 // Of creates a new Label with this key and the supplied value.
409 func (k *Float32) Of(v float32) label.Label {
410 return label.Of64(k, uint64(math.Float32bits(v)))
413 // Get can be used to get a label for the key from a label.Map.
414 func (k *Float32) Get(lm label.Map) float32 {
415 if t := lm.Find(k); t.Valid() {
421 // From can be used to get a value from a Label.
422 func (k *Float32) From(t label.Label) float32 {
423 return math.Float32frombits(uint32(t.Unpack64()))
426 // Float64 represents a key
427 type Float64 struct {
432 // NewFloat64 creates a new Key for int64 values.
433 func NewFloat64(name, description string) *Float64 {
434 return &Float64{name: name, description: description}
437 func (k *Float64) Name() string { return k.name }
438 func (k *Float64) Description() string { return k.description }
440 func (k *Float64) Format(w io.Writer, buf []byte, l label.Label) {
441 w.Write(strconv.AppendFloat(buf, k.From(l), 'E', -1, 64))
444 // Of creates a new Label with this key and the supplied value.
445 func (k *Float64) Of(v float64) label.Label {
446 return label.Of64(k, math.Float64bits(v))
449 // Get can be used to get a label for the key from a label.Map.
450 func (k *Float64) Get(lm label.Map) float64 {
451 if t := lm.Find(k); t.Valid() {
457 // From can be used to get a value from a Label.
458 func (k *Float64) From(t label.Label) float64 {
459 return math.Float64frombits(t.Unpack64())
462 // String represents a key
468 // NewString creates a new Key for int64 values.
469 func NewString(name, description string) *String {
470 return &String{name: name, description: description}
473 func (k *String) Name() string { return k.name }
474 func (k *String) Description() string { return k.description }
476 func (k *String) Format(w io.Writer, buf []byte, l label.Label) {
477 w.Write(strconv.AppendQuote(buf, k.From(l)))
480 // Of creates a new Label with this key and the supplied value.
481 func (k *String) Of(v string) label.Label { return label.OfString(k, v) }
483 // Get can be used to get a label for the key from a label.Map.
484 func (k *String) Get(lm label.Map) string {
485 if t := lm.Find(k); t.Valid() {
491 // From can be used to get a value from a Label.
492 func (k *String) From(t label.Label) string { return t.UnpackString() }
494 // Boolean represents a key
495 type Boolean struct {
500 // NewBoolean creates a new Key for bool values.
501 func NewBoolean(name, description string) *Boolean {
502 return &Boolean{name: name, description: description}
505 func (k *Boolean) Name() string { return k.name }
506 func (k *Boolean) Description() string { return k.description }
508 func (k *Boolean) Format(w io.Writer, buf []byte, l label.Label) {
509 w.Write(strconv.AppendBool(buf, k.From(l)))
512 // Of creates a new Label with this key and the supplied value.
513 func (k *Boolean) Of(v bool) label.Label {
515 return label.Of64(k, 1)
517 return label.Of64(k, 0)
520 // Get can be used to get a label for the key from a label.Map.
521 func (k *Boolean) Get(lm label.Map) bool {
522 if t := lm.Find(k); t.Valid() {
528 // From can be used to get a value from a Label.
529 func (k *Boolean) From(t label.Label) bool { return t.Unpack64() > 0 }
531 // Error represents a key
537 // NewError creates a new Key for int64 values.
538 func NewError(name, description string) *Error {
539 return &Error{name: name, description: description}
542 func (k *Error) Name() string { return k.name }
543 func (k *Error) Description() string { return k.description }
545 func (k *Error) Format(w io.Writer, buf []byte, l label.Label) {
546 io.WriteString(w, k.From(l).Error())
549 // Of creates a new Label with this key and the supplied value.
550 func (k *Error) Of(v error) label.Label { return label.OfValue(k, v) }
552 // Get can be used to get a label for the key from a label.Map.
553 func (k *Error) Get(lm label.Map) error {
554 if t := lm.Find(k); t.Valid() {
560 // From can be used to get a value from a Label.
561 func (k *Error) From(t label.Label) error {
562 err, _ := t.UnpackValue().(error)