2019-07-08 10:53:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
package event
|
2019-07-08 10:53:01 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key represents the key for a context tag.
|
|
|
|
// It is a helper to make use of context tagging slightly easier to read, it is
|
|
|
|
// not strictly needed to use it at all.
|
|
|
|
// It is intended that your common tagging keys are declared as constants of
|
|
|
|
// this type, and then you can use the methods of this type to apply and find
|
|
|
|
// those values in the context.
|
|
|
|
type Key string
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
// TagOf returns a Tag for a key and value.
|
2019-07-10 13:19:29 -06:00
|
|
|
// This is a trivial helper that makes common logging easier to read.
|
2020-03-07 16:02:27 -07:00
|
|
|
func TagOf(key interface{}, value interface{}) Tag {
|
|
|
|
return Tag{Key: key, Value: value}
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|
|
|
|
|
2019-07-08 10:53:01 -06:00
|
|
|
// Of creates a new Tag with this key and the supplied value.
|
|
|
|
// You can use this when building a tag list.
|
2020-03-07 16:02:27 -07:00
|
|
|
func (k Key) Of(v interface{}) Tag {
|
|
|
|
return Tag{Key: k, Value: v}
|
2019-07-08 10:53:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
// From can be used to get a tag for the key from a context.
|
|
|
|
func (k Key) From(ctx context.Context) Tag {
|
|
|
|
return Tag{Key: k, Value: ctx.Value(k)}
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
// With is a wrapper over the Label package level function for just this key.
|
2019-07-08 10:53:01 -06:00
|
|
|
func (k Key) With(ctx context.Context, v interface{}) context.Context {
|
2020-03-07 16:02:27 -07:00
|
|
|
return Label(ctx, Tag{Key: k, Value: v})
|
2019-07-08 10:53:01 -06:00
|
|
|
}
|