Skip to content

Commit

Permalink
Add SetOverride()
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Feb 11, 2019
1 parent 82b5348 commit ac717b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ holster.IsZero(value)
// Returns true if 'value' is zero (the default golang value)
holster.IsZeroValue(reflect.ValueOf(value))

// If 'value' is empty or of zero value, assign the default value.
// If 'dest' is empty or of zero value, assign the default value.
// This panics if the value is not a pointer or if value and
// default value are not of the same type.
var config struct {
Expand All @@ -273,7 +273,16 @@ 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")
holster.SetDefault(&config.Foo, os.Getenv("FOO"), "default")

// Use 'SetOverride() to assign the first value that is not empty or of zero
// value. The following will override the config file if 'foo' is provided via
// the cli or defined in the environment.

loadFromFile(&config)
argFoo = flag.String("foo", "", "foo via cli arg")

holster.SetOverride(&config.Foo, *argFoo, os.Env("FOO"))
```

## GetEnv
Expand Down
39 changes: 37 additions & 2 deletions set_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"reflect"
)

// If 'value' is empty or of zero value, assign the default value.
// If 'dest' is empty or of zero value, assign the default value.
// This panics if the value is not a pointer or if value and
// default value are not of the same type.
// var config struct {
// var config struct {
// Verbose *bool
// Foo string
// Bar int
Expand Down Expand Up @@ -51,6 +51,41 @@ func SetDefault(dest interface{}, defaultValue ...interface{}) {
}
}

// Assign the first value that is not empty or of zero value.
// This panics if the value is not a pointer or if value and
// default value are not of the same type.
// var config struct {
// Verbose *bool
// Foo string
// Bar int
// }
//
// loadFromFile(&config)
// argFoo = flag.String("foo", "", "foo via cli arg")
//
// // Override the config file if 'foo' is provided via
// // the cli or defined in the environment.
// holster.SetOverride(&config.Foo, *argFoo, os.Env("FOO"))
//
// Supply additional values and SetOverride() will
// choose the first value that is not of zero value. If all
// values are empty or zero the 'dest' will remain unchanged.
func SetOverride(dest interface{}, values ...interface{}) {
d := reflect.ValueOf(dest)
if d.Kind() != reflect.Ptr {
panic("holster.SetOverride: Expected first argument to be of type reflect.Ptr")
}
d = reflect.Indirect(d)
// Use the first non zero value value we find
for _, value := range values {
v := reflect.ValueOf(value)
if !IsZeroValue(v) {
d.Set(reflect.ValueOf(value))
return
}
}
}

// Returns true if 'value' is zero (the default golang value)
// var thingy string
// holster.IsZero(thingy) == true
Expand Down

0 comments on commit ac717b9

Please sign in to comment.