Skip to content

Commit c013a3b

Browse files
committed
Completed fix of issue #148
with respecting of env-prefix and partially saved backward compatibility by using new tag no_pfx
1 parent 5da5812 commit c013a3b

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

envconfig.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,30 @@ func gatherInfo(prefix string, spec interface{}) ([]varInfo, error) {
121121
if prefix != "" {
122122
info.Key = fmt.Sprintf("%s_%s", prefix, info.Key)
123123
}
124-
// If name is overridden, switch .Key and .Alt and don't forget about prefix.
125-
// Now the overridden name is for the first attempt to find value in environment.
126-
// The initial name is an alternative name used if the first attempt is not successful.
124+
125+
// override default env-variable name with an envconfig tag
127126
if info.Alt != "" {
128127
if prefix != "" {
129-
info.Alt = fmt.Sprintf("%s_%s", prefix, info.Alt)
128+
noPfxPlace := ftype.Tag.Get("no_pfx")
129+
if noPfxPlace == "key" {
130+
// now the key name is overridden and prefixed
131+
// alt name is not used
132+
info.Key = info.Alt
133+
info.Alt = ""
134+
} else if noPfxPlace == "alt" {
135+
// now the key name is overridden and prefixed
136+
// and alt name is overridden name without prefix
137+
info.Key = fmt.Sprintf("%s_%s", prefix, info.Alt)
138+
} else {
139+
// now key name is overridden and prefixed
140+
// but alt is the prev key name (source struct field name) with prefix
141+
prefixedStructFieldName := info.Key
142+
info.Key = fmt.Sprintf("%s_%s", prefix, info.Alt)
143+
info.Alt = prefixedStructFieldName
144+
}
145+
} else {
146+
130147
}
131-
info.Key, info.Alt = info.Alt, info.Key
132148
}
133149
info.Key = strings.ToUpper(info.Key)
134150
info.Alt = strings.ToUpper(info.Alt)
@@ -211,9 +227,6 @@ func Process(prefix string, spec interface{}) error {
211227
if !ok && def == "" {
212228
if isTrue(req) {
213229
key := info.Key
214-
if info.Alt != "" {
215-
key = info.Alt
216-
}
217230
return fmt.Errorf("required key %s missing value", key)
218231
}
219232
continue

envconfig_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ type Specification struct {
5454
SomePointerWithDefault *string `default:"foo2baz" desc:"foorbar is the word"`
5555
MultiWordVarWithAlt string `envconfig:"MULTI_WORD_VAR_WITH_ALT" desc:"what alt"`
5656
MultiWordVarWithLowerCaseAlt string `envconfig:"multi_word_var_with_lower_case_alt"`
57-
NoPrefixWithAlt string `envconfig:"SERVICE_HOST"`
57+
NoPrefixWithAlt string `envconfig:"SERVICE_HOST" no_pfx:"alt"`
5858
DefaultVar string `default:"foobar"`
5959
RequiredVar string `required:"True"`
60-
NoPrefixDefault string `envconfig:"BROKER" default:"127.0.0.1"`
60+
NoPrefixDefault string `envconfig:"BROKER" default:"127.0.0.1" no_pfx:"alt"`
6161
RequiredDefault string `required:"true" default:"foo2bar"`
6262
Ignored string `ignored:"true"`
6363
NestedSpecification struct {
64-
Property string `envconfig:"inner"`
64+
Property string `envconfig:"inner" no_pfx:"alt"`
6565
PropertyWithDefault string `default:"fuzzybydefault"`
6666
} `envconfig:"outer"`
6767
AfterNested string
@@ -804,6 +804,23 @@ func TestErrorMessageForRequiredAltVar(t *testing.T) {
804804
t.Error("no failure when missing required variable")
805805
}
806806

807+
if !strings.Contains(err.Error(), " ENV_CONFIG_BAR ") {
808+
t.Errorf("expected error message to contain BAR, got \"%v\"", err)
809+
}
810+
}
811+
812+
func TestErrorMessageForRequiredOverriddenKeyVar(t *testing.T) {
813+
var s struct {
814+
Foo string `envconfig:"BAR" required:"true" no_pfx:"key"`
815+
}
816+
817+
os.Clearenv()
818+
err := Process("env_config", &s)
819+
820+
if err == nil {
821+
t.Error("no failure when missing required variable")
822+
}
823+
807824
if !strings.Contains(err.Error(), " BAR ") {
808825
t.Errorf("expected error message to contain BAR, got \"%v\"", err)
809826
}

0 commit comments

Comments
 (0)