2024-03-10 00:10:33 +03:00
|
|
|
package wrappermetadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-03-10 00:12:48 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2024-03-10 00:10:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
client.Client
|
|
|
|
exclude []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWrapper(exclude ...string) client.Wrapper {
|
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
handler := &wrapper{
|
|
|
|
Client: c,
|
|
|
|
exclude: exclude,
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientCallWrapper(exclude ...string) client.CallWrapper {
|
|
|
|
return func(fn client.CallFunc) client.CallFunc {
|
|
|
|
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
if exclude == nil {
|
|
|
|
return fn(ctx, addr, req, rsp, opts)
|
|
|
|
}
|
|
|
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
|
|
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ook || omd == nil {
|
|
|
|
omd = metadata.New(len(imd))
|
|
|
|
}
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, exclude...), false))
|
|
|
|
}
|
|
|
|
return fn(ctx, addr, req, rsp, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 00:12:48 +03:00
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
2024-03-10 00:10:33 +03:00
|
|
|
if w.exclude == nil {
|
|
|
|
return w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
}
|
|
|
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
|
|
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ook || omd == nil {
|
|
|
|
omd = metadata.New(len(imd))
|
|
|
|
}
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, w.exclude...), false))
|
|
|
|
}
|
|
|
|
return w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
}
|
|
|
|
|
2024-03-10 00:12:48 +03:00
|
|
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
2024-03-10 00:10:33 +03:00
|
|
|
if w.exclude == nil {
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
|
|
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ook || omd == nil {
|
|
|
|
omd = metadata.New(len(imd))
|
|
|
|
}
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, w.exclude...), false))
|
|
|
|
}
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerHandlerWrapper(exclude ...string) server.HandlerWrapper {
|
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
if exclude == nil {
|
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
|
|
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ook || omd == nil {
|
|
|
|
omd = metadata.New(len(imd))
|
|
|
|
}
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, metadata.Merge(omd, metadata.Copy(imd, exclude...), false))
|
|
|
|
}
|
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|