Skip to content

Commit

Permalink
Fix interpolated bools (#19)
Browse files Browse the repository at this point in the history
Fix interpolated bools
  • Loading branch information
Alex authored Jun 12, 2017
1 parent f38b63d commit 1278baf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 3 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ func TestInvalidConfigFailures(t *testing.T) {
t.Parallel()
valueType := []byte(`
id: xyz
boolean:
boolean: hooli
`)
provider := NewYAMLProviderFromBytes(valueType)
assert.Panics(t, func() { NewYAMLProviderFromBytes([]byte("bytes: \n\x010")) }, "Can't parse empty boolean")
assert.Panics(t, func() { NewYAMLProviderFromBytes([]byte("bytes: \n\x010")) }, "Can't parse invalid YAML")
assert.Panics(t, func() { provider.Get("id").AsInt() }, "Can't parse as int")
assert.Panics(t, func() { provider.Get("boolean").AsBool() }, "Can't parse empty boolean")
assert.Panics(t, func() { provider.Get("boolean").AsBool() }, "Can't parse invalid boolean")
assert.Panics(t, func() { provider.Get("id").AsFloat() }, "Can't parse as float")
}

Expand Down
7 changes: 5 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ func convertValueFromStruct(src interface{}, dst *reflect.Value) error {
return convertFloats(src, dst)

case reflect.Bool:
if v, err := strconv.ParseBool(fmt.Sprint(src)); err == nil {
dst.SetBool(v)
v, err := strconv.ParseBool(fmt.Sprint(src))
if err != nil {
return err
}

dst.SetBool(v)

case reflect.String:
dst.SetString(fmt.Sprint(src))

Expand Down
16 changes: 16 additions & 0 deletions static_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"strings"
)

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

func TestInterpolatedBool(t *testing.T) {
t.Parallel()

f := func(key string) (string, bool) {
if key == "interpolate" {
return "true", true
}
return "", false
}

p := NewYAMLProviderFromReaderWithExpand(f, ioutil.NopCloser(strings.NewReader("val: ${interpolate:false}")))
assert.True(t, p.Get("val").AsBool())
}

func TestConfigDefaults(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 3 additions & 5 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,9 @@ func (cv Value) TryAsInt() (int, bool) {

// TryAsBool attempts to return the configuration value as a bool
func (cv Value) TryAsBool() (bool, bool) {
v := cv.Value()
if val, err := convertValue(v, reflect.TypeOf(true)); v != nil && err == nil {
return val.(bool), true
}
return false, false
var res bool
err := newValueProvider(cv.Value()).Get(Root).Populate(&res)
return res, err == nil
}

// TryAsFloat attempts to return the configuration value as a float
Expand Down

0 comments on commit 1278baf

Please sign in to comment.