Skip to content

Commit

Permalink
Merge pull request lindenlab#3 from lindenlab/fix-recursive-parse-wit…
Browse files Browse the repository at this point in the history
…h-prefix

Fix Recursive Parse With Prefix
  • Loading branch information
ZedLinden authored Sep 12, 2019
2 parents 7e26545 + 2f439e5 commit 287b20d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.1
0.4.2
2 changes: 1 addition & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func doParse(ref reflect.Value, prefix string, funcMap CustomParsers) error {
for i := 0; i < refType.NumField(); i++ {
refField := ref.Field(i)
if reflect.Ptr == refField.Kind() && !refField.IsNil() && refField.CanSet() {
err := Parse(refField.Interface())
err := ParseWithPrefixFuncs(refField.Interface(), prefix, funcMap)
if nil != err {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ func TestParsesEnvInner(t *testing.T) {
assert.Equal(t, "someinnervalue", cfg.InnerStruct.Inner)
}

func TestParsesEnvInnerWithPrefix(t *testing.T) {
os.Setenv("test_innervar", "someinnervalue")
defer os.Clearenv()
cfg := ParentStruct{
InnerStruct: &InnerStruct{},
unexported: &InnerStruct{},
}
assert.NoError(t, ParseWithPrefix(&cfg, "test_"))
assert.Equal(t, "someinnervalue", cfg.InnerStruct.Inner)
}

func TestParsesEnvInnerNil(t *testing.T) {
os.Setenv("innervar", "someinnervalue")
defer os.Clearenv()
Expand All @@ -186,6 +197,14 @@ func TestParsesEnvDerived(t *testing.T) {
assert.Equal(t, "someinnervalue", cfg.Inner)
}

func TestParsesEnvDerivedWithPrefix(t *testing.T) {
os.Setenv("test_innervar", "someinnervalue")
defer os.Clearenv()
cfg := DerivedStruct{}
assert.NoError(t, ParseWithPrefix(&cfg, "test_"))
assert.Equal(t, "someinnervalue", cfg.Inner)
}

func TestParsesEnvDerivedInvalid(t *testing.T) {
os.Setenv("innernum", "-547")
defer os.Clearenv()
Expand Down

0 comments on commit 287b20d

Please sign in to comment.