-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver_func.go
115 lines (107 loc) · 2.74 KB
/
receiver_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package notificationcenter
import (
"context"
"reflect"
"github.com/pkg/errors"
"github.com/geniusrabbit/notificationcenter/v2/decoder"
)
var errInvalidReturnType = errors.New("invalid return types")
// ReceiverFrom converts income handler type to Receiver interface
func ReceiverFrom(handler any) Receiver {
switch h := handler.(type) {
case Receiver:
return h
case func() error:
return FuncReceiver(func(msg Message) error {
if err := h(); err != nil {
return err
}
return msg.Ack()
})
case func(msg Message) error:
return FuncReceiver(h)
case func(ctx context.Context, msg Message) error:
return FuncReceiver(func(msg Message) error { return h(msg.Context(), msg) })
default:
return ExtFuncReceiver(h)
}
}
var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
msgType = reflect.TypeOf((*Message)(nil)).Elem()
)
// ExtFuncReceiver wraps function argument with arbitrary input data type
func ExtFuncReceiver(f any, decs ...decoder.Decoder) Receiver {
fv := reflect.ValueOf(f)
if fv.Kind() != reflect.Func {
panic("argument must be a function")
}
dec := decoder.JSON
if len(decs) > 0 && decs[0] != nil {
dec = decs[0]
}
var (
ft = fv.Type()
argMapper = make([]func(Message) (reflect.Value, error), 0, ft.NumIn())
retMapper = make([]func(reflect.Value) error, 0, ft.NumOut())
)
for i := 0; i < ft.NumIn(); i++ {
inType := ft.In(i)
switch inType {
case contextType:
argMapper = append(argMapper, func(msg Message) (reflect.Value, error) {
return reflect.ValueOf(msg.Context()), nil
})
case msgType:
argMapper = append(argMapper, func(msg Message) (reflect.Value, error) {
return reflect.ValueOf(msg), nil
})
default:
argMapper = append(argMapper, func(msg Message) (reflect.Value, error) {
newValue, newValueI := newValue(inType)
err := dec(msg.Body(), newValueI)
return newValue, err
})
}
}
for i := 0; i < ft.NumOut(); i++ {
outType := ft.Out(i)
switch outType {
case errorType:
retMapper = append(retMapper, func(v reflect.Value) error {
if v.IsNil() {
return nil
}
return v.Interface().(error)
})
default:
panic(errInvalidReturnType)
}
}
return FuncReceiver(func(msg Message) error {
args := make([]reflect.Value, 0, len(argMapper))
for _, fm := range argMapper {
arg, err := fm(msg)
if err != nil {
return err
}
args = append(args, arg)
}
retVals := fv.Call(args)
for i, fr := range retMapper {
if err := fr(retVals[i]); err != nil {
return err
}
}
return nil
})
}
func newValue(t reflect.Type) (reflect.Value, any) {
if t.Kind() == reflect.Ptr {
return newValue(t.Elem())
}
v := reflect.New(t)
i := v.Interface()
return v, i
}