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
|
|
|
)
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// Annotation describes the annotation of a key/value pair made on a Context via
|
2019-06-15 23:28:29 +00:00
|
|
|
// the Annotate call.
|
2019-02-09 19:08:30 +00:00
|
|
|
type Annotation struct {
|
|
|
|
Key, Value interface{}
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
type annotation struct {
|
|
|
|
Annotation
|
|
|
|
root, prev *annotation
|
|
|
|
}
|
|
|
|
|
|
|
|
type annotationKey int
|
|
|
|
|
2019-02-08 23:44:20 +00:00
|
|
|
// Annotate takes in one or more key/value pairs (kvs' length must be even) and
|
2019-06-15 23:28:29 +00:00
|
|
|
// returns a Context carrying them.
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// if multiple annotations are passed in here it's not actually necessary to
|
|
|
|
// create an intermediate Context for each one, so keep curr outside and
|
|
|
|
// only use it later
|
|
|
|
var curr, root *annotation
|
|
|
|
prev, _ := ctx.Value(annotationKey(0)).(*annotation)
|
|
|
|
if prev != nil {
|
|
|
|
root = prev.root
|
|
|
|
}
|
2019-02-08 23:44:20 +00:00
|
|
|
for i := 0; i < len(kvs); i += 2 {
|
2019-02-09 19:08:30 +00:00
|
|
|
curr = &annotation{
|
2019-06-15 23:28:29 +00:00
|
|
|
Annotation: Annotation{Key: kvs[i], Value: kvs[i+1]},
|
|
|
|
prev: prev,
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
if root == nil {
|
|
|
|
root = curr
|
|
|
|
}
|
|
|
|
curr.root = curr
|
|
|
|
prev = curr
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, annotationKey(0), curr)
|
2019-02-08 23:44:20 +00:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-05-18 18:15:35 +00:00
|
|
|
// Annotated is a shortcut for calling Annotate with a context.Background().
|
|
|
|
func Annotated(kvs ...interface{}) context.Context {
|
|
|
|
return Annotate(context.Background(), kvs...)
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// AnnotationSet describes a set of unique Annotation values which were
|
|
|
|
// retrieved off a Context via the Annotations function. An AnnotationSet has a
|
|
|
|
// couple methods on it to aid in post-processing.
|
|
|
|
type AnnotationSet []Annotation
|
|
|
|
|
|
|
|
// Annotations returns all Annotation values which have been set via Annotate on
|
2019-05-18 18:15:35 +00:00
|
|
|
// this Context and its ancestors. If a key was set twice then only the most
|
|
|
|
// recent value is included.
|
2019-02-09 19:08:30 +00:00
|
|
|
func Annotations(ctx context.Context) AnnotationSet {
|
|
|
|
a, _ := ctx.Value(annotationKey(0)).(*annotation)
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-15 23:28:29 +00:00
|
|
|
m := map[interface{}]bool{}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
|
|
|
var aa AnnotationSet
|
|
|
|
for {
|
|
|
|
if a == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-06-15 23:28:29 +00:00
|
|
|
if m[a.Key] {
|
2019-02-09 19:08:30 +00:00
|
|
|
a = a.prev
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
aa = append(aa, a.Annotation)
|
2019-06-15 23:28:29 +00:00
|
|
|
m[a.Key] = true
|
2019-02-09 19:08:30 +00:00
|
|
|
a = a.prev
|
|
|
|
}
|
|
|
|
return aa
|
|
|
|
}
|
|
|
|
|
2019-05-18 18:15:35 +00:00
|
|
|
// StringMap formats each of the Annotations into strings using fmt.Sprint. If
|
2019-06-15 23:28:29 +00:00
|
|
|
// any two keys format to the same string, then type information will be
|
|
|
|
// prefaced to each one.
|
2019-02-09 19:08:30 +00:00
|
|
|
func (aa AnnotationSet) StringMap() map[string]string {
|
|
|
|
type mKey struct {
|
2019-06-15 23:28:29 +00:00
|
|
|
str string
|
|
|
|
typ string
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
m := map[mKey][]Annotation{}
|
|
|
|
for _, a := range aa {
|
|
|
|
k := mKey{str: fmt.Sprint(a.Key)}
|
|
|
|
m[k] = append(m[k], a)
|
|
|
|
}
|
|
|
|
|
|
|
|
nextK := func(k mKey, a Annotation) mKey {
|
2019-06-15 23:28:29 +00:00
|
|
|
if k.typ == "" {
|
2019-02-09 19:08:30 +00:00
|
|
|
k.typ = fmt.Sprintf("%T", a.Key)
|
|
|
|
} 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
|
|
|
|
for _, a := range annotations {
|
|
|
|
k2 := nextK(k, a)
|
|
|
|
m[k2] = append(m[k2], a)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
a := annotations[0]
|
|
|
|
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
|
|
|
}
|
|
|
|
outM[kStr] = fmt.Sprint(a.Value)
|
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.
|
|
|
|
func (aa AnnotationSet) StringSlice(sorted bool) [][2]string {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeAnnotations(ctxA, ctxB context.Context) context.Context {
|
|
|
|
annotationA, _ := ctxA.Value(annotationKey(0)).(*annotation)
|
|
|
|
annotationB, _ := ctxB.Value(annotationKey(0)).(*annotation)
|
|
|
|
if annotationB == nil {
|
|
|
|
return ctxA
|
|
|
|
} else if annotationA == nil {
|
|
|
|
return context.WithValue(ctxA, annotationKey(0), annotationB)
|
|
|
|
}
|
|
|
|
|
|
|
|
var headA, currA *annotation
|
|
|
|
currB := annotationB
|
|
|
|
for {
|
|
|
|
if currB == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
prevA := &annotation{
|
|
|
|
Annotation: currB.Annotation,
|
|
|
|
root: annotationA.root,
|
|
|
|
}
|
|
|
|
if currA != nil {
|
|
|
|
currA.prev = prevA
|
|
|
|
}
|
|
|
|
currA, currB = prevA, currB.prev
|
|
|
|
if headA == nil {
|
|
|
|
headA = currA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currA.prev = annotationA
|
|
|
|
return context.WithValue(ctxA, annotationKey(0), headA)
|
|
|
|
}
|
2019-02-08 23:44:20 +00:00
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// MergeAnnotations sequentially merges the annotation data of the passed in
|
|
|
|
// Contexts into the first passed in one. 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.
|
2019-02-09 19:08:30 +00:00
|
|
|
//
|
|
|
|
// NOTE this will panic if no Contexts are passed in.
|
|
|
|
func MergeAnnotations(ctxs ...context.Context) context.Context {
|
2019-02-27 18:05:51 +00:00
|
|
|
return MergeAnnotationsInto(ctxs[0], ctxs[1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeAnnotationsInto is a convenience function which works like
|
|
|
|
// MergeAnnotations.
|
|
|
|
func MergeAnnotationsInto(ctx context.Context, ctxs ...context.Context) context.Context {
|
|
|
|
for _, ctxB := range ctxs {
|
|
|
|
ctx = mergeAnnotations(ctx, ctxB)
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|
2019-02-27 18:05:51 +00:00
|
|
|
return ctx
|
2019-02-08 23:44:20 +00:00
|
|
|
}
|