Skip to content

Commit

Permalink
chore: allow integer env vars (#1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
RTann authored and vladbologa committed Oct 13, 2023
1 parent bed1a7e commit d9cb443
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/env/integersetting.go
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,
}
}

0 comments on commit d9cb443

Please sign in to comment.