From 3d5c81f6723cc16198ea00329032cc58ac90dd15 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sat, 6 Jan 2018 15:19:15 +0000 Subject: [PATCH] syntactic sugar --- nats.go | 13 ++++++------- nats_test.go | 12 ++++++------ options.go | 7 +++---- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/nats.go b/nats.go index 0b0544e..ac81674 100644 --- a/nats.go +++ b/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), } diff --git a/nats_test.go b/nats_test.go index c95f57a..e827fba 100644 --- a/nats_test.go +++ b/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)) diff --git a/options.go b/options.go index d3ab9bf..46ab343 100644 --- a/options.go +++ b/options.go @@ -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) } }