From 354a16905072466754e783c79c633509a662b974 Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Tue, 22 Sep 2020 16:08:01 +0200 Subject: [PATCH] Add errors to config methods (#2015) --- config/config.go | 6 +++--- config/store/store.go | 16 ++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 08f01908..845aaf58 100644 --- a/config/config.go +++ b/config/config.go @@ -7,9 +7,9 @@ import ( // Config is an interface abstraction for dynamic configuration type Config interface { - Get(path string, options ...Option) Value - Set(path string, val interface{}, options ...Option) - Delete(path string, options ...Option) + Get(path string, options ...Option) (Value, error) + Set(path string, val interface{}, options ...Option) error + Delete(path string, options ...Option) error } // Value represents a value of any type diff --git a/config/store/store.go b/config/store/store.go index 71eab439..e881cf16 100644 --- a/config/store/store.go +++ b/config/store/store.go @@ -31,17 +31,17 @@ func mergeOptions(old config.Options, nu ...config.Option) config.Options { return n } -func (c *conf) Get(path string, options ...config.Option) config.Value { +func (c *conf) Get(path string, options ...config.Option) (config.Value, error) { rec, err := c.store.Read(c.key) dat := []byte("{}") if err == nil && len(rec) > 0 { dat = rec[0].Value } values := config.NewJSONValues(dat) - return values.Get(path) + return values.Get(path), nil } -func (c *conf) Set(path string, val interface{}, options ...config.Option) { +func (c *conf) Set(path string, val interface{}, options ...config.Option) error { rec, err := c.store.Read(c.key) dat := []byte("{}") if err == nil && len(rec) > 0 { @@ -49,18 +49,22 @@ func (c *conf) Set(path string, val interface{}, options ...config.Option) { } values := config.NewJSONValues(dat) values.Set(path, val) - c.store.Write(&store.Record{ + return c.store.Write(&store.Record{ Key: c.key, Value: values.Bytes(), }) } -func (c *conf) Delete(path string, options ...config.Option) { +func (c *conf) Delete(path string, options ...config.Option) error { rec, err := c.store.Read(c.key) dat := []byte("{}") if err != nil || len(rec) == 0 { - return + return nil } values := config.NewJSONValues(dat) values.Delete(path) + return c.store.Write(&store.Record{ + Key: c.key, + Value: values.Bytes(), + }) }