Skip to content

Commit 2388f66

Browse files
committed
break config.Get and return error with value
1 parent 60474ed commit 2388f66

File tree

9 files changed

+44
-20
lines changed

9 files changed

+44
-20
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Sync() error {
7979
}
8080

8181
// Get a value from the config.
82-
func Get(path ...string) reader.Value {
82+
func Get(path ...string) (reader.Value, error) {
8383
return DefaultConfig.Get(path...)
8484
}
8585

config/default.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (c *config) Close() error {
193193
return nil
194194
}
195195

196-
func (c *config) Get(path ...string) reader.Value {
196+
func (c *config) Get(path ...string) (reader.Value, error) {
197197
c.RLock()
198198
defer c.RUnlock()
199199

@@ -203,7 +203,7 @@ func (c *config) Get(path ...string) reader.Value {
203203
}
204204

205205
// no value
206-
return newValue()
206+
return newValue(), nil
207207
}
208208

209209
func (c *config) Set(val interface{}, path ...string) {
@@ -263,7 +263,10 @@ func (c *config) Load(sources ...source.Source) error {
263263
}
264264

265265
func (c *config) Watch(path ...string) (Watcher, error) {
266-
value := c.Get(path...)
266+
value, err := c.Get(path...)
267+
if err != nil {
268+
return nil, err
269+
}
267270

268271
w, err := c.opts.Loader.Watch(path...)
269272
if err != nil {
@@ -299,8 +302,7 @@ func (w *watcher) Next() (reader.Value, error) {
299302
return nil, err
300303
}
301304

302-
w.value = v.Get()
303-
return w.value, nil
305+
return v.Get()
304306
}
305307
}
306308

config/default_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ func TestConfigMerge(t *testing.T) {
123123
t.Fatalf("Expected no error but got %v", err)
124124
}
125125

126-
actualHost := conf.Get("amqp", "host").String("backup")
127-
if actualHost != "rabbit.testing.com" {
126+
actualHost, err := conf.Get("amqp", "host")
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
host := actualHost.String("backup")
131+
if host != "rabbit.testing.com" {
128132
t.Fatalf("Expected %v but got %v",
129133
"rabbit.testing.com",
130-
actualHost)
134+
host)
131135
}
132136
}
133137

@@ -161,6 +165,10 @@ func TestConfigWatcherDirtyOverrite(t *testing.T) {
161165
for i := range ss {
162166
k := fmt.Sprintf("key%d", i)
163167
v := fmt.Sprintf("val%d", i)
164-
equalS(t, conf.Get(k).String(""), v)
168+
cc, err := conf.Get(k)
169+
if err != nil {
170+
t.Fatal(err)
171+
}
172+
equalS(t, cc.String(""), v)
165173
}
166174
}

config/loader/memory/memory.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ func (m *memory) update() {
181181
continue
182182
}
183183

184+
val, _ := vals.Get(w.path...)
185+
184186
uv := updateValue{
185187
version: m.snap.Version,
186-
value: vals.Get(w.path...),
188+
value: val,
187189
}
188190

189191
select {
@@ -287,7 +289,7 @@ func (m *memory) Get(path ...string) (reader.Value, error) {
287289

288290
// did sync actually work?
289291
if m.vals != nil {
290-
return m.vals.Get(path...), nil
292+
return m.vals.Get(path...)
291293
}
292294

293295
// assuming vals is nil
@@ -305,7 +307,7 @@ func (m *memory) Get(path ...string) (reader.Value, error) {
305307
m.vals = v
306308

307309
if m.vals != nil {
308-
return m.vals.Get(path...), nil
310+
return m.vals.Get(path...)
309311
}
310312

311313
// ok we're going hardcore now

config/reader/json/json_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func TestReader(t *testing.T) {
3636
}
3737

3838
for _, test := range testData {
39-
if v := values.Get(test.path...).String(""); v != test.value {
39+
if v, err := values.Get(test.path...); err != nil {
40+
t.Fatal(err)
41+
} else if v.String("") != test.value {
4042
t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path)
4143
}
4244
}

config/reader/json/values.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func newValues(ch *source.ChangeSet) (reader.Values, error) {
3030
return &jsonValues{ch, sj}, nil
3131
}
3232

33-
func (j *jsonValues) Get(path ...string) reader.Value {
34-
return &jsonValue{j.sj.GetPath(path...)}
33+
func (j *jsonValues) Get(path ...string) (reader.Value, error) {
34+
return &jsonValue{j.sj.GetPath(path...)}, nil
3535
}
3636

3737
func (j *jsonValues) Del(path ...string) {

config/reader/json/values_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ func TestValues(t *testing.T) {
3737
t.Fatal(err)
3838
}
3939

40-
err = values.Get(test.path...).Scan(&test.accepter)
40+
v, err := values.Get(test.path...)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
err = v.Scan(&test.accepter)
4145
if err != nil {
4246
t.Fatal(err)
4347
}
@@ -74,7 +78,11 @@ func TestStructArray(t *testing.T) {
7478
t.Fatal(err)
7579
}
7680

77-
err = values.Get().Scan(&test.accepter)
81+
v, err := values.Get()
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
err = v.Scan(&test.accepter)
7886
if err != nil {
7987
t.Fatal(err)
8088
}

config/reader/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Reader interface {
1717
// Values is returned by the reader.
1818
type Values interface {
1919
Bytes() []byte
20-
Get(path ...string) Value
20+
Get(path ...string) (Value, error)
2121
Set(val interface{}, path ...string)
2222
Del(path ...string)
2323
Map() map[string]interface{}

config/source/cli/cli_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func TestCliSourceDefault(t *testing.T) {
5656
)
5757

5858
config.Load(cliSrc)
59-
if fval := config.Get("flag").String("default"); fval != expVal {
59+
if val, err := config.Get("flag"); err != nil {
60+
t.Fatal(err)
61+
} else if fval := val.String("default"); fval != expVal {
6062
t.Fatalf("default flag value not loaded %v != %v", fval, expVal)
6163
}
6264
}

0 commit comments

Comments
 (0)