Skip to content

Commit

Permalink
Handle case where user accidentally uses Process() with a Config when… (
Browse files Browse the repository at this point in the history
#109)

* Handle case where user accidentally uses Process() with a Config when they should use ProcessWith() by converting their call to the respective ProcessWith call.

* Document Special case for Config

* Document Special case for Config
  • Loading branch information
pdewilde committed May 16, 2024
1 parent 7e286de commit 61cfc0d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
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 {
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

0 comments on commit 61cfc0d

Please sign in to comment.