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 {
149
149
}
150
150
151
151
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
+ }
152
164
o .defaultVal = v
153
165
return o
154
166
}
You can’t perform that action at this time.
0 commit comments