-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: allow integer env vars (#1289)
- Loading branch information
1 parent
bed1a7e
commit d9cb443
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package env | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
// IntegerSetting represents an environment variable which should be parsed into an integer. | ||
type IntegerSetting interface { | ||
Setting | ||
Int() int | ||
} | ||
|
||
type integerSetting struct { | ||
Setting | ||
defaultValue int | ||
} | ||
|
||
// Int returns the int object represented by the environment variable. | ||
func (s *integerSetting) Int() int { | ||
v, err := strconv.Atoi(s.Value()) | ||
if err != nil { | ||
return s.defaultValue | ||
} | ||
return v | ||
} | ||
|
||
// RegisterIntegerSetting globally registers and returns a new integer setting. | ||
func RegisterIntegerSetting(envVar string, defaultValue int, opts ...SettingOption) IntegerSetting { | ||
return &integerSetting{ | ||
Setting: RegisterSetting(envVar, append(opts, WithDefault(strconv.Itoa(defaultValue)))...), | ||
defaultValue: defaultValue, | ||
} | ||
} |