2019-07-07 13:25:20 +03:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-05-17 01:41:44 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2019-07-07 13:25:20 +03:00
|
|
|
)
|
|
|
|
|
2022-05-17 01:41:44 +03:00
|
|
|
var (
|
2022-12-03 02:27:37 +03:00
|
|
|
DefaultClientErrorFunc = func(req client.Request, rsp interface{}, err error) error {
|
|
|
|
if rsp != nil {
|
|
|
|
return errors.BadGateway(req.Service(), "%v", err)
|
|
|
|
}
|
2022-05-17 01:41:44 +03:00
|
|
|
return errors.BadRequest(req.Service(), "%v", err)
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:27:37 +03:00
|
|
|
DefaultServerErrorFunc = func(req server.Request, rsp interface{}, err error) error {
|
|
|
|
if rsp != nil {
|
|
|
|
return errors.BadGateway(req.Service(), "%v", err)
|
|
|
|
}
|
2022-05-17 01:41:44 +03:00
|
|
|
return errors.BadRequest(req.Service(), "%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultPublishErrorFunc = func(msg client.Message, err error) error {
|
|
|
|
return errors.BadRequest(msg.Topic(), "%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultSubscribeErrorFunc = func(msg server.Message, err error) error {
|
|
|
|
return errors.BadRequest(msg.Topic(), "%v", err)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-12-03 02:27:37 +03:00
|
|
|
ClientErrorFunc func(client.Request, interface{}, error) error
|
|
|
|
ServerErrorFunc func(server.Request, interface{}, error) error
|
2022-05-17 01:41:44 +03:00
|
|
|
PublishErrorFunc func(client.Message, error) error
|
|
|
|
SubscribeErrorFunc func(server.Message, error) error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Options struct holds wrapper options
|
|
|
|
type Options struct {
|
|
|
|
ClientErrorFn ClientErrorFunc
|
|
|
|
ServerErrorFn ServerErrorFunc
|
|
|
|
PublishErrorFn PublishErrorFunc
|
|
|
|
SubscribeErrorFn SubscribeErrorFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option func signature
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2022-12-03 02:27:37 +03:00
|
|
|
func ClientReqErrorFn(fn ClientErrorFunc) Option {
|
2022-05-17 01:41:44 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.ClientErrorFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServerErrorFn(fn ServerErrorFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ServerErrorFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PublishErrorFn(fn PublishErrorFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.PublishErrorFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SubscribeErrorFn(fn SubscribeErrorFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.SubscribeErrorFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
ClientErrorFn: DefaultClientErrorFunc,
|
|
|
|
ServerErrorFn: DefaultServerErrorFunc,
|
|
|
|
PublishErrorFn: DefaultPublishErrorFunc,
|
|
|
|
SubscribeErrorFn: DefaultSubscribeErrorFunc,
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-12-17 01:57:03 +03:00
|
|
|
type validator interface {
|
2019-07-07 13:25:20 +03:00
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
2020-12-17 01:57:03 +03:00
|
|
|
type wrapper struct {
|
|
|
|
client.Client
|
2022-05-17 01:41:44 +03:00
|
|
|
opts Options
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 01:41:44 +03:00
|
|
|
func NewClientWrapper(opts ...Option) client.Wrapper {
|
2020-12-17 01:57:03 +03:00
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
handler := &wrapper{
|
|
|
|
Client: c,
|
2022-05-17 01:41:44 +03:00
|
|
|
opts: NewOptions(opts...),
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 01:41:44 +03:00
|
|
|
func NewClientCallWrapper(opts ...Option) client.CallWrapper {
|
|
|
|
options := NewOptions(opts...)
|
2020-12-17 01:57:03 +03:00
|
|
|
return func(fn client.CallFunc) client.CallFunc {
|
|
|
|
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
if v, ok := req.Body().(validator); ok {
|
2022-12-03 02:17:05 +03:00
|
|
|
if verr := v.Validate(); verr != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return options.ClientErrorFn(req, nil, verr)
|
2022-12-03 02:17:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err := fn(ctx, addr, req, rsp, opts)
|
|
|
|
if v, ok := rsp.(validator); ok {
|
|
|
|
if verr := v.Validate(); verr != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return options.ClientErrorFn(req, rsp, verr)
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 02:17:05 +03:00
|
|
|
return err
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
if v, ok := req.Body().(validator); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return w.opts.ClientErrorFn(req, nil, err)
|
2022-12-03 02:17:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err := w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
if v, ok := rsp.(validator); ok {
|
|
|
|
if verr := v.Validate(); verr != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return w.opts.ClientErrorFn(req, rsp, verr)
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 02:17:05 +03:00
|
|
|
return err
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
|
|
|
if v, ok := req.Body().(validator); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return nil, w.opts.ClientErrorFn(req, nil, err)
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
|
|
|
if v, ok := msg.Payload().(validator); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
2022-05-17 01:41:44 +03:00
|
|
|
return w.opts.PublishErrorFn(msg, err)
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.Client.Publish(ctx, msg, opts...)
|
|
|
|
}
|
|
|
|
|
2022-05-17 01:41:44 +03:00
|
|
|
func NewServerHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
|
|
|
options := NewOptions(opts...)
|
2019-07-07 13:25:20 +03:00
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
2020-12-17 01:57:03 +03:00
|
|
|
if v, ok := req.Body().(validator); ok {
|
2019-07-07 13:25:20 +03:00
|
|
|
if err := v.Validate(); err != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return options.ClientErrorFn(req, nil, err)
|
2022-12-03 02:17:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
if v, ok := rsp.(validator); ok {
|
|
|
|
if verr := v.Validate(); verr != nil {
|
2022-12-03 02:27:37 +03:00
|
|
|
return options.ClientErrorFn(req, rsp, err)
|
2019-07-07 13:25:20 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 02:17:05 +03:00
|
|
|
return err
|
2019-07-07 13:25:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 01:57:03 +03:00
|
|
|
|
2022-05-17 01:41:44 +03:00
|
|
|
func NewServerSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
|
|
|
options := NewOptions(opts...)
|
2020-12-17 01:57:03 +03:00
|
|
|
return func(fn server.SubscriberFunc) server.SubscriberFunc {
|
|
|
|
return func(ctx context.Context, msg server.Message) error {
|
2022-05-17 01:41:44 +03:00
|
|
|
if v, ok := msg.Body().(validator); ok {
|
2020-12-17 01:57:03 +03:00
|
|
|
if err := v.Validate(); err != nil {
|
2022-05-17 01:41:44 +03:00
|
|
|
return options.SubscribeErrorFn(msg, err)
|
2020-12-17 01:57:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fn(ctx, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|