Skip to content

Commit

Permalink
feat: calling WithDefaults with the wrong type now panics
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Jan 15, 2021
1 parent d48b43d commit 16d8195
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ func NewOption(kind reflect.Kind, names ...string) Option {
}

func (o *option) WithDefault(v interface{}) Option {
if v == nil {
panic(fmt.Errorf("cannot use nil as a default"))
}

// if type of value does not match the option type
if vKind, oKind := reflect.TypeOf(v).Kind(), o.Type(); vKind != oKind {
// if the reason they do not match is not because of Slice vs Array equivalence
// Note: Figuring out if the type of Slice/Array matches is not done in this function
if !((vKind == reflect.Array || vKind == reflect.Slice) && (oKind == reflect.Array || oKind == reflect.Slice)) {
panic(fmt.Errorf("invalid default for the given type, expected %s got %s", o.Type(), vKind))
}
}
o.defaultVal = v
return o
}
Expand Down

0 comments on commit 16d8195

Please sign in to comment.