Skip to content

Commit

Permalink
Merge pull request lindenlab#1 from lindenlab/parse-into-base-structs
Browse files Browse the repository at this point in the history
Parse Into Base Structs
  • Loading branch information
ZedLinden authored May 3, 2019
2 parents 540dbfc + 415c1a4 commit 9eb8dd6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.0
0.4.0
5 changes: 5 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func doParse(ref reflect.Value, prefix string, funcMap CustomParsers) error {
continue
}
if value == "" {
if reflect.Struct == refField.Kind() {
if err := doParse(refField, prefix, funcMap); err != nil {
errorList = append(errorList, err.Error())
}
}
continue
}
if err := set(refField, refTypeField, value, funcMap); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ type InnerStruct struct {
Number uint `env:"innernum"`
}

type DerivedStruct struct {
BaseStruct
}

type BaseStruct struct {
Inner string `env:"innervar"`
Number uint `env:"innernum"`
}

func TestParsesEnv(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
Expand Down Expand Up @@ -169,6 +178,21 @@ func TestParsesEnvInnerInvalid(t *testing.T) {
assert.Error(t, Parse(&cfg))
}

func TestParsesEnvDerived(t *testing.T) {
os.Setenv("innervar", "someinnervalue")
defer os.Clearenv()
cfg := DerivedStruct{}
assert.NoError(t, Parse(&cfg))
assert.Equal(t, "someinnervalue", cfg.Inner)
}

func TestParsesEnvDerivedInvalid(t *testing.T) {
os.Setenv("innernum", "-547")
defer os.Clearenv()
cfg := DerivedStruct{}
assert.Error(t, Parse(&cfg))
}

func TestEmptyVars(t *testing.T) {
cfg := Config{}
assert.NoError(t, Parse(&cfg))
Expand Down

0 comments on commit 9eb8dd6

Please sign in to comment.