@@ -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
0 commit comments