Skip to content

Add MustProcess which panics on errors #115

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

Merged
merged 2 commits into from
Jul 6, 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
18 changes: 18 additions & 0 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ func Process(ctx context.Context, i any, mus ...Mutator) error {
})
}

// MustProcess is a helper that calls [Process] and panics if an error is
// encountered. Unlike [Process], the input value is returned, making it ideal
// for anonymous initializations:
//
// var env = envconfig.MustProcess(context.Background(), &struct{
// Field string `env:"FIELD,required"`
// })
//
// This is not recommend for production services, but it can be useful for quick
// CLIs and scripts that want to take advantage of envconfig's environment
// parsing at the expense of testability and graceful error handling.
func MustProcess[T any](ctx context.Context, i T, mus ...Mutator) T {
if err := Process(ctx, i, mus...); err != nil {
panic(err)
}
return i
}

// ProcessWith executes the decoding process using the provided [Config].
func ProcessWith(ctx context.Context, c *Config) error {
if c == nil {
Expand Down
65 changes: 65 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2958,6 +2958,71 @@ func TestProcessWith(t *testing.T) {
}
}

func TestMustProcess(t *testing.T) {
cases := []struct {
name string
target any
exp any
env map[string]string
expPanic bool
}{
{
name: "panics_on_error",
target: &struct {
Field string `env:"FIELD,required"`
}{},
exp: &struct {
Field string `env:"FIELD,required"`
}{},
env: nil,
expPanic: true,
},
{
name: "returns_value",
target: &struct {
Field string `env:"FIELD,required"`
}{},
exp: &struct {
Field string `env:"FIELD,required"`
}{
Field: "value",
},
env: map[string]string{
"FIELD": "value",
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
for k, v := range tc.env {
t.Setenv(k, v)
}

defer func() {
if r := recover(); r != nil {
if !tc.expPanic {
t.Fatal(r)
}
} else if tc.expPanic {
t.Errorf("expected a panic")
}
}()

ctx := context.Background()
got := MustProcess(ctx, tc.target)

if got != tc.target {
t.Errorf("expected result to be the same object as target (%#v, %#v)", got, tc.target)
}

if diff := cmp.Diff(tc.exp, tc.target); diff != "" {
t.Fatalf("mismatch (-want, +got):\n%s", diff)
}
})
}
}

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

Expand Down