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

Handle case where user accidentally uses Process() with a Config when… #109

Merged
merged 3 commits into from
May 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,18 @@ 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
}

// Process decodes the struct using values from environment variables. See
// [ProcessWith] for a more customizable version.
// [ProcessWith] for a more customizable version. If *Config is provided for i,
// [ProcessWith] is called using the *Config with mus appended.
func Process(ctx context.Context, i any, mus ...Mutator) error {
if v, ok := i.(*Config); ok {
pdewilde marked this conversation as resolved.
Show resolved Hide resolved
v.Mutators = append(v.Mutators, mus...)
return ProcessWith(ctx, v)
}
return ProcessWith(ctx, &Config{
Target: i,
Mutators: mus,
Expand Down Expand Up @@ -529,7 +534,7 @@ func processWith(ctx context.Context, c *Config) error {

// SplitString splits the given string on the provided rune, unless the rune is
// escaped by the escape character.
func splitString(s string, on string, esc string) []string {
func splitString(s, on, esc string) []string {
a := strings.Split(s, on)

for i := len(a) - 2; i >= 0; i-- {
Expand Down
39 changes: 39 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,45 @@ type MapStruct struct {

type Base64ByteSlice []Base64Bytes

func TestProcessWithConfigArgument(t *testing.T) {
t.Parallel()

ctx := context.Background()
lookuper := MapLookuper(map[string]string{
"MEAT_TYPE": "chicken",
})

firstMutator := MutatorFunc(func(ctx context.Context, originalKey, resolvedKey, originalValue, currentValue string) (newValue string, stop bool, err error) {
if originalKey == "MEAT_TYPE" && currentValue == "chicken" {
return "pork", false, nil
}
return currentValue, false, nil
})

secondMutator := MutatorFunc(func(ctx context.Context, originalKey, resolvedKey, originalValue, currentValue string) (newValue string, stop bool, err error) {
if originalKey == "MEAT_TYPE" && currentValue == "pork" {
return "fish", false, nil
}
return currentValue, false, nil
})

var meatConfig Meat
cfg := &Config{
Target: &meatConfig,
Lookuper: lookuper,
Mutators: []Mutator{firstMutator},
}

err := Process(ctx, cfg, secondMutator)
if err != nil {
t.Errorf("unexpected error from Process: %s", err.Error())
}

if diff := cmp.Diff(meatConfig.Type, "fish"); diff != "" {
t.Errorf("wrong value in meatConfig. Diff (-got +want): %s", diff)
}
}

func TestProcessWith(t *testing.T) {
t.Parallel()

Expand Down