blob: 574d696bb269f14def59a0a991c8ea3496f4418e [file] [log] [blame]
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package keys
import (
"golang.org/x/exp/event"
)
// Value represents a key for untyped values.
type Value string
// From can be used to get a value from a Label.
func (k Value) From(l event.Label) interface{} { return l.Value.Interface() }
// Of creates a new Label with this key and the supplied value.
func (k Value) Of(v interface{}) event.Label {
return event.Label{Name: string(k), Value: event.ValueOf(v)}
}
// Tag represents a key for tagging labels that have no value.
// These are used when the existence of the label is the entire information it
// carries, such as marking events to be of a specific kind, or from a specific
// package.
type Tag string
// New creates a new Label with this key.
func (k Tag) New() event.Label {
return event.Label{Name: string(k)}
}
// Int represents a key
type Int string
// Of creates a new Label with this key and the supplied value.
func (k Int) Of(v int) event.Label {
return event.Label{Name: string(k), Value: event.Int64Of(int64(v))}
}
// From can be used to get a value from a Label.
func (k Int) From(l event.Label) int { return int(l.Value.Int64()) }
// Int8 represents a key
type Int8 string
// Of creates a new Label with this key and the supplied value.
func (k Int8) Of(v int8) event.Label {
return event.Label{Name: string(k), Value: event.Int64Of(int64(v))}
}
// From can be used to get a value from a Label.
func (k Int8) From(l event.Label) int8 { return int8(l.Value.Int64()) }
// Int16 represents a key
type Int16 string
// Of creates a new Label with this key and the supplied value.
func (k Int16) Of(v int16) event.Label {
return event.Label{Name: string(k), Value: event.Int64Of(int64(v))}
}
// From can be used to get a value from a Label.
func (k Int16) From(l event.Label) int16 { return int16(l.Value.Int64()) }
// Int32 represents a key
type Int32 string
// Of creates a new Label with this key and the supplied value.
func (k Int32) Of(v int32) event.Label {
return event.Label{Name: string(k), Value: event.Int64Of(int64(v))}
}
// From can be used to get a value from a Label.
func (k Int32) From(l event.Label) int32 { return int32(l.Value.Int64()) }
// Int64 represents a key
type Int64 string
// Of creates a new Label with this key and the supplied value.
func (k Int64) Of(v int64) event.Label {
return event.Label{Name: string(k), Value: event.Int64Of(v)}
}
// From can be used to get a value from a Label.
func (k Int64) From(l event.Label) int64 { return l.Value.Int64() }
// UInt represents a key
type UInt string
// Of creates a new Label with this key and the supplied value.
func (k UInt) Of(v uint) event.Label {
return event.Label{Name: string(k), Value: event.Uint64Of(uint64(v))}
}
// From can be used to get a value from a Label.
func (k UInt) From(l event.Label) uint { return uint(l.Value.Uint64()) }
// UInt8 represents a key
type UInt8 string
// Of creates a new Label with this key and the supplied value.
func (k UInt8) Of(v uint8) event.Label {
return event.Label{Name: string(k), Value: event.Uint64Of(uint64(v))}
}
// From can be used to get a value from a Label.
func (k UInt8) From(l event.Label) uint8 { return uint8(l.Value.Uint64()) }
// UInt16 represents a key
type UInt16 string
// Of creates a new Label with this key and the supplied value.
func (k UInt16) Of(v uint16) event.Label {
return event.Label{Name: string(k), Value: event.Uint64Of(uint64(v))}
}
// From can be used to get a value from a Label.
func (k UInt16) From(l event.Label) uint16 { return uint16(l.Value.Uint64()) }
// UInt32 represents a key
type UInt32 string
// Of creates a new Label with this key and the supplied value.
func (k UInt32) Of(v uint32) event.Label {
return event.Label{Name: string(k), Value: event.Uint64Of(uint64(v))}
}
// From can be used to get a value from a Label.
func (k UInt32) From(l event.Label) uint32 { return uint32(l.Value.Uint64()) }
// UInt64 represents a key
type UInt64 string
// Of creates a new Label with this key and the supplied value.
func (k UInt64) Of(v uint64) event.Label {
return event.Label{Name: string(k), Value: event.Uint64Of(v)}
}
// From can be used to get a value from a Label.
func (k UInt64) From(l event.Label) uint64 { return l.Value.Uint64() }
// Float32 represents a key
type Float32 string
// Of creates a new Label with this key and the supplied value.
func (k Float32) Of(v float32) event.Label {
return event.Label{Name: string(k), Value: event.Float64Of(float64(v))}
}
// From can be used to get a value from a Label.
func (k Float32) From(l event.Label) float32 { return float32(l.Value.Float64()) }
// Float64 represents a key
type Float64 string
// Of creates a new Label with this key and the supplied value.
func (k Float64) Of(v float64) event.Label {
return event.Label{Name: string(k), Value: event.Float64Of(v)}
}
// From can be used to get a value from a Label.
func (k Float64) From(l event.Label) float64 {
return l.Value.Float64()
}
// String represents a key
type String string
// Of creates a new Label with this key and the supplied value.
func (k String) Of(v string) event.Label {
return event.Label{Name: string(k), Value: event.StringOf(v)}
}
// From can be used to get a value from a Label.
func (k String) From(l event.Label) string { return l.Value.String() }
// Bool represents a key
type Bool string
// Of creates a new Label with this key and the supplied value.
func (k Bool) Of(v bool) event.Label {
return event.Label{Name: string(k), Value: event.BoolOf(v)}
}
// From can be used to get a value from a Label.
func (k Bool) From(l event.Label) bool { return l.Value.Bool() }