Skip to content

Commit d9cb443

Browse files
RTannvladbologa
authored andcommitted
chore: allow integer env vars (#1289)
1 parent bed1a7e commit d9cb443

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pkg/env/integersetting.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package env
2+
3+
import (
4+
"strconv"
5+
)
6+
7+
// IntegerSetting represents an environment variable which should be parsed into an integer.
8+
type IntegerSetting interface {
9+
Setting
10+
Int() int
11+
}
12+
13+
type integerSetting struct {
14+
Setting
15+
defaultValue int
16+
}
17+
18+
// Int returns the int object represented by the environment variable.
19+
func (s *integerSetting) Int() int {
20+
v, err := strconv.Atoi(s.Value())
21+
if err != nil {
22+
return s.defaultValue
23+
}
24+
return v
25+
}
26+
27+
// RegisterIntegerSetting globally registers and returns a new integer setting.
28+
func RegisterIntegerSetting(envVar string, defaultValue int, opts ...SettingOption) IntegerSetting {
29+
return &integerSetting{
30+
Setting: RegisterSetting(envVar, append(opts, WithDefault(strconv.Itoa(defaultValue)))...),
31+
defaultValue: defaultValue,
32+
}
33+
}

0 commit comments

Comments
 (0)