From 8e4fd16affa3e98cceb2d50f77c09ba01f644e0b Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Jun 2019 18:31:32 +0800 Subject: [PATCH 1/2] Add consul-specific option for config (as registry) --- config/source/consul/consul.go | 5 +++++ config/source/consul/options.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/config/source/consul/consul.go b/config/source/consul/consul.go index 16e85332..c4a6bf60 100644 --- a/config/source/consul/consul.go +++ b/config/source/consul/consul.go @@ -74,6 +74,11 @@ func NewSource(opts ...source.Option) source.Source { // use default config config := api.DefaultConfig() + // use the consul config passed in the options if any + if co, ok := options.Context.Value(consulConfigKey{}).(*api.Config); ok { + config = co + } + // check if there are any addrs a, ok := options.Context.Value(addressKey{}).(string) if ok { diff --git a/config/source/consul/options.go b/config/source/consul/options.go index 9420a803..a1a1fadb 100644 --- a/config/source/consul/options.go +++ b/config/source/consul/options.go @@ -3,6 +3,7 @@ package consul import ( "context" + "github.com/hashicorp/consul/api" "github.com/micro/go-micro/config/source" ) @@ -11,6 +12,7 @@ type prefixKey struct{} type stripPrefixKey struct{} type dcKey struct{} type tokenKey struct{} +type consulConfigKey struct{} // WithAddress sets the consul address func WithAddress(a string) source.Option { @@ -61,3 +63,13 @@ func WithToken(p string) source.Option { o.Context = context.WithValue(o.Context, tokenKey{}, p) } } + +// WithConsulConfig set consul-specific options +func WithConsulConfig(c *api.Config) source.Option { + return func(o *source.Options) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, consulConfigKey{}, c) + } +} From a8dbca756c7e4842ff7c3d59dfde7416046fb4f2 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Jun 2019 22:41:31 +0800 Subject: [PATCH 2/2] rename stuff per feedback --- config/source/consul/consul.go | 2 +- config/source/consul/options.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/source/consul/consul.go b/config/source/consul/consul.go index c4a6bf60..f5c3c695 100644 --- a/config/source/consul/consul.go +++ b/config/source/consul/consul.go @@ -75,7 +75,7 @@ func NewSource(opts ...source.Option) source.Source { config := api.DefaultConfig() // use the consul config passed in the options if any - if co, ok := options.Context.Value(consulConfigKey{}).(*api.Config); ok { + if co, ok := options.Context.Value(configKey{}).(*api.Config); ok { config = co } diff --git a/config/source/consul/options.go b/config/source/consul/options.go index a1a1fadb..8bca6a66 100644 --- a/config/source/consul/options.go +++ b/config/source/consul/options.go @@ -12,7 +12,7 @@ type prefixKey struct{} type stripPrefixKey struct{} type dcKey struct{} type tokenKey struct{} -type consulConfigKey struct{} +type configKey struct{} // WithAddress sets the consul address func WithAddress(a string) source.Option { @@ -64,12 +64,12 @@ func WithToken(p string) source.Option { } } -// WithConsulConfig set consul-specific options -func WithConsulConfig(c *api.Config) source.Option { +// WithConfig set consul-specific options +func WithConfig(c *api.Config) source.Option { return func(o *source.Options) { if o.Context == nil { o.Context = context.Background() } - o.Context = context.WithValue(o.Context, consulConfigKey{}, c) + o.Context = context.WithValue(o.Context, configKey{}, c) } }