syntactic sugar
This commit is contained in:
parent
0b9f78d3ee
commit
3d5c81f672
13
nats.go
13
nats.go
@ -169,7 +169,6 @@ func (n *nbroker) String() string {
|
||||
}
|
||||
|
||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
||||
|
||||
options := broker.Options{
|
||||
// Default codec
|
||||
Codec: json.NewCodec(),
|
||||
@ -180,29 +179,29 @@ func NewBroker(opts ...broker.Option) broker.Broker {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
natsOptions := nats.GetDefaultOptions()
|
||||
natsOpts := nats.GetDefaultOptions()
|
||||
if n, ok := options.Context.Value(optionsKey{}).(nats.Options); ok {
|
||||
natsOptions = n
|
||||
natsOpts = n
|
||||
}
|
||||
|
||||
// broker.Options have higher priority than nats.Options
|
||||
// only if Addrs, Secure or TLSConfig were not set through a broker.Option
|
||||
// we read them from nats.Option
|
||||
if len(options.Addrs) == 0 {
|
||||
options.Addrs = natsOptions.Servers
|
||||
options.Addrs = natsOpts.Servers
|
||||
}
|
||||
|
||||
if !options.Secure {
|
||||
options.Secure = natsOptions.Secure
|
||||
options.Secure = natsOpts.Secure
|
||||
}
|
||||
|
||||
if options.TLSConfig == nil {
|
||||
options.TLSConfig = natsOptions.TLSConfig
|
||||
options.TLSConfig = natsOpts.TLSConfig
|
||||
}
|
||||
|
||||
nb := &nbroker{
|
||||
opts: options,
|
||||
nopts: natsOptions,
|
||||
nopts: natsOpts,
|
||||
addrs: setAddrs(options.Addrs),
|
||||
}
|
||||
|
||||
|
12
nats_test.go
12
nats_test.go
@ -14,21 +14,21 @@ var addrTestCases = []struct {
|
||||
addrs map[string]string // expected address : set address
|
||||
}{
|
||||
{
|
||||
"brokerOptionConstructor",
|
||||
"brokerOpts",
|
||||
"set broker addresses through a broker.Option in constructor",
|
||||
map[string]string{
|
||||
"nats://192.168.10.1:5222": "192.168.10.1:5222",
|
||||
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
|
||||
},
|
||||
{
|
||||
"brokerOptionInit",
|
||||
"brokerInit",
|
||||
"set broker addresses through a broker.Option in broker.Init()",
|
||||
map[string]string{
|
||||
"nats://192.168.10.1:5222": "192.168.10.1:5222",
|
||||
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
|
||||
},
|
||||
{
|
||||
"natsOptionConstructor",
|
||||
"natsOpts",
|
||||
"set broker addresses through the nats.Option in constructor",
|
||||
map[string]string{
|
||||
"nats://192.168.10.1:5222": "192.168.10.1:5222",
|
||||
@ -56,15 +56,15 @@ func TestInitAddrs(t *testing.T) {
|
||||
}
|
||||
|
||||
switch tc.name {
|
||||
case "brokerOptionConstructor":
|
||||
case "brokerOpts":
|
||||
// we know that there are just two addrs in the dict
|
||||
br = NewBroker(broker.Addrs(addrs[0], addrs[1]))
|
||||
br.Init()
|
||||
case "brokerOptionInit":
|
||||
case "brokerInit":
|
||||
br = NewBroker()
|
||||
// we know that there are just two addrs in the dict
|
||||
br.Init(broker.Addrs(addrs[0], addrs[1]))
|
||||
case "natsOptionConstructor":
|
||||
case "natsOpts":
|
||||
nopts := nats.GetDefaultOptions()
|
||||
nopts.Servers = addrs
|
||||
br = NewBroker(Options(nopts))
|
||||
|
@ -9,13 +9,12 @@ import (
|
||||
|
||||
type optionsKey struct{}
|
||||
|
||||
// Options allow to inject a nats.Options struct for configuring
|
||||
// the nats connection
|
||||
func Options(nopts nats.Options) broker.Option {
|
||||
// Options accepts nats.Options
|
||||
func Options(opts nats.Options) broker.Option {
|
||||
return func(o *broker.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, optionsKey{}, nopts)
|
||||
o.Context = context.WithValue(o.Context, optionsKey{}, opts)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user