keys.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. package keys
  5. import (
  6. "fmt"
  7. "io"
  8. "math"
  9. "strconv"
  10. "golang.org/x/tools/internal/event/label"
  11. )
  12. // Value represents a key for untyped values.
  13. type Value struct {
  14. name string
  15. description string
  16. }
  17. // New creates a new Key for untyped values.
  18. func New(name, description string) *Value {
  19. return &Value{name: name, description: description}
  20. }
  21. func (k *Value) Name() string { return k.name }
  22. func (k *Value) Description() string { return k.description }
  23. func (k *Value) Format(w io.Writer, buf []byte, l label.Label) {
  24. fmt.Fprint(w, k.From(l))
  25. }
  26. // Get can be used to get a label for the key from a label.Map.
  27. func (k *Value) Get(lm label.Map) interface{} {
  28. if t := lm.Find(k); t.Valid() {
  29. return k.From(t)
  30. }
  31. return nil
  32. }
  33. // From can be used to get a value from a Label.
  34. func (k *Value) From(t label.Label) interface{} { return t.UnpackValue() }
  35. // Of creates a new Label with this key and the supplied value.
  36. func (k *Value) Of(value interface{}) label.Label { return label.OfValue(k, value) }
  37. // Tag represents a key for tagging labels that have no value.
  38. // These are used when the existence of the label is the entire information it
  39. // carries, such as marking events to be of a specific kind, or from a specific
  40. // package.
  41. type Tag struct {
  42. name string
  43. description string
  44. }
  45. // NewTag creates a new Key for tagging labels.
  46. func NewTag(name, description string) *Tag {
  47. return &Tag{name: name, description: description}
  48. }
  49. func (k *Tag) Name() string { return k.name }
  50. func (k *Tag) Description() string { return k.description }
  51. func (k *Tag) Format(w io.Writer, buf []byte, l label.Label) {}
  52. // New creates a new Label with this key.
  53. func (k *Tag) New() label.Label { return label.OfValue(k, nil) }
  54. // Int represents a key
  55. type Int struct {
  56. name string
  57. description string
  58. }
  59. // NewInt creates a new Key for int values.
  60. func NewInt(name, description string) *Int {
  61. return &Int{name: name, description: description}
  62. }
  63. func (k *Int) Name() string { return k.name }
  64. func (k *Int) Description() string { return k.description }
  65. func (k *Int) Format(w io.Writer, buf []byte, l label.Label) {
  66. w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
  67. }
  68. // Of creates a new Label with this key and the supplied value.
  69. func (k *Int) Of(v int) label.Label { return label.Of64(k, uint64(v)) }
  70. // Get can be used to get a label for the key from a label.Map.
  71. func (k *Int) Get(lm label.Map) int {
  72. if t := lm.Find(k); t.Valid() {
  73. return k.From(t)
  74. }
  75. return 0
  76. }
  77. // From can be used to get a value from a Label.
  78. func (k *Int) From(t label.Label) int { return int(t.Unpack64()) }
  79. // Int8 represents a key
  80. type Int8 struct {
  81. name string
  82. description string
  83. }
  84. // NewInt8 creates a new Key for int8 values.
  85. func NewInt8(name, description string) *Int8 {
  86. return &Int8{name: name, description: description}
  87. }
  88. func (k *Int8) Name() string { return k.name }
  89. func (k *Int8) Description() string { return k.description }
  90. func (k *Int8) Format(w io.Writer, buf []byte, l label.Label) {
  91. w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
  92. }
  93. // Of creates a new Label with this key and the supplied value.
  94. func (k *Int8) Of(v int8) label.Label { return label.Of64(k, uint64(v)) }
  95. // Get can be used to get a label for the key from a label.Map.
  96. func (k *Int8) Get(lm label.Map) int8 {
  97. if t := lm.Find(k); t.Valid() {
  98. return k.From(t)
  99. }
  100. return 0
  101. }
  102. // From can be used to get a value from a Label.
  103. func (k *Int8) From(t label.Label) int8 { return int8(t.Unpack64()) }
  104. // Int16 represents a key
  105. type Int16 struct {
  106. name string
  107. description string
  108. }
  109. // NewInt16 creates a new Key for int16 values.
  110. func NewInt16(name, description string) *Int16 {
  111. return &Int16{name: name, description: description}
  112. }
  113. func (k *Int16) Name() string { return k.name }
  114. func (k *Int16) Description() string { return k.description }
  115. func (k *Int16) Format(w io.Writer, buf []byte, l label.Label) {
  116. w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
  117. }
  118. // Of creates a new Label with this key and the supplied value.
  119. func (k *Int16) Of(v int16) label.Label { return label.Of64(k, uint64(v)) }
  120. // Get can be used to get a label for the key from a label.Map.
  121. func (k *Int16) Get(lm label.Map) int16 {
  122. if t := lm.Find(k); t.Valid() {
  123. return k.From(t)
  124. }
  125. return 0
  126. }
  127. // From can be used to get a value from a Label.
  128. func (k *Int16) From(t label.Label) int16 { return int16(t.Unpack64()) }
  129. // Int32 represents a key
  130. type Int32 struct {
  131. name string
  132. description string
  133. }
  134. // NewInt32 creates a new Key for int32 values.
  135. func NewInt32(name, description string) *Int32 {
  136. return &Int32{name: name, description: description}
  137. }
  138. func (k *Int32) Name() string { return k.name }
  139. func (k *Int32) Description() string { return k.description }
  140. func (k *Int32) Format(w io.Writer, buf []byte, l label.Label) {
  141. w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
  142. }
  143. // Of creates a new Label with this key and the supplied value.
  144. func (k *Int32) Of(v int32) label.Label { return label.Of64(k, uint64(v)) }
  145. // Get can be used to get a label for the key from a label.Map.
  146. func (k *Int32) Get(lm label.Map) int32 {
  147. if t := lm.Find(k); t.Valid() {
  148. return k.From(t)
  149. }
  150. return 0
  151. }
  152. // From can be used to get a value from a Label.
  153. func (k *Int32) From(t label.Label) int32 { return int32(t.Unpack64()) }
  154. // Int64 represents a key
  155. type Int64 struct {
  156. name string
  157. description string
  158. }
  159. // NewInt64 creates a new Key for int64 values.
  160. func NewInt64(name, description string) *Int64 {
  161. return &Int64{name: name, description: description}
  162. }
  163. func (k *Int64) Name() string { return k.name }
  164. func (k *Int64) Description() string { return k.description }
  165. func (k *Int64) Format(w io.Writer, buf []byte, l label.Label) {
  166. w.Write(strconv.AppendInt(buf, k.From(l), 10))
  167. }
  168. // Of creates a new Label with this key and the supplied value.
  169. func (k *Int64) Of(v int64) label.Label { return label.Of64(k, uint64(v)) }
  170. // Get can be used to get a label for the key from a label.Map.
  171. func (k *Int64) Get(lm label.Map) int64 {
  172. if t := lm.Find(k); t.Valid() {
  173. return k.From(t)
  174. }
  175. return 0
  176. }
  177. // From can be used to get a value from a Label.
  178. func (k *Int64) From(t label.Label) int64 { return int64(t.Unpack64()) }
  179. // UInt represents a key
  180. type UInt struct {
  181. name string
  182. description string
  183. }
  184. // NewUInt creates a new Key for uint values.
  185. func NewUInt(name, description string) *UInt {
  186. return &UInt{name: name, description: description}
  187. }
  188. func (k *UInt) Name() string { return k.name }
  189. func (k *UInt) Description() string { return k.description }
  190. func (k *UInt) Format(w io.Writer, buf []byte, l label.Label) {
  191. w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
  192. }
  193. // Of creates a new Label with this key and the supplied value.
  194. func (k *UInt) Of(v uint) label.Label { return label.Of64(k, uint64(v)) }
  195. // Get can be used to get a label for the key from a label.Map.
  196. func (k *UInt) Get(lm label.Map) uint {
  197. if t := lm.Find(k); t.Valid() {
  198. return k.From(t)
  199. }
  200. return 0
  201. }
  202. // From can be used to get a value from a Label.
  203. func (k *UInt) From(t label.Label) uint { return uint(t.Unpack64()) }
  204. // UInt8 represents a key
  205. type UInt8 struct {
  206. name string
  207. description string
  208. }
  209. // NewUInt8 creates a new Key for uint8 values.
  210. func NewUInt8(name, description string) *UInt8 {
  211. return &UInt8{name: name, description: description}
  212. }
  213. func (k *UInt8) Name() string { return k.name }
  214. func (k *UInt8) Description() string { return k.description }
  215. func (k *UInt8) Format(w io.Writer, buf []byte, l label.Label) {
  216. w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
  217. }
  218. // Of creates a new Label with this key and the supplied value.
  219. func (k *UInt8) Of(v uint8) label.Label { return label.Of64(k, uint64(v)) }
  220. // Get can be used to get a label for the key from a label.Map.
  221. func (k *UInt8) Get(lm label.Map) uint8 {
  222. if t := lm.Find(k); t.Valid() {
  223. return k.From(t)
  224. }
  225. return 0
  226. }
  227. // From can be used to get a value from a Label.
  228. func (k *UInt8) From(t label.Label) uint8 { return uint8(t.Unpack64()) }
  229. // UInt16 represents a key
  230. type UInt16 struct {
  231. name string
  232. description string
  233. }
  234. // NewUInt16 creates a new Key for uint16 values.
  235. func NewUInt16(name, description string) *UInt16 {
  236. return &UInt16{name: name, description: description}
  237. }
  238. func (k *UInt16) Name() string { return k.name }
  239. func (k *UInt16) Description() string { return k.description }
  240. func (k *UInt16) Format(w io.Writer, buf []byte, l label.Label) {
  241. w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
  242. }
  243. // Of creates a new Label with this key and the supplied value.
  244. func (k *UInt16) Of(v uint16) label.Label { return label.Of64(k, uint64(v)) }
  245. // Get can be used to get a label for the key from a label.Map.
  246. func (k *UInt16) Get(lm label.Map) uint16 {
  247. if t := lm.Find(k); t.Valid() {
  248. return k.From(t)
  249. }
  250. return 0
  251. }
  252. // From can be used to get a value from a Label.
  253. func (k *UInt16) From(t label.Label) uint16 { return uint16(t.Unpack64()) }
  254. // UInt32 represents a key
  255. type UInt32 struct {
  256. name string
  257. description string
  258. }
  259. // NewUInt32 creates a new Key for uint32 values.
  260. func NewUInt32(name, description string) *UInt32 {
  261. return &UInt32{name: name, description: description}
  262. }
  263. func (k *UInt32) Name() string { return k.name }
  264. func (k *UInt32) Description() string { return k.description }
  265. func (k *UInt32) Format(w io.Writer, buf []byte, l label.Label) {
  266. w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
  267. }
  268. // Of creates a new Label with this key and the supplied value.
  269. func (k *UInt32) Of(v uint32) label.Label { return label.Of64(k, uint64(v)) }
  270. // Get can be used to get a label for the key from a label.Map.
  271. func (k *UInt32) Get(lm label.Map) uint32 {
  272. if t := lm.Find(k); t.Valid() {
  273. return k.From(t)
  274. }
  275. return 0
  276. }
  277. // From can be used to get a value from a Label.
  278. func (k *UInt32) From(t label.Label) uint32 { return uint32(t.Unpack64()) }
  279. // UInt64 represents a key
  280. type UInt64 struct {
  281. name string
  282. description string
  283. }
  284. // NewUInt64 creates a new Key for uint64 values.
  285. func NewUInt64(name, description string) *UInt64 {
  286. return &UInt64{name: name, description: description}
  287. }
  288. func (k *UInt64) Name() string { return k.name }
  289. func (k *UInt64) Description() string { return k.description }
  290. func (k *UInt64) Format(w io.Writer, buf []byte, l label.Label) {
  291. w.Write(strconv.AppendUint(buf, k.From(l), 10))
  292. }
  293. // Of creates a new Label with this key and the supplied value.
  294. func (k *UInt64) Of(v uint64) label.Label { return label.Of64(k, v) }
  295. // Get can be used to get a label for the key from a label.Map.
  296. func (k *UInt64) Get(lm label.Map) uint64 {
  297. if t := lm.Find(k); t.Valid() {
  298. return k.From(t)
  299. }
  300. return 0
  301. }
  302. // From can be used to get a value from a Label.
  303. func (k *UInt64) From(t label.Label) uint64 { return t.Unpack64() }
  304. // Float32 represents a key
  305. type Float32 struct {
  306. name string
  307. description string
  308. }
  309. // NewFloat32 creates a new Key for float32 values.
  310. func NewFloat32(name, description string) *Float32 {
  311. return &Float32{name: name, description: description}
  312. }
  313. func (k *Float32) Name() string { return k.name }
  314. func (k *Float32) Description() string { return k.description }
  315. func (k *Float32) Format(w io.Writer, buf []byte, l label.Label) {
  316. w.Write(strconv.AppendFloat(buf, float64(k.From(l)), 'E', -1, 32))
  317. }
  318. // Of creates a new Label with this key and the supplied value.
  319. func (k *Float32) Of(v float32) label.Label {
  320. return label.Of64(k, uint64(math.Float32bits(v)))
  321. }
  322. // Get can be used to get a label for the key from a label.Map.
  323. func (k *Float32) Get(lm label.Map) float32 {
  324. if t := lm.Find(k); t.Valid() {
  325. return k.From(t)
  326. }
  327. return 0
  328. }
  329. // From can be used to get a value from a Label.
  330. func (k *Float32) From(t label.Label) float32 {
  331. return math.Float32frombits(uint32(t.Unpack64()))
  332. }
  333. // Float64 represents a key
  334. type Float64 struct {
  335. name string
  336. description string
  337. }
  338. // NewFloat64 creates a new Key for int64 values.
  339. func NewFloat64(name, description string) *Float64 {
  340. return &Float64{name: name, description: description}
  341. }
  342. func (k *Float64) Name() string { return k.name }
  343. func (k *Float64) Description() string { return k.description }
  344. func (k *Float64) Format(w io.Writer, buf []byte, l label.Label) {
  345. w.Write(strconv.AppendFloat(buf, k.From(l), 'E', -1, 64))
  346. }
  347. // Of creates a new Label with this key and the supplied value.
  348. func (k *Float64) Of(v float64) label.Label {
  349. return label.Of64(k, math.Float64bits(v))
  350. }
  351. // Get can be used to get a label for the key from a label.Map.
  352. func (k *Float64) Get(lm label.Map) float64 {
  353. if t := lm.Find(k); t.Valid() {
  354. return k.From(t)
  355. }
  356. return 0
  357. }
  358. // From can be used to get a value from a Label.
  359. func (k *Float64) From(t label.Label) float64 {
  360. return math.Float64frombits(t.Unpack64())
  361. }
  362. // String represents a key
  363. type String struct {
  364. name string
  365. description string
  366. }
  367. // NewString creates a new Key for int64 values.
  368. func NewString(name, description string) *String {
  369. return &String{name: name, description: description}
  370. }
  371. func (k *String) Name() string { return k.name }
  372. func (k *String) Description() string { return k.description }
  373. func (k *String) Format(w io.Writer, buf []byte, l label.Label) {
  374. w.Write(strconv.AppendQuote(buf, k.From(l)))
  375. }
  376. // Of creates a new Label with this key and the supplied value.
  377. func (k *String) Of(v string) label.Label { return label.OfString(k, v) }
  378. // Get can be used to get a label for the key from a label.Map.
  379. func (k *String) Get(lm label.Map) string {
  380. if t := lm.Find(k); t.Valid() {
  381. return k.From(t)
  382. }
  383. return ""
  384. }
  385. // From can be used to get a value from a Label.
  386. func (k *String) From(t label.Label) string { return t.UnpackString() }
  387. // Boolean represents a key
  388. type Boolean struct {
  389. name string
  390. description string
  391. }
  392. // NewBoolean creates a new Key for bool values.
  393. func NewBoolean(name, description string) *Boolean {
  394. return &Boolean{name: name, description: description}
  395. }
  396. func (k *Boolean) Name() string { return k.name }
  397. func (k *Boolean) Description() string { return k.description }
  398. func (k *Boolean) Format(w io.Writer, buf []byte, l label.Label) {
  399. w.Write(strconv.AppendBool(buf, k.From(l)))
  400. }
  401. // Of creates a new Label with this key and the supplied value.
  402. func (k *Boolean) Of(v bool) label.Label {
  403. if v {
  404. return label.Of64(k, 1)
  405. }
  406. return label.Of64(k, 0)
  407. }
  408. // Get can be used to get a label for the key from a label.Map.
  409. func (k *Boolean) Get(lm label.Map) bool {
  410. if t := lm.Find(k); t.Valid() {
  411. return k.From(t)
  412. }
  413. return false
  414. }
  415. // From can be used to get a value from a Label.
  416. func (k *Boolean) From(t label.Label) bool { return t.Unpack64() > 0 }
  417. // Error represents a key
  418. type Error struct {
  419. name string
  420. description string
  421. }
  422. // NewError creates a new Key for int64 values.
  423. func NewError(name, description string) *Error {
  424. return &Error{name: name, description: description}
  425. }
  426. func (k *Error) Name() string { return k.name }
  427. func (k *Error) Description() string { return k.description }
  428. func (k *Error) Format(w io.Writer, buf []byte, l label.Label) {
  429. io.WriteString(w, k.From(l).Error())
  430. }
  431. // Of creates a new Label with this key and the supplied value.
  432. func (k *Error) Of(v error) label.Label { return label.OfValue(k, v) }
  433. // Get can be used to get a label for the key from a label.Map.
  434. func (k *Error) Get(lm label.Map) error {
  435. if t := lm.Find(k); t.Valid() {
  436. return k.From(t)
  437. }
  438. return nil
  439. }
  440. // From can be used to get a value from a Label.
  441. func (k *Error) From(t label.Label) error {
  442. err, _ := t.UnpackValue().(error)
  443. return err
  444. }