Add errors to config methods (#2015)

This commit is contained in:
Janos Dobronszki
2020-09-22 16:08:01 +02:00
committed by GitHub
parent 6e083b9aca
commit 354a169050
2 changed files with 13 additions and 9 deletions

View File

@@ -7,9 +7,9 @@ import (
// Config is an interface abstraction for dynamic configuration // Config is an interface abstraction for dynamic configuration
type Config interface { type Config interface {
Get(path string, options ...Option) Value Get(path string, options ...Option) (Value, error)
Set(path string, val interface{}, options ...Option) Set(path string, val interface{}, options ...Option) error
Delete(path string, options ...Option) Delete(path string, options ...Option) error
} }
// Value represents a value of any type // Value represents a value of any type

View File

@@ -31,17 +31,17 @@ func mergeOptions(old config.Options, nu ...config.Option) config.Options {
return n 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) rec, err := c.store.Read(c.key)
dat := []byte("{}") dat := []byte("{}")
if err == nil && len(rec) > 0 { if err == nil && len(rec) > 0 {
dat = rec[0].Value dat = rec[0].Value
} }
values := config.NewJSONValues(dat) 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) rec, err := c.store.Read(c.key)
dat := []byte("{}") dat := []byte("{}")
if err == nil && len(rec) > 0 { 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 := config.NewJSONValues(dat)
values.Set(path, val) values.Set(path, val)
c.store.Write(&store.Record{ return c.store.Write(&store.Record{
Key: c.key, Key: c.key,
Value: values.Bytes(), 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) rec, err := c.store.Read(c.key)
dat := []byte("{}") dat := []byte("{}")
if err != nil || len(rec) == 0 { if err != nil || len(rec) == 0 {
return return nil
} }
values := config.NewJSONValues(dat) values := config.NewJSONValues(dat)
values.Delete(path) values.Delete(path)
return c.store.Write(&store.Record{
Key: c.key,
Value: values.Bytes(),
})
} }