...
1 package grpc_ctxtags
2
3 import (
4 "context"
5 )
6
7 type ctxMarker struct{}
8
9 var (
10
11
12 ctxMarkerKey = &ctxMarker{}
13
14 NoopTags = &noopTags{}
15 )
16
17
18
19 type Tags interface {
20
21 Set(key string, value interface{}) Tags
22
23 Has(key string) bool
24
25
26 Values() map[string]interface{}
27 }
28
29 type mapTags struct {
30 values map[string]interface{}
31 }
32
33 func (t *mapTags) Set(key string, value interface{}) Tags {
34 t.values[key] = value
35 return t
36 }
37
38 func (t *mapTags) Has(key string) bool {
39 _, ok := t.values[key]
40 return ok
41 }
42
43 func (t *mapTags) Values() map[string]interface{} {
44 return t.values
45 }
46
47 type noopTags struct{}
48
49 func (t *noopTags) Set(key string, value interface{}) Tags {
50 return t
51 }
52
53 func (t *noopTags) Has(key string) bool {
54 return false
55 }
56
57 func (t *noopTags) Values() map[string]interface{} {
58 return nil
59 }
60
61
62
63 func Extract(ctx context.Context) Tags {
64 t, ok := ctx.Value(ctxMarkerKey).(Tags)
65 if !ok {
66 return NoopTags
67 }
68
69 return t
70 }
71
72 func SetInContext(ctx context.Context, tags Tags) context.Context {
73 return context.WithValue(ctx, ctxMarkerKey, tags)
74 }
75
76 func NewTags() Tags {
77 return &mapTags{values: make(map[string]interface{})}
78 }
79
View as plain text