Skip to content

Commit 1278baf

Browse files
author
Alex
authored
Fix interpolated bools (#19)
Fix interpolated bools
1 parent f38b63d commit 1278baf

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,12 @@ func TestInvalidConfigFailures(t *testing.T) {
349349
t.Parallel()
350350
valueType := []byte(`
351351
id: xyz
352-
boolean:
352+
boolean: hooli
353353
`)
354354
provider := NewYAMLProviderFromBytes(valueType)
355-
assert.Panics(t, func() { NewYAMLProviderFromBytes([]byte("bytes: \n\x010")) }, "Can't parse empty boolean")
355+
assert.Panics(t, func() { NewYAMLProviderFromBytes([]byte("bytes: \n\x010")) }, "Can't parse invalid YAML")
356356
assert.Panics(t, func() { provider.Get("id").AsInt() }, "Can't parse as int")
357-
assert.Panics(t, func() { provider.Get("boolean").AsBool() }, "Can't parse empty boolean")
357+
assert.Panics(t, func() { provider.Get("boolean").AsBool() }, "Can't parse invalid boolean")
358358
assert.Panics(t, func() { provider.Get("id").AsFloat() }, "Can't parse as float")
359359
}
360360

decoder.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,13 @@ func convertValueFromStruct(src interface{}, dst *reflect.Value) error {
187187
return convertFloats(src, dst)
188188

189189
case reflect.Bool:
190-
if v, err := strconv.ParseBool(fmt.Sprint(src)); err == nil {
191-
dst.SetBool(v)
190+
v, err := strconv.ParseBool(fmt.Sprint(src))
191+
if err != nil {
192+
return err
192193
}
193194

195+
dst.SetBool(v)
196+
194197
case reflect.String:
195198
dst.SetString(fmt.Sprint(src))
196199

static_provider_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
"github.com/stretchr/testify/assert"
2929
"github.com/stretchr/testify/require"
30+
"io/ioutil"
31+
"strings"
3032
)
3133

3234
func TestStaticProvider_Name(t *testing.T) {
@@ -296,6 +298,20 @@ func TestValue_ChildKeys(t *testing.T) {
296298
t.Run("MapOfInts", func(t *testing.T) { op(t, Root, []string{"3", "5"}) })
297299
}
298300

301+
func TestInterpolatedBool(t *testing.T) {
302+
t.Parallel()
303+
304+
f := func(key string) (string, bool) {
305+
if key == "interpolate" {
306+
return "true", true
307+
}
308+
return "", false
309+
}
310+
311+
p := NewYAMLProviderFromReaderWithExpand(f, ioutil.NopCloser(strings.NewReader("val: ${interpolate:false}")))
312+
assert.True(t, p.Get("val").AsBool())
313+
}
314+
299315
func TestConfigDefaults(t *testing.T) {
300316
t.Parallel()
301317

value.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,9 @@ func (cv Value) TryAsInt() (int, bool) {
191191

192192
// TryAsBool attempts to return the configuration value as a bool
193193
func (cv Value) TryAsBool() (bool, bool) {
194-
v := cv.Value()
195-
if val, err := convertValue(v, reflect.TypeOf(true)); v != nil && err == nil {
196-
return val.(bool), true
197-
}
198-
return false, false
194+
var res bool
195+
err := newValueProvider(cv.Value()).Get(Root).Populate(&res)
196+
return res, err == nil
199197
}
200198

201199
// TryAsFloat attempts to return the configuration value as a float

0 commit comments

Comments
 (0)