1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-31 21:59:42 +02:00

break config.Get and return error with value

This commit is contained in:
asim 2025-05-04 21:31:17 +01:00
parent 60474ed38f
commit 2388f662cf
9 changed files with 44 additions and 20 deletions

View File

@ -79,7 +79,7 @@ func Sync() error {
} }
// Get a value from the config. // Get a value from the config.
func Get(path ...string) reader.Value { func Get(path ...string) (reader.Value, error) {
return DefaultConfig.Get(path...) return DefaultConfig.Get(path...)
} }

View File

@ -193,7 +193,7 @@ func (c *config) Close() error {
return nil return nil
} }
func (c *config) Get(path ...string) reader.Value { func (c *config) Get(path ...string) (reader.Value, error) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
@ -203,7 +203,7 @@ func (c *config) Get(path ...string) reader.Value {
} }
// no value // no value
return newValue() return newValue(), nil
} }
func (c *config) Set(val interface{}, path ...string) { func (c *config) Set(val interface{}, path ...string) {
@ -263,7 +263,10 @@ func (c *config) Load(sources ...source.Source) error {
} }
func (c *config) Watch(path ...string) (Watcher, error) { func (c *config) Watch(path ...string) (Watcher, error) {
value := c.Get(path...) value, err := c.Get(path...)
if err != nil {
return nil, err
}
w, err := c.opts.Loader.Watch(path...) w, err := c.opts.Loader.Watch(path...)
if err != nil { if err != nil {
@ -299,8 +302,7 @@ func (w *watcher) Next() (reader.Value, error) {
return nil, err return nil, err
} }
w.value = v.Get() return v.Get()
return w.value, nil
} }
} }

View File

@ -123,11 +123,15 @@ func TestConfigMerge(t *testing.T) {
t.Fatalf("Expected no error but got %v", err) t.Fatalf("Expected no error but got %v", err)
} }
actualHost := conf.Get("amqp", "host").String("backup") actualHost, err := conf.Get("amqp", "host")
if actualHost != "rabbit.testing.com" { if err != nil {
t.Fatal(err)
}
host := actualHost.String("backup")
if host != "rabbit.testing.com" {
t.Fatalf("Expected %v but got %v", t.Fatalf("Expected %v but got %v",
"rabbit.testing.com", "rabbit.testing.com",
actualHost) host)
} }
} }
@ -161,6 +165,10 @@ func TestConfigWatcherDirtyOverrite(t *testing.T) {
for i := range ss { for i := range ss {
k := fmt.Sprintf("key%d", i) k := fmt.Sprintf("key%d", i)
v := fmt.Sprintf("val%d", i) v := fmt.Sprintf("val%d", i)
equalS(t, conf.Get(k).String(""), v) cc, err := conf.Get(k)
if err != nil {
t.Fatal(err)
}
equalS(t, cc.String(""), v)
} }
} }

View File

@ -181,9 +181,11 @@ func (m *memory) update() {
continue continue
} }
val, _ := vals.Get(w.path...)
uv := updateValue{ uv := updateValue{
version: m.snap.Version, version: m.snap.Version,
value: vals.Get(w.path...), value: val,
} }
select { select {
@ -287,7 +289,7 @@ func (m *memory) Get(path ...string) (reader.Value, error) {
// did sync actually work? // did sync actually work?
if m.vals != nil { if m.vals != nil {
return m.vals.Get(path...), nil return m.vals.Get(path...)
} }
// assuming vals is nil // assuming vals is nil
@ -305,7 +307,7 @@ func (m *memory) Get(path ...string) (reader.Value, error) {
m.vals = v m.vals = v
if m.vals != nil { if m.vals != nil {
return m.vals.Get(path...), nil return m.vals.Get(path...)
} }
// ok we're going hardcore now // ok we're going hardcore now

View File

@ -36,7 +36,9 @@ func TestReader(t *testing.T) {
} }
for _, test := range testData { for _, test := range testData {
if v := values.Get(test.path...).String(""); v != test.value { if v, err := values.Get(test.path...); err != nil {
t.Fatal(err)
} else if v.String("") != test.value {
t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path) t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path)
} }
} }

View File

@ -30,8 +30,8 @@ func newValues(ch *source.ChangeSet) (reader.Values, error) {
return &jsonValues{ch, sj}, nil return &jsonValues{ch, sj}, nil
} }
func (j *jsonValues) Get(path ...string) reader.Value { func (j *jsonValues) Get(path ...string) (reader.Value, error) {
return &jsonValue{j.sj.GetPath(path...)} return &jsonValue{j.sj.GetPath(path...)}, nil
} }
func (j *jsonValues) Del(path ...string) { func (j *jsonValues) Del(path ...string) {

View File

@ -37,7 +37,11 @@ func TestValues(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = values.Get(test.path...).Scan(&test.accepter) v, err := values.Get(test.path...)
if err != nil {
t.Fatal(err)
}
err = v.Scan(&test.accepter)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -74,7 +78,11 @@ func TestStructArray(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = values.Get().Scan(&test.accepter) v, err := values.Get()
if err != nil {
t.Fatal(err)
}
err = v.Scan(&test.accepter)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -17,7 +17,7 @@ type Reader interface {
// Values is returned by the reader. // Values is returned by the reader.
type Values interface { type Values interface {
Bytes() []byte Bytes() []byte
Get(path ...string) Value Get(path ...string) (Value, error)
Set(val interface{}, path ...string) Set(val interface{}, path ...string)
Del(path ...string) Del(path ...string)
Map() map[string]interface{} Map() map[string]interface{}

View File

@ -56,7 +56,9 @@ func TestCliSourceDefault(t *testing.T) {
) )
config.Load(cliSrc) config.Load(cliSrc)
if fval := config.Get("flag").String("default"); fval != expVal { if val, err := config.Get("flag"); err != nil {
t.Fatal(err)
} else if fval := val.String("default"); fval != expVal {
t.Fatalf("default flag value not loaded %v != %v", fval, expVal) t.Fatalf("default flag value not loaded %v != %v", fval, expVal)
} }
} }