Skip to content

Commit

Permalink
fix env overwriting of int and bool variables (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Groschupp <[email protected]>
  • Loading branch information
cgroschupp and Christian Groschupp authored Oct 10, 2020
1 parent 270edb5 commit 863dc6b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pkg/config/initialize_from_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"reflect"
"strconv"

"github.com/pkg/errors"

Expand Down Expand Up @@ -112,5 +113,30 @@ func reflectSetValueFromConfigKey(viperConfigKey string, fieldToBeSet reflect.Va

fieldLogger.Debug("loading viper value into struct")

fieldToBeSet.Set(reflectedVal)
switch fieldToBeSet.Kind() {
case reflect.Int:
switch viperVal.(type) {
case int:
fieldToBeSet.Set(reflectedVal)
default:
if v, err := strconv.ParseInt(viperVal.(string), 10, 64); err == nil {
fieldToBeSet.SetInt(v)
} else {
fieldLogger.WithError(err).Errorf("unable to parse int: %v", viperVal)
}
}
case reflect.Bool:
switch viperVal.(type) {
case bool:
fieldToBeSet.Set(reflectedVal)
default:
if v, err := strconv.ParseBool(viperVal.(string)); err == nil {
fieldToBeSet.SetBool(v)
} else {
fieldLogger.WithError(err).Errorf("unable to parse bool: %v", viperVal)
}
}
default:
fieldToBeSet.Set(reflectedVal)
}
}
38 changes: 38 additions & 0 deletions pkg/config/initialize_from_viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"os"
"strings"
"testing"

Expand All @@ -28,6 +29,7 @@ foo:
example: true
newExample: false
brudiTest: "foobar"
brudiNumber: 2222
`)

func (initializeFromViperTestSuite *InitializeFromViperTestSuite) TestInitializeStructFromViperWithoutTags() {
Expand All @@ -39,6 +41,7 @@ func (initializeFromViperTestSuite *InitializeFromViperTestSuite) TestInitialize
Example: true,
AnotherExample: false,
BrudiTest: "foobar",
BrudiNumber: 2222,
},
}

Expand All @@ -64,6 +67,41 @@ func (initializeFromViperTestSuite *InitializeFromViperTestSuite) TestInitialize
CustomExample: true,
CustomAnotherExample: false,
CustomBrudiTest: "foobar",
CustomBrudiNumber: 2222,
},
}

assert.NoError(
initializeFromViperTestSuite.T(),
InitializeStructFromViper(
"foo",
&isConfig,
),
)

assert.Equal(initializeFromViperTestSuite.T(), shouldBeConfig, isConfig)
}

func (initializeFromViperTestSuite *InitializeFromViperTestSuite) TestInitializeStructFromViperOverwriteEnv() {
assert.NoError(initializeFromViperTestSuite.T(), viper.ReadConfig(bytes.NewBuffer(exampleConfig)))

os.Setenv("FOO_BAR_BRUDINUMBER", "2223")
os.Setenv("FOO_BAR_BRUDITEST", "foobar1")
os.Setenv("FOO_BAR_ANOTHEREXAMPLE", "true")

defer os.Unsetenv("FOO_BAR_BRUDITEST")
defer os.Unsetenv("FOO_BAR_BRUDINUMBER")
defer os.Unsetenv("FOO_BAR_ANOTHEREXAMPLE")

isConfig := taggedFooConfig{
CustomBar: &taggedBarConfig{},
}
shouldBeConfig := taggedFooConfig{
CustomBar: &taggedBarConfig{
CustomExample: true,
CustomAnotherExample: true,
CustomBrudiTest: "foobar1",
CustomBrudiNumber: 2223,
},
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type untaggedBarConfig struct {
Example bool
AnotherExample bool
BrudiTest string `validate:"min=1"`
BrudiNumber int
}

type taggedFooConfig struct {
Expand All @@ -28,6 +29,7 @@ type taggedBarConfig struct {
CustomExample bool `viper:"example"`
CustomAnotherExample bool `viper:"anotherExample"`
CustomBrudiTest string `viper:"brudiTest" validate:"min=1"`
CustomBrudiNumber int `viper:"brudiNumber"`
}

func fooConfigValidation(sl validator.StructLevel) {
Expand Down

0 comments on commit 863dc6b

Please sign in to comment.