allow to override path in save/load/watch
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		
							
								
								
									
										39
									
								
								consul.go
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								consul.go
									
									
									
									
									
								
							| @@ -82,15 +82,23 @@ func (c *consulConfig) Init(opts ...config.Option) error { | ||||
| } | ||||
|  | ||||
| func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error { | ||||
| 	path := c.path | ||||
| 	options := config.NewLoadOptions(opts...) | ||||
| 	if options.Context != nil { | ||||
| 		if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" { | ||||
| 			path = v | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if err := config.DefaultBeforeLoad(ctx, c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	pair, _, err := c.cli.KV().Get(c.path, nil) | ||||
| 	pair, _, err := c.cli.KV().Get(path, nil) | ||||
| 	if err != nil { | ||||
| 		err = fmt.Errorf("consul path %s load error: %w", c.path, err) | ||||
| 		err = fmt.Errorf("consul path %s load error: %w", path, err) | ||||
| 	} else if pair == nil { | ||||
| 		err = fmt.Errorf("consul path %s load error: not found", c.path) | ||||
| 		err = fmt.Errorf("consul path %s load error: not found", path) | ||||
| 	} | ||||
|  | ||||
| 	if err != nil { | ||||
| @@ -101,7 +109,6 @@ func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) erro | ||||
| 		return config.DefaultAfterLoad(ctx, c) | ||||
| 	} | ||||
|  | ||||
| 	options := config.NewLoadOptions(opts...) | ||||
| 	mopts := []func(*mergo.Config){mergo.WithTypeCheck} | ||||
| 	if options.Override { | ||||
| 		mopts = append(mopts, mergo.WithOverride) | ||||
| @@ -138,19 +145,27 @@ func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) erro | ||||
| } | ||||
|  | ||||
| func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error { | ||||
| 	path := c.path | ||||
| 	options := config.NewSaveOptions(opts...) | ||||
| 	if options.Context != nil { | ||||
| 		if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" { | ||||
| 			path = v | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if err := config.DefaultBeforeSave(ctx, c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	buf, err := c.opts.Codec.Marshal(c.opts.Struct) | ||||
| 	if err == nil { | ||||
| 		_, err = c.cli.KV().Put(&api.KVPair{Key: c.path, Value: buf}, nil) | ||||
| 		_, err = c.cli.KV().Put(&api.KVPair{Key: path, Value: buf}, nil) | ||||
| 	} | ||||
|  | ||||
| 	if err != nil { | ||||
| 		c.opts.Logger.Errorf(c.opts.Context, "consul path %s save error: %v", c.path, err) | ||||
| 		c.opts.Logger.Errorf(c.opts.Context, "consul path %s save error: %v", path, err) | ||||
| 		if !c.opts.AllowFail { | ||||
| 			return fmt.Errorf("consul path %s save error: %v", c.path, err) | ||||
| 			return fmt.Errorf("consul path %s save error: %v", path, err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -170,9 +185,17 @@ func (c *consulConfig) Name() string { | ||||
| } | ||||
|  | ||||
| func (c *consulConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) { | ||||
| 	path := c.path | ||||
| 	options := config.NewWatchOptions(opts...) | ||||
| 	if options.Context != nil { | ||||
| 		if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" { | ||||
| 			path = v | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	w := &consulWatcher{ | ||||
| 		cli:   c.cli, | ||||
| 		path:  c.path, | ||||
| 		path:  path, | ||||
| 		opts:  c.opts, | ||||
| 		wopts: config.NewWatchOptions(opts...), | ||||
| 		done:  make(chan struct{}), | ||||
|   | ||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @@ -16,5 +16,5 @@ require ( | ||||
| 	github.com/mitchellh/mapstructure v1.4.2 // indirect | ||||
| 	github.com/pkg/errors v0.9.1 // indirect | ||||
| 	github.com/stretchr/testify v1.7.0 // indirect | ||||
| 	go.unistack.org/micro/v3 v3.8.7 | ||||
| 	go.unistack.org/micro/v3 v3.8.9 | ||||
| ) | ||||
|   | ||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							| @@ -164,8 +164,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ | ||||
| github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= | ||||
| go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA= | ||||
| go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8= | ||||
| go.unistack.org/micro/v3 v3.8.7 h1:k1zOpJ3uS8MxdhK8annRsa5J/LW7MpqPjwYuekW61wE= | ||||
| go.unistack.org/micro/v3 v3.8.7/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA= | ||||
| go.unistack.org/micro/v3 v3.8.9 h1:F+HAQSHI86F8Xx5D6XKvvuN0TtOuVvJM+OBV8aYTg0Y= | ||||
| go.unistack.org/micro/v3 v3.8.9/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA= | ||||
| golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= | ||||
| golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= | ||||
| golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||||
|   | ||||
							
								
								
									
										12
									
								
								options.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								options.go
									
									
									
									
									
								
							| @@ -44,3 +44,15 @@ type timeoutKey struct{} | ||||
| func Timeout(td time.Duration) config.Option { | ||||
| 	return config.SetOption(timeoutKey{}, td) | ||||
| } | ||||
|  | ||||
| func LoadPath(path string) config.LoadOption { | ||||
| 	return config.SetLoadOption(pathKey{}, path) | ||||
| } | ||||
|  | ||||
| func SavePath(path string) config.SaveOption { | ||||
| 	return config.SetSaveOption(pathKey{}, path) | ||||
| } | ||||
|  | ||||
| func WatchPath(path string) config.WatchOption { | ||||
| 	return config.SetWatchOption(pathKey{}, path) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user