Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
16d8195b
Commit
16d8195b
authored
Jan 15, 2021
by
Adin Schmahmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: calling WithDefaults with the wrong type now panics
parent
d48b43da
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
0 deletions
+12
-0
option.go
option.go
+12
-0
No files found.
option.go
View file @
16d8195b
...
...
@@ -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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment