Compare commits

..

3 Commits
master ... v3

Author SHA1 Message Date
28ca015b0e update for latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-23 18:35:53 +03:00
af79e6adc5 rework
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 16:31:57 +03:00
85fff3f75b initial import
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 00:12:48 +03:00
3 changed files with 85 additions and 89 deletions

12
go.mod
View File

@ -1,5 +1,13 @@
module go.unistack.org/micro-wrapper-metadata/v4 module go.unistack.org/micro-wrapper-metadata/v3
go 1.20 go 1.20
require go.unistack.org/micro/v4 v4.0.17 require go.unistack.org/micro/v3 v3.10.66
require (
github.com/golang/protobuf v1.5.4 // indirect
golang.org/x/sys v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

33
go.sum
View File

@ -1,2 +1,31 @@
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
go.unistack.org/micro/v3 v3.10.49 h1:eW+1OCH4ENjfL0Uaw3+Js9pXRB63+NuThfaozM+eWBs=
go.unistack.org/micro/v3 v3.10.49/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
go.unistack.org/micro/v3 v3.10.66 h1:tiG8HnyTC71IZWSC2qT/DmLhJinZJL9qvw+4Fvpm3d4=
go.unistack.org/micro/v3 v3.10.66/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e h1:NumxXLPfHSndr3wBBdeKiVHjGVFzi9RX2HwwQke94iY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

View File

@ -3,37 +3,29 @@ package wrappermetadata
import ( import (
"context" "context"
"go.unistack.org/micro/v4/client" "go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v4/options" "go.unistack.org/micro/v3/server"
"go.unistack.org/micro/v4/server"
) )
type wrapper struct { var DefaultMetadataKeys = []string{"x-request-id"}
client.Client
type hook struct {
keys []string keys []string
} }
func NewClientWrapper(keys ...string) client.Wrapper { func NewHook(keys ...string) *hook {
return func(c client.Client) client.Client { return &hook{keys: keys}
handler := &wrapper{
Client: c,
keys: keys,
}
return handler
}
} }
func NewClientCallWrapper(keys ...string) client.CallWrapper { func metadataCopy(ctx context.Context, keys []string) context.Context {
return func(fn client.CallFunc) client.CallFunc {
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
if keys == nil { if keys == nil {
return fn(ctx, addr, req, rsp, opts) return ctx
} }
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
omd, ook := metadata.FromOutgoingContext(ctx) omd, ook := metadata.FromOutgoingContext(ctx)
if !ook || omd == nil { if !ook || omd == nil {
omd = metadata.New(len(imd)) omd = metadata.New(len(keys))
} }
for _, k := range keys { for _, k := range keys {
if v, ok := imd.Get(k); ok && v != "" { if v, ok := imd.Get(k); ok && v != "" {
@ -44,74 +36,41 @@ func NewClientCallWrapper(keys ...string) client.CallWrapper {
ctx = metadata.NewOutgoingContext(ctx, omd) ctx = metadata.NewOutgoingContext(ctx, omd)
} }
} }
return fn(ctx, addr, req, rsp, opts) return ctx
} }
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall {
return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
return next(metadataCopy(ctx, w.keys), req, rsp, opts...)
} }
} }
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error { func (w *hook) ClientStream(next client.FuncStream) client.FuncStream {
if w.keys == nil { return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
return w.Client.Call(ctx, req, rsp, opts...) return next(metadataCopy(ctx, w.keys), 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))
}
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...)
} }
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) { func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish {
if w.keys == nil { return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
return w.Client.Stream(ctx, req, opts...) return next(metadataCopy(ctx, w.keys), msg, opts...)
} }
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
omd, ook := metadata.FromOutgoingContext(ctx)
if !ook || omd == nil {
omd = metadata.New(len(imd))
}
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...)
} }
func NewServerHandlerWrapper(keys ...string) server.HandlerWrapper { func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish {
return func(fn server.HandlerFunc) server.HandlerFunc { return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) error {
return next(metadataCopy(ctx, w.keys), msgs, opts...)
}
}
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
return func(ctx context.Context, req server.Request, rsp interface{}) error { return func(ctx context.Context, req server.Request, rsp interface{}) error {
if keys == nil { return next(metadataCopy(ctx, w.keys), req, rsp)
return fn(ctx, req, rsp) }
} }
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
omd, ook := metadata.FromOutgoingContext(ctx) func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
if !ook || omd == nil { return func(ctx context.Context, msg server.Message) error {
omd = metadata.New(len(imd)) return next(metadataCopy(ctx, w.keys), msg)
}
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)
}
} }
} }