2020-07-27 15:22:00 +03:00
|
|
|
package mucp
|
2015-06-12 21:52:27 +03:00
|
|
|
|
|
|
|
import (
|
2015-12-02 20:42:14 +03:00
|
|
|
"fmt"
|
2015-06-12 21:52:27 +03:00
|
|
|
"reflect"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/transport"
|
2015-06-12 21:52:27 +03:00
|
|
|
)
|
|
|
|
|
2015-12-02 20:42:14 +03:00
|
|
|
const (
|
|
|
|
subSig = "func(context.Context, interface{}) error"
|
|
|
|
)
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
type handler struct {
|
|
|
|
method reflect.Value
|
|
|
|
reqType reflect.Type
|
|
|
|
ctxType reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
|
|
|
topic string
|
|
|
|
rcvr reflect.Value
|
|
|
|
typ reflect.Type
|
|
|
|
subscriber interface{}
|
|
|
|
handlers []*handler
|
|
|
|
endpoints []*registry.Endpoint
|
2020-07-27 15:22:00 +03:00
|
|
|
opts server.SubscriberOptions
|
2015-06-12 21:52:27 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 16:00:51 +03:00
|
|
|
func newMessage(msg transport.Message) *broker.Message {
|
|
|
|
return &broker.Message{
|
|
|
|
Header: msg.Header,
|
|
|
|
Body: msg.Body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
|
|
|
options := server.SubscriberOptions{
|
2019-05-24 15:06:27 +03:00
|
|
|
AutoAck: true,
|
|
|
|
}
|
|
|
|
|
2016-01-08 17:02:32 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
var endpoints []*registry.Endpoint
|
|
|
|
var handlers []*handler
|
|
|
|
|
|
|
|
if typ := reflect.TypeOf(sub); typ.Kind() == reflect.Func {
|
|
|
|
h := &handler{
|
|
|
|
method: reflect.ValueOf(sub),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
h.reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
h.ctxType = typ.In(0)
|
|
|
|
h.reqType = typ.In(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
|
|
|
endpoints = append(endpoints, ®istry.Endpoint{
|
|
|
|
Name: "Func",
|
|
|
|
Request: extractSubValue(typ),
|
|
|
|
Metadata: map[string]string{
|
2015-10-11 14:12:41 +03:00
|
|
|
"topic": topic,
|
|
|
|
"subscriber": "true",
|
2015-06-12 21:52:27 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
2015-10-11 14:12:41 +03:00
|
|
|
hdlr := reflect.ValueOf(sub)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
method := typ.Method(m)
|
|
|
|
h := &handler{
|
|
|
|
method: method.Func,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch method.Type.NumIn() {
|
|
|
|
case 2:
|
|
|
|
h.reqType = method.Type.In(1)
|
|
|
|
case 3:
|
|
|
|
h.ctxType = method.Type.In(1)
|
|
|
|
h.reqType = method.Type.In(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
|
|
|
endpoints = append(endpoints, ®istry.Endpoint{
|
2015-10-11 14:12:41 +03:00
|
|
|
Name: name + "." + method.Name,
|
2015-06-12 21:52:27 +03:00
|
|
|
Request: extractSubValue(method.Type),
|
|
|
|
Metadata: map[string]string{
|
2015-10-11 14:12:41 +03:00
|
|
|
"topic": topic,
|
|
|
|
"subscriber": "true",
|
2015-06-12 21:52:27 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &subscriber{
|
|
|
|
rcvr: reflect.ValueOf(sub),
|
|
|
|
typ: reflect.TypeOf(sub),
|
|
|
|
topic: topic,
|
|
|
|
subscriber: sub,
|
|
|
|
handlers: handlers,
|
|
|
|
endpoints: endpoints,
|
2016-01-08 17:02:32 +03:00
|
|
|
opts: options,
|
2015-06-12 21:52:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
func validateSubscriber(sub server.Subscriber) error {
|
2015-12-02 20:42:14 +03:00
|
|
|
typ := reflect.TypeOf(sub.Subscriber())
|
|
|
|
var argType reflect.Type
|
|
|
|
|
|
|
|
if typ.Kind() == reflect.Func {
|
|
|
|
name := "Func"
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 2:
|
|
|
|
argType = typ.In(1)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("subscriber %v takes wrong number of args: %v required signature %s", name, typ.NumIn(), subSig)
|
|
|
|
}
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return fmt.Errorf("subscriber %v argument type not exported: %v", name, argType)
|
|
|
|
}
|
|
|
|
if typ.NumOut() != 1 {
|
2016-04-06 20:03:27 +03:00
|
|
|
return fmt.Errorf("subscriber %v has wrong number of outs: %v require signature %s",
|
2015-12-02 20:42:14 +03:00
|
|
|
name, typ.NumOut(), subSig)
|
|
|
|
}
|
|
|
|
if returnType := typ.Out(0); returnType != typeOfError {
|
|
|
|
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hdlr := reflect.ValueOf(sub.Subscriber())
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
method := typ.Method(m)
|
|
|
|
|
|
|
|
switch method.Type.NumIn() {
|
|
|
|
case 3:
|
|
|
|
argType = method.Type.In(2)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("subscriber %v.%v takes wrong number of args: %v required signature %s",
|
|
|
|
name, method.Name, method.Type.NumIn(), subSig)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return fmt.Errorf("%v argument type not exported: %v", name, argType)
|
|
|
|
}
|
|
|
|
if method.Type.NumOut() != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"subscriber %v.%v has wrong number of outs: %v require signature %s",
|
|
|
|
name, method.Name, method.Type.NumOut(), subSig)
|
|
|
|
}
|
|
|
|
if returnType := method.Type.Out(0); returnType != typeOfError {
|
|
|
|
return fmt.Errorf("subscriber %v.%v returns %v not error", name, method.Name, returnType.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Subscriber() interface{} {
|
|
|
|
return s.subscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Endpoints() []*registry.Endpoint {
|
|
|
|
return s.endpoints
|
|
|
|
}
|
2016-01-08 17:02:32 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
func (s *subscriber) Options() server.SubscriberOptions {
|
2016-01-08 17:02:32 +03:00
|
|
|
return s.opts
|
|
|
|
}
|