From 16d8195b08175a3d0e9eb8e62deb2b35b62656bc Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Fri, 15 Jan 2021 01:01:35 -0500 Subject: [PATCH] feat: calling WithDefaults with the wrong type now panics --- option.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/option.go b/option.go index 21b59b7..3247e57 100644 --- a/option.go +++ b/option.go @@ -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 } -- GitLab