Skip to content

Commit

Permalink
Drop ExtractDefaults functionality
Browse files Browse the repository at this point in the history
**:warning: Breaking!** Remove `ExtractDefaults` function. It was rarely used and resulted in extraneous processing.
  • Loading branch information
sethvargo committed Dec 18, 2023
1 parent 053a0d2 commit 9a6052d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 98 deletions.
22 changes: 0 additions & 22 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,28 +242,6 @@ func ProcessWith(ctx context.Context, i any, l Lookuper, mus ...Mutator) error {
return processWith(ctx, i, l, false, mus...)
}

// ExtractDefaults is a helper that returns a fully-populated struct with the
// default values resolved. This is helpful when you want to return a constant
// "default" configuration that is not affected by the user's environment.
//
// type Config struct {
// Port string `env:"PORT,default=8080"`
// }
//
// func DefaultConfig() *Config {
// var cfg Config
// if err := envconfig.ExtractDefaults(ctx, &cfg); err != nil {
// panic("failed to extract default config: %s" + err.Error())
// }
// return &cfg
// }
//
// This is effectively the same as calling [ProcessWith] with an empty
// [MapLookuper].
func ExtractDefaults(ctx context.Context, i any, mus ...Mutator) error {
return processWith(ctx, i, MapLookuper(nil), false, mus...)
}

// processWith is a helper that captures whether the parent wanted
// initialization.
func processWith(ctx context.Context, i any, l Lookuper, parentNoInit bool, mus ...Mutator) error {
Expand Down
76 changes: 0 additions & 76 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2682,79 +2682,3 @@ func TestValidateEnvName(t *testing.T) {
}
})
}
}

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

cases := []struct {
name string
input any
exp any
err error
errMsg string
}{
{
name: "nil",
input: (*Electron)(nil),
err: ErrNotStruct,
},
{
name: "extracts",
input: &struct {
Field1 string `env:"FIELD, default=8080"`
Field2 string `env:"FIELD"`
}{},
exp: &struct {
Field1 string `env:"FIELD, default=8080"`
Field2 string `env:"FIELD"`
}{
Field1: "8080",
Field2: "",
},
},
}

for _, tc := range cases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()
if err := ExtractDefaults(ctx, tc.input); err != nil {
if tc.err == nil && tc.errMsg == "" {
t.Fatal(err)
}

if tc.err != nil && !errors.Is(err, tc.err) {
t.Fatalf("expected \n%+v\n to be \n%+v\n", err, tc.err)
}

if got, want := err.Error(), tc.errMsg; want != "" && !strings.Contains(got, want) {
t.Fatalf("expected \n%+v\n to match \n%+v\n", got, want)
}

// There's an error, but it passed all our tests, so return now.
return
}

opts := cmp.AllowUnexported(
// Custom decoder type
CustomDecoderType{},

// Custom standard library interfaces decoder type
CustomStdLibDecodingType{},

// Custom decoder type that returns an error
CustomTypeError{},

// Anonymous struct with private fields
struct{ field string }{},
)
if diff := cmp.Diff(tc.exp, tc.input, opts); diff != "" {
t.Fatalf("mismatch (-want, +got):\n%s", diff)
}
})
}
}

0 comments on commit 9a6052d

Please sign in to comment.