diff --git a/config_test.go b/config_test.go index 2a8258d..1591c7a 100644 --- a/config_test.go +++ b/config_test.go @@ -134,6 +134,13 @@ type testCfg struct { Uint8Field uint8 } } + SecondNestedStruct struct { + StringField string + StructField struct { + StringField string + } + SecondStringField string + } } func (p *pFull) Provide(config interface{}) error { @@ -156,6 +163,21 @@ func (p *pFull) Provide(config interface{}) error { NestedInt16: 321, }, }, + SecondNestedStruct: struct { + StringField string + StructField struct { + StringField string + } + SecondStringField string + }{ + StringField: "nested-123", + StructField: struct { + StringField string + }{ + StringField: "inner-nested", + }, + SecondStringField: "nested-123", + }, } parse(config, &cfg) diff --git a/env.go b/env.go index c00f9cb..5e6fa72 100644 --- a/env.go +++ b/env.go @@ -52,7 +52,7 @@ func provide(prefix string, config reflect.Value) error { } func parseValue(prefix string, vField reflect.Value, tField reflect.StructField) error { - if prefix != "" { + if prefix != "" && !strings.HasSuffix(prefix, "_") { prefix = strings.ToUpper(prefix) + "_" } diff --git a/env_test.go b/env_test.go index 1e0cb1f..9689df7 100644 --- a/env_test.go +++ b/env_test.go @@ -69,6 +69,15 @@ func TestEnvConfigWithoutPrefix(t *testing.T) { if cfg.NestedStruct.AnotherLevel.Uint8Field != uint8Filed { t.Errorf("Value is '%d', but %d expected", cfg.NestedStruct.AnotherLevel.Uint8Field, uint8Filed) } + if cfg.SecondNestedStruct.StringField != nestedString { + t.Errorf("Value is '%s', but %q expected", cfg.SecondNestedStruct.StringField, nestedString) + } + if cfg.SecondNestedStruct.SecondStringField != nestedString { + t.Errorf("Value is '%s', but %q expected", cfg.SecondNestedStruct.SecondStringField, nestedString) + } + if cfg.SecondNestedStruct.StructField.StringField != nestedString { + t.Errorf("Value is '%s', but %q expected", cfg.SecondNestedStruct.StructField.StringField, nestedString) + } } func TestEnvConfigEmptyString(t *testing.T) { @@ -158,4 +167,7 @@ func setUpEnv(prefix string) { _ = os.Setenv(p+"NESTEDSTRUCT_FLOAT32FIELD", fmt.Sprintf("%f", float32Field)) _ = os.Setenv(p+"NESTEDSTRUCT_ANOTHERLEVEL_NESTEDINT16", strconv.Itoa(nestedInt16)) _ = os.Setenv(p+"NESTEDSTRUCT_ANOTHERLEVEL_UINT8FIELD", strconv.Itoa(uint8Filed)) + _ = os.Setenv(p+"SECONDNESTEDSTRUCT_STRINGFIELD", nestedString) + _ = os.Setenv(p+"SECONDNESTEDSTRUCT_STRUCTFIELD_STRINGFIELD", nestedString) + _ = os.Setenv(p+"SECONDNESTEDSTRUCT_SECONDSTRINGFIELD", nestedString) }