keys.go (6561B)
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 keys 6 7 import ( 8 "fmt" 9 "math" 10 "strconv" 11 12 "golang.org/x/tools/internal/event/label" 13 ) 14 15 // Value is a [label.Key] for untyped values. 16 type Value struct { 17 name string 18 description string 19 } 20 21 // New creates a new Key for untyped values. 22 func New(name, description string) *Value { 23 return &Value{name: name, description: description} 24 } 25 26 func (k *Value) Name() string { return k.name } 27 func (k *Value) Description() string { return k.description } 28 29 func (k *Value) Append(buf []byte, l label.Label) []byte { 30 return fmt.Append(buf, k.From(l)) 31 } 32 33 // Get returns the label for the key of a label.Map. 34 func (k *Value) Get(lm label.Map) any { 35 if t := lm.Find(k); t.Valid() { 36 return k.From(t) 37 } 38 return nil 39 } 40 41 // From returns the value of a Label. 42 func (k *Value) From(t label.Label) any { return t.UnpackValue() } 43 44 // Of creates a new Label with this key and the supplied value. 45 func (k *Value) Of(value any) label.Label { return label.OfValue(k, value) } 46 47 // Tag represents a key for tagging labels that have no value. 48 // These are used when the existence of the label is the entire information it 49 // carries, such as marking events to be of a specific kind, or from a specific 50 // package. 51 type Tag struct { 52 name string 53 description string 54 } 55 56 // NewTag creates a new [label.Key] for tagging labels. 57 func NewTag(name, description string) *Tag { 58 return &Tag{name: name, description: description} 59 } 60 61 func (k *Tag) Name() string { return k.name } 62 func (k *Tag) Description() string { return k.description } 63 64 func (k *Tag) Append(buf []byte, l label.Label) []byte { return buf } 65 66 // New creates a new Label with this key. 67 func (k *Tag) New() label.Label { return label.OfValue(k, nil) } 68 69 // Int is a [label.Key] for signed integers. 70 type Int struct { 71 name string 72 description string 73 } 74 75 // NewInt returns a new [label.Key] for int64 values. 76 func NewInt(name, description string) *Int { 77 return &Int{name: name, description: description} 78 } 79 80 func (k *Int) Name() string { return k.name } 81 func (k *Int) Description() string { return k.description } 82 83 func (k *Int) Append(buf []byte, l label.Label) []byte { 84 return strconv.AppendInt(buf, k.From(l), 10) 85 } 86 87 // Of creates a new Label with this key and the supplied value. 88 func (k *Int) Of(v int) label.Label { return k.Of64(int64(v)) } 89 90 // Of64 creates a new Label with this key and the supplied value. 91 func (k *Int) Of64(v int64) label.Label { return label.Of64(k, uint64(v)) } 92 93 // Get returns the label for the key of a label.Map. 94 func (k *Int) Get(lm label.Map) int64 { 95 if t := lm.Find(k); t.Valid() { 96 return k.From(t) 97 } 98 return 0 99 } 100 101 // From returns the value of a Label. 102 func (k *Int) From(t label.Label) int64 { return int64(t.Unpack64()) } 103 104 // Uint is a [label.Key] for unsigned integers. 105 type Uint struct { 106 name string 107 description string 108 } 109 110 // NewUint creates a new [label.Key] for unsigned values. 111 func NewUint(name, description string) *Uint { 112 return &Uint{name: name, description: description} 113 } 114 115 func (k *Uint) Name() string { return k.name } 116 func (k *Uint) Description() string { return k.description } 117 118 func (k *Uint) Append(buf []byte, l label.Label) []byte { 119 return strconv.AppendUint(buf, k.From(l), 10) 120 } 121 122 // Of creates a new Label with this key and the supplied value. 123 func (k *Uint) Of(v uint64) label.Label { return label.Of64(k, v) } 124 125 // Get returns the label for the key of a label.Map. 126 func (k *Uint) Get(lm label.Map) uint64 { 127 if t := lm.Find(k); t.Valid() { 128 return k.From(t) 129 } 130 return 0 131 } 132 133 // From returns the value of a Label. 134 func (k *Uint) From(t label.Label) uint64 { return t.Unpack64() } 135 136 // Float is a label.Key for floating-point values. 137 type Float struct { 138 name string 139 description string 140 } 141 142 // NewFloat creates a new [label.Key] for floating-point values. 143 func NewFloat(name, description string) *Float { 144 return &Float{name: name, description: description} 145 } 146 147 func (k *Float) Name() string { return k.name } 148 func (k *Float) Description() string { return k.description } 149 150 func (k *Float) Append(buf []byte, l label.Label) []byte { 151 return strconv.AppendFloat(buf, k.From(l), 'E', -1, 64) 152 } 153 154 // Of creates a new Label with this key and the supplied value. 155 func (k *Float) Of(v float64) label.Label { 156 return label.Of64(k, math.Float64bits(v)) 157 } 158 159 // Get returns the label for the key of a label.Map. 160 func (k *Float) Get(lm label.Map) float64 { 161 if t := lm.Find(k); t.Valid() { 162 return k.From(t) 163 } 164 return 0 165 } 166 167 // From returns the value of a Label. 168 func (k *Float) From(t label.Label) float64 { 169 return math.Float64frombits(t.Unpack64()) 170 } 171 172 // String represents a key 173 type String struct { 174 name string 175 description string 176 } 177 178 // NewString creates a new Key for int64 values. 179 func NewString(name, description string) *String { 180 return &String{name: name, description: description} 181 } 182 183 func (k *String) Name() string { return k.name } 184 func (k *String) Description() string { return k.description } 185 186 func (k *String) Append(buf []byte, l label.Label) []byte { 187 return strconv.AppendQuote(buf, k.From(l)) 188 } 189 190 // Of creates a new Label with this key and the supplied value. 191 func (k *String) Of(v string) label.Label { return label.OfString(k, v) } 192 193 // Get returns the label for the key of a label.Map. 194 func (k *String) Get(lm label.Map) string { 195 if t := lm.Find(k); t.Valid() { 196 return k.From(t) 197 } 198 return "" 199 } 200 201 // From returns the value of a Label. 202 func (k *String) From(t label.Label) string { return t.UnpackString() } 203 204 // Error represents a key 205 type Error struct { 206 name string 207 description string 208 } 209 210 // NewError returns a new [label.Key] for error values. 211 func NewError(name, description string) *Error { 212 return &Error{name: name, description: description} 213 } 214 215 func (k *Error) Name() string { return k.name } 216 func (k *Error) Description() string { return k.description } 217 218 func (k *Error) Append(buf []byte, l label.Label) []byte { 219 return append(buf, k.From(l).Error()...) 220 } 221 222 // Of returns a new Label with this key and the supplied value. 223 func (k *Error) Of(v error) label.Label { return label.OfValue(k, v) } 224 225 // Get returns the label for the key of a label.Map. 226 func (k *Error) Get(lm label.Map) error { 227 if t := lm.Find(k); t.Valid() { 228 return k.From(t) 229 } 230 return nil 231 } 232 233 // From returns the value of a Label. 234 func (k *Error) From(t label.Label) error { 235 err, _ := t.UnpackValue().(error) 236 return err 237 }