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