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:
parent
60474ed38f
commit
2388f662cf
@ -79,7 +79,7 @@ func Sync() error {
|
||||
}
|
||||
|
||||
// Get a value from the config.
|
||||
func Get(path ...string) reader.Value {
|
||||
func Get(path ...string) (reader.Value, error) {
|
||||
return DefaultConfig.Get(path...)
|
||||
}
|
||||
|
||||
|
@ -193,7 +193,7 @@ func (c *config) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *config) Get(path ...string) reader.Value {
|
||||
func (c *config) Get(path ...string) (reader.Value, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
@ -203,7 +203,7 @@ func (c *config) Get(path ...string) reader.Value {
|
||||
}
|
||||
|
||||
// no value
|
||||
return newValue()
|
||||
return newValue(), nil
|
||||
}
|
||||
|
||||
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) {
|
||||
value := c.Get(path...)
|
||||
value, err := c.Get(path...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w, err := c.opts.Loader.Watch(path...)
|
||||
if err != nil {
|
||||
@ -299,8 +302,7 @@ func (w *watcher) Next() (reader.Value, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w.value = v.Get()
|
||||
return w.value, nil
|
||||
return v.Get()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,11 +123,15 @@ func TestConfigMerge(t *testing.T) {
|
||||
t.Fatalf("Expected no error but got %v", err)
|
||||
}
|
||||
|
||||
actualHost := conf.Get("amqp", "host").String("backup")
|
||||
if actualHost != "rabbit.testing.com" {
|
||||
actualHost, err := conf.Get("amqp", "host")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
host := actualHost.String("backup")
|
||||
if host != "rabbit.testing.com" {
|
||||
t.Fatalf("Expected %v but got %v",
|
||||
"rabbit.testing.com",
|
||||
actualHost)
|
||||
host)
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,6 +165,10 @@ func TestConfigWatcherDirtyOverrite(t *testing.T) {
|
||||
for i := range ss {
|
||||
k := fmt.Sprintf("key%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)
|
||||
}
|
||||
}
|
||||
|
@ -181,9 +181,11 @@ func (m *memory) update() {
|
||||
continue
|
||||
}
|
||||
|
||||
val, _ := vals.Get(w.path...)
|
||||
|
||||
uv := updateValue{
|
||||
version: m.snap.Version,
|
||||
value: vals.Get(w.path...),
|
||||
value: val,
|
||||
}
|
||||
|
||||
select {
|
||||
@ -287,7 +289,7 @@ func (m *memory) Get(path ...string) (reader.Value, error) {
|
||||
|
||||
// did sync actually work?
|
||||
if m.vals != nil {
|
||||
return m.vals.Get(path...), nil
|
||||
return m.vals.Get(path...)
|
||||
}
|
||||
|
||||
// assuming vals is nil
|
||||
@ -305,7 +307,7 @@ func (m *memory) Get(path ...string) (reader.Value, error) {
|
||||
m.vals = v
|
||||
|
||||
if m.vals != nil {
|
||||
return m.vals.Get(path...), nil
|
||||
return m.vals.Get(path...)
|
||||
}
|
||||
|
||||
// ok we're going hardcore now
|
||||
|
@ -36,7 +36,9 @@ func TestReader(t *testing.T) {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -30,8 +30,8 @@ func newValues(ch *source.ChangeSet) (reader.Values, error) {
|
||||
return &jsonValues{ch, sj}, nil
|
||||
}
|
||||
|
||||
func (j *jsonValues) Get(path ...string) reader.Value {
|
||||
return &jsonValue{j.sj.GetPath(path...)}
|
||||
func (j *jsonValues) Get(path ...string) (reader.Value, error) {
|
||||
return &jsonValue{j.sj.GetPath(path...)}, nil
|
||||
}
|
||||
|
||||
func (j *jsonValues) Del(path ...string) {
|
||||
|
@ -37,7 +37,11 @@ func TestValues(t *testing.T) {
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -74,7 +78,11 @@ func TestStructArray(t *testing.T) {
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ type Reader interface {
|
||||
// Values is returned by the reader.
|
||||
type Values interface {
|
||||
Bytes() []byte
|
||||
Get(path ...string) Value
|
||||
Get(path ...string) (Value, error)
|
||||
Set(val interface{}, path ...string)
|
||||
Del(path ...string)
|
||||
Map() map[string]interface{}
|
||||
|
@ -56,7 +56,9 @@ func TestCliSourceDefault(t *testing.T) {
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user