File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed
Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -149,6 +149,18 @@ func NewOption(kind reflect.Kind, names ...string) Option {
149149}
150150
151151func (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}
You can’t perform that action at this time.
0 commit comments