Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
da95cc0762 |
56
metadata.go
56
metadata.go
@ -11,23 +11,23 @@ import (
|
|||||||
|
|
||||||
type wrapper struct {
|
type wrapper struct {
|
||||||
client.Client
|
client.Client
|
||||||
exclude []string
|
keys []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientWrapper(exclude ...string) client.Wrapper {
|
func NewClientWrapper(keys ...string) client.Wrapper {
|
||||||
return func(c client.Client) client.Client {
|
return func(c client.Client) client.Client {
|
||||||
handler := &wrapper{
|
handler := &wrapper{
|
||||||
Client: c,
|
Client: c,
|
||||||
exclude: exclude,
|
keys: keys,
|
||||||
}
|
}
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientCallWrapper(exclude ...string) client.CallWrapper {
|
func NewClientCallWrapper(keys ...string) client.CallWrapper {
|
||||||
return func(fn client.CallFunc) client.CallFunc {
|
return func(fn client.CallFunc) client.CallFunc {
|
||||||
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||||
if exclude == nil {
|
if keys == nil {
|
||||||
return fn(ctx, addr, req, rsp, opts)
|
return fn(ctx, addr, req, rsp, opts)
|
||||||
}
|
}
|
||||||
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
||||||
@ -35,7 +35,14 @@ func NewClientCallWrapper(exclude ...string) client.CallWrapper {
|
|||||||
if !ook || omd == nil {
|
if !ook || omd == nil {
|
||||||
omd = metadata.New(len(imd))
|
omd = metadata.New(len(imd))
|
||||||
}
|
}
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, exclude...), false))
|
for _, k := range keys {
|
||||||
|
if v, ok := imd.Get(k); ok && v != "" {
|
||||||
|
omd.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ook {
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return fn(ctx, addr, req, rsp, opts)
|
return fn(ctx, addr, req, rsp, opts)
|
||||||
}
|
}
|
||||||
@ -43,7 +50,7 @@ func NewClientCallWrapper(exclude ...string) client.CallWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error {
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error {
|
||||||
if w.exclude == nil {
|
if w.keys == nil {
|
||||||
return w.Client.Call(ctx, req, rsp, opts...)
|
return w.Client.Call(ctx, req, rsp, opts...)
|
||||||
}
|
}
|
||||||
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
||||||
@ -51,13 +58,20 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{},
|
|||||||
if !ook || omd == nil {
|
if !ook || omd == nil {
|
||||||
omd = metadata.New(len(imd))
|
omd = metadata.New(len(imd))
|
||||||
}
|
}
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, w.exclude...), false))
|
for _, k := range w.keys {
|
||||||
|
if v, ok := imd.Get(k); ok && v != "" {
|
||||||
|
omd.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ook {
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return w.Client.Call(ctx, req, rsp, opts...)
|
return w.Client.Call(ctx, req, rsp, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
|
||||||
if w.exclude == nil {
|
if w.keys == nil {
|
||||||
return w.Client.Stream(ctx, req, opts...)
|
return w.Client.Stream(ctx, req, opts...)
|
||||||
}
|
}
|
||||||
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
||||||
@ -65,15 +79,22 @@ func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...option
|
|||||||
if !ook || omd == nil {
|
if !ook || omd == nil {
|
||||||
omd = metadata.New(len(imd))
|
omd = metadata.New(len(imd))
|
||||||
}
|
}
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, w.exclude...), false))
|
for _, k := range w.keys {
|
||||||
|
if v, ok := imd.Get(k); ok && v != "" {
|
||||||
|
omd.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ook {
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return w.Client.Stream(ctx, req, opts...)
|
return w.Client.Stream(ctx, req, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerHandlerWrapper(exclude ...string) server.HandlerWrapper {
|
func NewServerHandlerWrapper(keys ...string) server.HandlerWrapper {
|
||||||
return func(fn server.HandlerFunc) server.HandlerFunc {
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
||||||
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
||||||
if exclude == nil {
|
if keys == nil {
|
||||||
return fn(ctx, req, rsp)
|
return fn(ctx, req, rsp)
|
||||||
}
|
}
|
||||||
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
||||||
@ -81,7 +102,14 @@ func NewServerHandlerWrapper(exclude ...string) server.HandlerWrapper {
|
|||||||
if !ook || omd == nil {
|
if !ook || omd == nil {
|
||||||
omd = metadata.New(len(imd))
|
omd = metadata.New(len(imd))
|
||||||
}
|
}
|
||||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, exclude...), false))
|
for _, k := range keys {
|
||||||
|
if v, ok := imd.Get(k); ok && v != "" {
|
||||||
|
omd.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ook {
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return fn(ctx, req, rsp)
|
return fn(ctx, req, rsp)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user