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

Add option in Config to not remove prefix when expanding default values which resolve to other environment variables #103

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Package envconfig populates struct fields based on environment variable
// values (or anything that responds to "Lookup"). Structs declare their
// environment dependencies using the "env" tag with the key being the name of
// the environment variable, case sensitive.
// the environment variable, case-sensitive.
//
// type MyStruct struct {
// A string `env:"A"` // resolves A to $A
Expand Down Expand Up @@ -264,8 +264,13 @@ type Config struct {
// default value is false.
DefaultRequired bool

// Mutators is an optiona list of mutators to apply to lookups.
// Mutators is an optional list of mutators to apply to lookups.
Mutators []Mutator

// KeepPrefixExpansion specifies if prefixes are removed when expanding default
// values that reference other environment variables. Set to true to keep any
// prefix.
KeepPrefixExpansion bool
}

// Process decodes the struct using values from environment variables. See
Expand Down Expand Up @@ -336,6 +341,8 @@ func processWith(ctx context.Context, c *Config) error {
structDecodeUnset := c.DefaultDecodeUnset
structRequired := c.DefaultRequired

unwrapOnExpand := !c.KeepPrefixExpansion

mutators := c.Mutators

for i := 0; i < t.NumField(); i++ {
Expand Down Expand Up @@ -428,7 +435,7 @@ func processWith(ctx context.Context, c *Config) error {
// Lookup the value, ignoring an error if the key isn't defined. This is
// required for nested structs that don't declare their own `env` keys,
// but have internal fields with an `env` defined.
val, found, usedDefault, err := lookup(key, required, opts.Default, l)
val, found, usedDefault, err := lookup(key, required, opts.Default, unwrapOnExpand, l)
if err != nil && !errors.Is(err, ErrMissingKey) {
return fmt.Errorf("%s: %w", tf.Name, err)
}
Expand Down Expand Up @@ -483,7 +490,7 @@ func processWith(ctx context.Context, c *Config) error {
continue
}

val, found, usedDefault, err := lookup(key, required, opts.Default, l)
val, found, usedDefault, err := lookup(key, required, opts.Default, unwrapOnExpand, l)
if err != nil {
return fmt.Errorf("%s: %w", tf.Name, err)
}
Expand Down Expand Up @@ -591,7 +598,7 @@ LOOP:
// first boolean parameter indicates whether the value was found in the
// lookuper. The second boolean parameter indicates whether the default value
// was used.
func lookup(key string, required bool, defaultValue string, l Lookuper) (string, bool, bool, error) {
func lookup(key string, required bool, defaultValue string, unwrapOnExpand bool, l Lookuper) (string, bool, bool, error) {
if key == "" {
// The struct has something like `env:",required"`, which is likely a
// mistake. We could try to infer the envvar from the field name, but that
Expand Down Expand Up @@ -620,7 +627,7 @@ func lookup(key string, required bool, defaultValue string, l Lookuper) (string,
// a different environment variable.
val = os.Expand(defaultValue, func(i string) string {
lookuper := l
if v, ok := lookuper.(unwrappableLookuper); ok {
if v, ok := lookuper.(unwrappableLookuper); ok && unwrapOnExpand {
lookuper = v.Unwrap()
}

Expand Down Expand Up @@ -839,13 +846,13 @@ func validateEnvName(s string) bool {
}

// isLetter returns true if the given rune is a letter between a-z,A-Z. This is
// different than unicode.IsLetter which includes all L character cases.
// different from unicode.IsLetter which includes all L character cases.
func isLetter(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
}

// isNumber returns true if the given run is a number between 0-9. This is
// different than unicode.IsNumber in that it only allows 0-9.
// different from unicode.IsNumber in that it only allows 0-9.
func isNumber(r rune) bool {
return r >= '0' && r <= '9'
}
76 changes: 54 additions & 22 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,20 @@ func TestProcessWith(t *testing.T) {
t.Parallel()

cases := []struct {
name string
target any
lookuper Lookuper
defDelimiter string
defSeparator string
defNoInit bool
defOverwrite bool
defDecodeUnset bool
defRequired bool
mutators []Mutator
exp any
err error
errMsg string
name string
target any
lookuper Lookuper
defDelimiter string
defSeparator string
defNoInit bool
defOverwrite bool
defDecodeUnset bool
defRequired bool
keepPrefixExpand bool
mutators []Mutator
exp any
err error
errMsg string
}{
// nil pointer
{
Expand Down Expand Up @@ -1311,6 +1312,36 @@ func TestProcessWith(t *testing.T) {
"DEFAULT": "value",
}))),
},
{
name: "default/expand_keep_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{
"PREFIX_DEFAULT": "value",
})),
keepPrefixExpand: true,
},
{
name: "default/expand_keep_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{
"INNER_OUTER_DEFAULT": "value",
}))),
keepPrefixExpand: true,
},
{
name: "default/slice",
target: &struct {
Expand Down Expand Up @@ -2873,15 +2904,16 @@ func TestProcessWith(t *testing.T) {

ctx := context.Background()
if err := ProcessWith(ctx, &Config{
Target: tc.target,
Lookuper: tc.lookuper,
DefaultDelimiter: tc.defDelimiter,
DefaultSeparator: tc.defSeparator,
DefaultNoInit: tc.defNoInit,
DefaultOverwrite: tc.defOverwrite,
DefaultDecodeUnset: tc.defDecodeUnset,
DefaultRequired: tc.defRequired,
Mutators: tc.mutators,
Target: tc.target,
Lookuper: tc.lookuper,
DefaultDelimiter: tc.defDelimiter,
DefaultSeparator: tc.defSeparator,
DefaultNoInit: tc.defNoInit,
DefaultOverwrite: tc.defOverwrite,
DefaultDecodeUnset: tc.defDecodeUnset,
DefaultRequired: tc.defRequired,
KeepPrefixExpansion: tc.keepPrefixExpand,
Mutators: tc.mutators,
}); err != nil {
if tc.err == nil && tc.errMsg == "" {
t.Fatal(err)
Expand Down