2019-02-08 23:44:20 +00:00
|
|
|
package mctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-02-09 19:08:30 +00:00
|
|
|
"sort"
|
2019-02-08 23:44:20 +00:00
|
|
|
)
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
type ctxKeyAnnotation int
|
|
|
|
|
|
|
|
// Annotator is a type which can add annotation data to an existing set of
|
|
|
|
// Annotations. The Annotate method should be expected to be called in a
|
|
|
|
// non-thread-safe manner.
|
|
|
|
type Annotator interface {
|
|
|
|
Annotate(Annotations)
|
|
|
|
}
|
|
|
|
|
|
|
|
type el struct {
|
|
|
|
annotator Annotator
|
|
|
|
prev *el
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// WithAnnotator takes in an Annotator and returns a Context which will produce
|
|
|
|
// that Annotator's annotations when the Annotate function is called. The
|
|
|
|
// Annotator will be not be evaluated until the first call to Annotate.
|
|
|
|
func WithAnnotator(ctx context.Context, annotator Annotator) context.Context {
|
|
|
|
curr := &el{annotator: annotator}
|
|
|
|
curr.prev, _ = ctx.Value(ctxKeyAnnotation(0)).(*el)
|
|
|
|
return context.WithValue(ctx, ctxKeyAnnotation(0), curr)
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
type annotationSeq []interface{}
|
|
|
|
|
|
|
|
func (s annotationSeq) Annotate(aa Annotations) {
|
|
|
|
for i := 0; i < len(s); i += 2 {
|
|
|
|
aa[s[i]] = s[i+1]
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Annotate is a shortcut for calling WithAnnotator using an Annotations
|
|
|
|
// containing the given key/value pairs.
|
|
|
|
//
|
|
|
|
// NOTE If the length of kvs is not divisible by two this will panic.
|
2019-02-08 23:44:20 +00:00
|
|
|
func Annotate(ctx context.Context, kvs ...interface{}) context.Context {
|
2019-02-09 19:08:30 +00:00
|
|
|
if len(kvs)%2 > 0 {
|
|
|
|
panic("kvs being passed to mctx.Annotate must have an even number of elements")
|
|
|
|
} else if len(kvs) == 0 {
|
|
|
|
return ctx
|
|
|
|
}
|
2021-02-07 23:04:41 +00:00
|
|
|
return WithAnnotator(ctx, annotationSeq(kvs))
|
2019-05-18 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Annotations is a set of key/value pairs representing a set of annotations. It
|
|
|
|
// implements the Annotator interface along with other useful post-processing
|
|
|
|
// methods.
|
|
|
|
type Annotations map[interface{}]interface{}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Annotate implements the method for the Annotator interface.
|
|
|
|
func (aa Annotations) Annotate(aa2 Annotations) {
|
|
|
|
for k, v := range aa {
|
|
|
|
aa2[k] = v
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// StringMap formats each of the key/value pairs into strings using fmt.Sprint.
|
|
|
|
// If any two keys format to the same string, then type information will be
|
2019-06-15 23:28:29 +00:00
|
|
|
// prefaced to each one.
|
2021-02-07 23:04:41 +00:00
|
|
|
func (aa Annotations) StringMap() map[string]string {
|
2019-02-09 19:08:30 +00:00
|
|
|
type mKey struct {
|
2019-06-15 23:28:29 +00:00
|
|
|
str string
|
|
|
|
typ string
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
2021-02-07 23:04:41 +00:00
|
|
|
m := map[mKey][][2]interface{}{}
|
|
|
|
for k, v := range aa {
|
|
|
|
mk := mKey{str: fmt.Sprint(k)}
|
|
|
|
m[mk] = append(m[mk], [2]interface{}{k, v})
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
nextK := func(k mKey, kv [2]interface{}) mKey {
|
2019-06-15 23:28:29 +00:00
|
|
|
if k.typ == "" {
|
2021-02-07 23:04:41 +00:00
|
|
|
k.typ = fmt.Sprintf("%T", kv[0])
|
2019-02-09 19:08:30 +00:00
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("mKey %#v is somehow conflicting with another", k))
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
return k
|
|
|
|
}
|
2019-02-08 23:44:20 +00:00
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
for {
|
|
|
|
var any bool
|
|
|
|
for k, annotations := range m {
|
|
|
|
if len(annotations) == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
any = true
|
2021-02-07 23:04:41 +00:00
|
|
|
for _, kv := range annotations {
|
|
|
|
k2 := nextK(k, kv)
|
|
|
|
m[k2] = append(m[k2], kv)
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
delete(m, k)
|
|
|
|
}
|
|
|
|
if !any {
|
|
|
|
break
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
2019-02-08 23:44:20 +00:00
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
outM := map[string]string{}
|
|
|
|
for k, annotations := range m {
|
2021-02-07 23:04:41 +00:00
|
|
|
kv := annotations[0]
|
2019-02-09 19:08:30 +00:00
|
|
|
kStr := k.str
|
|
|
|
if k.typ != "" {
|
2019-06-15 23:28:29 +00:00
|
|
|
kStr = k.typ + "(" + kStr + ")"
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
2021-02-07 23:04:41 +00:00
|
|
|
outM[kStr] = fmt.Sprint(kv[1])
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
return outM
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringSlice is like StringMap but it returns a slice of key/value tuples
|
|
|
|
// rather than a map. If sorted is true then the slice will be sorted by key in
|
|
|
|
// ascending order.
|
2021-02-07 23:04:41 +00:00
|
|
|
func (aa Annotations) StringSlice(sorted bool) [][2]string {
|
2019-02-09 19:08:30 +00:00
|
|
|
m := aa.StringMap()
|
|
|
|
slice := make([][2]string, 0, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
slice = append(slice, [2]string{k, v})
|
|
|
|
}
|
|
|
|
if sorted {
|
|
|
|
sort.Slice(slice, func(i, j int) bool {
|
|
|
|
return slice[i][0] < slice[j][0]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// EvaluateAnnotations collects all annotation key/values which have been set
|
|
|
|
// via Annotate(With) on this Context and its ancestors, and sets those
|
|
|
|
// key/values on the given Annotations. If a key was set twice then only the
|
|
|
|
// most recent value is included.
|
2021-04-09 23:30:59 +00:00
|
|
|
//
|
|
|
|
// For convenience the passed in Annotations is returned from this function, and
|
|
|
|
// if nil is given as the Annotations value then an Annotations will be
|
|
|
|
// allocated and returned.
|
|
|
|
func EvaluateAnnotations(ctx context.Context, aa Annotations) Annotations {
|
|
|
|
if aa == nil {
|
|
|
|
aa = Annotations{}
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
tmp := Annotations{}
|
|
|
|
for el, _ := ctx.Value(ctxKeyAnnotation(0)).(*el); el != nil; el = el.prev {
|
|
|
|
el.annotator.Annotate(tmp)
|
|
|
|
for k, v := range tmp {
|
|
|
|
if _, ok := aa[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
aa[k] = v
|
|
|
|
delete(tmp, k)
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 23:30:59 +00:00
|
|
|
return aa
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
2019-02-08 23:44:20 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
//
|
2019-02-09 19:08:30 +00:00
|
|
|
// MergeAnnotations sequentially merges the annotation data of the passed in
|
2021-02-07 23:04:41 +00:00
|
|
|
// Contexts into the first passed in Context. Data from a Context overwrites
|
2019-06-15 23:28:29 +00:00
|
|
|
// overlapping data on all passed in Contexts to the left of it. All other
|
|
|
|
// aspects of the first Context remain the same, and that Context is returned
|
|
|
|
// with the new set of Annotation data.
|
2021-02-07 23:04:41 +00:00
|
|
|
func MergeAnnotations(ctx context.Context, ctxs ...context.Context) context.Context {
|
|
|
|
aa := Annotations{}
|
|
|
|
tmp := Annotations{}
|
|
|
|
EvaluateAnnotations(ctx, aa)
|
2019-02-27 18:05:51 +00:00
|
|
|
for _, ctxB := range ctxs {
|
2021-02-07 23:04:41 +00:00
|
|
|
EvaluateAnnotations(ctxB, tmp)
|
|
|
|
for k, v := range tmp {
|
|
|
|
aa[k] = v
|
|
|
|
delete(tmp, k)
|
|
|
|
}
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2021-02-07 23:04:41 +00:00
|
|
|
return context.WithValue(ctx, ctxKeyAnnotation(0), &el{annotator: aa})
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2021-04-10 00:05:07 +00:00
|
|
|
|
|
|
|
type ctxAnnotator struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ca ctxAnnotator) Annotate(aa Annotations) {
|
|
|
|
EvaluateAnnotations(ca.ctx, aa)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContextAsAnnotator will return an Annotator which, when evaluated, will
|
|
|
|
// call EvaluateAnnotations on the given Context.
|
|
|
|
func ContextAsAnnotator(ctx context.Context) Annotator {
|
|
|
|
return ctxAnnotator{ctx}
|
|
|
|
}
|