Skip to content

Commit 16d8195

Browse files
committed
feat: calling WithDefaults with the wrong type now panics
1 parent d48b43d commit 16d8195

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

option.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ func NewOption(kind reflect.Kind, names ...string) Option {
149149
}
150150

151151
func (o *option) WithDefault(v interface{}) Option {
152+
if v == nil {
153+
panic(fmt.Errorf("cannot use nil as a default"))
154+
}
155+
156+
// if type of value does not match the option type
157+
if vKind, oKind := reflect.TypeOf(v).Kind(), o.Type(); vKind != oKind {
158+
// if the reason they do not match is not because of Slice vs Array equivalence
159+
// Note: Figuring out if the type of Slice/Array matches is not done in this function
160+
if !((vKind == reflect.Array || vKind == reflect.Slice) && (oKind == reflect.Array || oKind == reflect.Slice)) {
161+
panic(fmt.Errorf("invalid default for the given type, expected %s got %s", o.Type(), vKind))
162+
}
163+
}
152164
o.defaultVal = v
153165
return o
154166
}

0 commit comments

Comments
 (0)