github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/grpclogging/context.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package grpclogging 8 9 import ( 10 "context" 11 12 "go.uber.org/zap/zapcore" 13 ) 14 15 type fieldKeyType struct{} 16 17 var fieldKey = &fieldKeyType{} 18 19 func ZapFields(ctx context.Context) []zapcore.Field { 20 fields, ok := ctx.Value(fieldKey).([]zapcore.Field) 21 if ok { 22 return fields 23 } 24 return nil 25 } 26 27 func Fields(ctx context.Context) []interface{} { 28 fields, ok := ctx.Value(fieldKey).([]zapcore.Field) 29 if !ok { 30 return nil 31 } 32 genericFields := make([]interface{}, len(fields)) 33 for i := range fields { 34 genericFields[i] = fields[i] 35 } 36 return genericFields 37 } 38 39 func WithFields(ctx context.Context, fields []zapcore.Field) context.Context { 40 return context.WithValue(ctx, fieldKey, fields) 41 }