Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore prefix computing environment variable defaults #99

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ func (p *prefixLookuper) Key(key string) string {
return p.prefix + key
}

func (p *prefixLookuper) Unwrap() Lookuper {
l := p.l
for v, ok := l.(UnwrappableLookuper); ok; {
l = v.Unwrap()
}
return l
}

// UnwrappableLookuper is a lookuper that can return the underlying lookuper.
type UnwrappableLookuper interface {
Unwrap() Lookuper
}

// MultiLookuper wraps a collection of lookupers. It does not combine them, and
// lookups appear in the order in which they are provided to the initializer.
func MultiLookuper(lookupers ...Lookuper) Lookuper {
Expand Down Expand Up @@ -602,9 +615,14 @@ func lookup(key string, required bool, defaultValue string, l Lookuper) (string,

if defaultValue != "" {
// Expand the default value. This allows for a default value that maps to
// a different variable.
// a different environment variable.
val = os.Expand(defaultValue, func(i string) string {
s, ok := l.Lookup(i)
lookuper := l
if v, ok := lookuper.(UnwrappableLookuper); ok {
lookuper = v.Unwrap()
}

s, ok := lookuper.Lookup(i)
if ok {
return s
}
Expand Down
38 changes: 38 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,44 @@ func TestProcessWith(t *testing.T) {
},
lookuper: MapLookuper(nil),
},
{
name: "default/expand_prefix",
target: &struct {
Field string `env:"FIELD,default=$DEFAULT"`
}{},
exp: &struct {
Field string `env:"FIELD,default=$DEFAULT"`
}{
Field: "value",
},
lookuper: PrefixLookuper("PREFIX_", MapLookuper(map[string]string{
// Ensure that we use the underlying MapLookuper instead of the prefixed
// value when resolving a default:
//
// https://github.com/sethvargo/go-envconfig/issues/85
//
"DEFAULT": "value",
})),
},
{
name: "default/expand_prefix_prefix",
target: &struct {
Field string `env:"FIELD,default=$DEFAULT"`
}{},
exp: &struct {
Field string `env:"FIELD,default=$DEFAULT"`
}{
Field: "value",
},
lookuper: PrefixLookuper("OUTER_", PrefixLookuper("INNER_", MapLookuper(map[string]string{
// Ensure that we use the underlying MapLookuper instead of the prefixed
// value when resolving a default:
//
// https://github.com/sethvargo/go-envconfig/issues/85
//
"DEFAULT": "value",
}))),
},
{
name: "default/slice",
target: &struct {
Expand Down