-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.go
44 lines (36 loc) · 991 Bytes
/
options.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
package msngr
import (
"fmt"
"reflect"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/celestiaorg/go-libp2p-messenger/serde"
)
// Option defines an functional configurability for Messenger.
type Option func(messenger *Messenger)
// WithProtocols sets custom protocols for messenger to speak with
// At least one protocol is required.
func WithProtocols(pids ...protocol.ID) Option {
return func(m *Messenger) {
m.pids = pids
}
}
// WithMessageType sets a custom message type for receiving messages.
// Otherwise Messenger would not know what type to return in Receive.
func WithMessageType(msg serde.Message) Option {
return func(m *Messenger) {
tp := reflect.TypeOf(msg)
if tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
m.msgTp = tp
}
}
func (m *Messenger) options(opts ...Option) error {
for _, opt := range opts {
opt(m)
}
if m.pids == nil {
return fmt.Errorf("messenger: at least one protocol must be given through an option")
}
return nil
}