Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
fix: panic on invalid priority/flag values
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Jun 16, 2020
1 parent 7fbc0df commit dce3097
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const (
)

// WithDefault resolves the value of the flag given the provided default value.
//
// Panics if Flag is an invalid value.
func (f Flag) WithDefault(defaultValue bool) bool {
switch f {
case False:
Expand Down Expand Up @@ -126,17 +128,30 @@ const (

// WithDefault resolves the priority with the given default.
//
// If `defaultPriority` is Default/0, this function will return 0.
// If defaultPriority is Default/0, this function will return 0.
//
// Panics if the priority has an invalid value (e.g., not DefaultPriority,
// Disabled, or > 0).
func (p Priority) WithDefault(defaultPriority Priority) (priority int64, enabled bool) {
switch p {
case Disabled:
return 0, false
case DefaultPriority:
if defaultPriority < 0 {
switch defaultPriority {
case Disabled:
return 0, false
case DefaultPriority:
return 0, true
default:
if defaultPriority <= 0 {
panic(fmt.Sprintf("invalid priority %d < 0", int64(defaultPriority)))
}
return 0, true
}
return int64(defaultPriority), true
default:
if p <= 0 {
panic(fmt.Sprintf("invalid priority %d < 0", int64(p)))
}
return int64(p), true
}
}
Expand Down

0 comments on commit dce3097

Please sign in to comment.