Skip to content

Commit

Permalink
Merge branch 'feature/support-embedded-structure-without-inline-optio…
Browse files Browse the repository at this point in the history
…n' into 'master'

Feature/support embedded structure without inline option

See merge request backend/envconf!6
  • Loading branch information
komod committed Jul 30, 2020
2 parents 0da0f55 + 435841c commit d563962
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 0 additions & 3 deletions envconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ func loadStruct(prefix string, out *reflect.Value) {
}
name = prefix
} else {
if field.Anonymous {
continue
}
name = nameAndOpts[0]
if name == "" {
name = field.Name
Expand Down
23 changes: 22 additions & 1 deletion envconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func TestInlineWithoutStruct(t *testing.T) {
Load("TEST", &config)
}

func TestEmbeddedStructureWithoutInline(t *testing.T) {
os.Setenv("TEST_EMBEDDED_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_EMBEDDED_INT_IN_EMBEDDED_STRUCTURE", "19")

configWithTag := struct {
EmbeddedConfig `env:"embedded"`
}{}
Load("TEST", &configWithTag)
assertEqual(t, "StringInEmbeddedStructure", "a-z", configWithTag.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 19, configWithTag.IntInEmbeddedStructure)
}

func TestIgnoringSpecificTag(t *testing.T) {
// invalid
os.Setenv("TEST_-", "panics")
Expand All @@ -194,6 +206,8 @@ func TestIgnoringSpecificTag(t *testing.T) {
os.Setenv("TEST_IGNOREDINT", "123")
os.Setenv("TEST_IGNOREDSTRUCT_IGNOREDSTRINGINSTRUCT", "ignored")
os.Setenv("TEST_IGNOREDSTRUCT_IGNOREDINTINSTRUCT", "12345")
os.Setenv("TEST_EMBEDDEDCONFIG_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_EMBEDDEDCONFIG_INT_IN_EMBEDDED_STRUCTURE", "19")

config := struct {
IgnoredString string `env:"-"`
Expand All @@ -202,21 +216,25 @@ func TestIgnoringSpecificTag(t *testing.T) {
IgnoredStringInStruct string
IgnoredIntInStruct int
} `env:"-"`
EmbeddedConfig `env:"-"`
}{}
Load("TEST", &config)

assertEqual(t, "IgnoredString", "", config.IgnoredString)
assertEqual(t, "IgnoredInt", 0, config.IgnoredInt)
assertEqual(t, "IgnoredStringInStruct", "", config.IgnoredStruct.IgnoredStringInStruct)
assertEqual(t, "IgnoredIntInStruct", 0, config.IgnoredStruct.IgnoredIntInStruct)
assertEqual(t, "StringInEmbeddedStructure", "", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 0, config.IntInEmbeddedStructure)
}

func TestNoTag(t *testing.T) {
// auto named
os.Setenv("TEST_AUTONAMEDSTRING", "auto named")
os.Setenv("TEST_AUTONAMEDINT", "321")
os.Setenv("TEST_AUTONAMEDSTRUCT_AUTONAMEDSTRINGINSTRUCT", "auto named in struct")
os.Setenv("TEST_AUTONAMEDSTRUCT_AUTONAMEDINTINSTRUCT", "54321")
os.Setenv("TEST_EMBEDDEDCONFIG_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_EMBEDDEDCONFIG_INT_IN_EMBEDDED_STRUCTURE", "19")

config := struct {
AutoNamedString string
Expand All @@ -225,11 +243,14 @@ func TestNoTag(t *testing.T) {
AutoNamedStringInStruct string
AutoNamedIntInStruct int
}
EmbeddedConfig
}{}
Load("TEST", &config)

assertEqual(t, "AutoNamedString", "auto named", config.AutoNamedString)
assertEqual(t, "AutoNamedInt", 321, config.AutoNamedInt)
assertEqual(t, "AutoNamedStringInStruct", "auto named in struct", config.AutoNamedStruct.AutoNamedStringInStruct)
assertEqual(t, "AutoNamedIntInStruct", 54321, config.AutoNamedStruct.AutoNamedIntInStruct)
assertEqual(t, "StringInEmbeddedStructure", "a-z", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 19, config.IntInEmbeddedStructure)
}

0 comments on commit d563962

Please sign in to comment.