Skip to content

Commit

Permalink
added variadic defaults to SetDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Jul 3, 2018
1 parent f0b7012 commit dbf7980
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ var config struct {
}
holster.SetDefault(&config.Foo, "default")
holster.SetDefault(&config.Bar, 200)

// Supply additional default values and SetDefault will
// choose the first default that is not of zero value
// holster.SetDefault(&config.Foo, os.Getenv("FOO"), "default")
```

## GetEnv
Expand Down
29 changes: 21 additions & 8 deletions set_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ limitations under the License.
*/
package holster

import "reflect"
import (
"reflect"
)

// If 'value' is empty or of zero value, assign the default value.
// This panics if the value is not a pointer or if value and
Expand All @@ -27,14 +29,25 @@ import "reflect"
// }
// holster.SetDefault(&config.Foo, "default")
// holster.SetDefault(&config.Bar, 200)
func SetDefault(value, defaultValue interface{}) {
v := reflect.ValueOf(value)
if v.Kind() != reflect.Ptr {
panic("holster.IfEmpty: Expected first argument to be of type reflect.Ptr")
//
// Supply additional default values and SetDefault will
// choose the first default that is not of zero value
// holster.SetDefault(&config.Foo, os.Getenv("FOO"), "default")
func SetDefault(dest interface{}, defaultValue ...interface{}) {
d := reflect.ValueOf(dest)
if d.Kind() != reflect.Ptr {
panic("holster.SetDefault: Expected first argument to be of type reflect.Ptr")
}
v = reflect.Indirect(v)
if IsZeroValue(v) {
v.Set(reflect.ValueOf(defaultValue))
d = reflect.Indirect(d)
if IsZeroValue(d) {
// Use the first non zero default value we find
for _, value := range defaultValue {
v := reflect.ValueOf(value)
if !IsZeroValue(v) {
d.Set(reflect.ValueOf(value))
return
}
}
}
}

Expand Down
21 changes: 20 additions & 1 deletion set_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ func (s *SetDefaultTestSuite) TestIfEmpty(c *C) {
c.Assert(conf.Bar, Equals, 500)
}

func (s *SetDefaultTestSuite) TestIfDefaultPrecedence(c *C) {
var conf struct {
Foo string
Bar string
}
c.Assert(conf.Foo, Equals, "")
c.Assert(conf.Bar, Equals, "")

// Should use the final default value
envValue := ""
holster.SetDefault(&conf.Foo, envValue, "default")
c.Assert(conf.Foo, Equals, "default")

// Should use envValue
envValue = "bar"
holster.SetDefault(&conf.Bar, envValue, "default")
c.Assert(conf.Bar, Equals, "bar")
}

func (s *SetDefaultTestSuite) TestIsEmpty(c *C) {
var count64 int64
var thing string
Expand Down Expand Up @@ -83,7 +102,7 @@ func (s *SetDefaultTestSuite) TestIfEmptyTypePanic(c *C) {
func (s *SetDefaultTestSuite) TestIfEmptyNonPtrPanic(c *C) {
defer func() {
if r := recover(); r != nil {
c.Assert(r, Equals, "holster.IfEmpty: Expected first argument to be of type reflect.Ptr")
c.Assert(r, Equals, "holster.SetDefault: Expected first argument to be of type reflect.Ptr")
}
}()

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0
1.7.1

0 comments on commit dbf7980

Please sign in to comment.