github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/tags/context.go (about) 1 package grpc_ctxtags 2 3 import ( 4 "context" 5 ) 6 7 type ctxMarker struct{} 8 9 var ( 10 // ctxMarkerKey is the Context value marker used by *all* logging middleware. 11 // The logging middleware object must interf 12 ctxMarkerKey = &ctxMarker{} 13 // NoopTags is a trivial, minimum overhead implementation of Tags for which all operations are no-ops. 14 NoopTags = &noopTags{} 15 ) 16 17 // Tags is the interface used for storing request tags between Context calls. 18 // The default implementation is *not* thread safe, and should be handled only in the context of the request. 19 type Tags interface { 20 // Set sets the given key in the metadata tags. 21 Set(key string, value interface{}) Tags 22 // Has checks if the given key exists. 23 Has(key string) bool 24 // Values returns a map of key to values. 25 // Do not modify the underlying map, please use Set instead. 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 // Extracts returns a pre-existing Tags object in the Context. 62 // If the context wasn't set in a tag interceptor, a no-op Tag storage is returned that will *not* be propagated in context. 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 }