MustProcess: panic if processing fails, return the value #114
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I originally made this PR to kelseyhightower/envconfig#213 but this looks to be the supported alternative
This adds a generic
MustProcess
method, that panics if processing fails, and returns the spec value.Example usage:
This lets the env struct be moved to an init-time var, out of
func main
, and retains all the env parsing behavior we've all come to know and love.The structure of that
var
statement is kind of gross (inline-defining a struct and its empty value, and passing it to the method), if I'm missing some way to make it better, let me know.Type parameters were required, since
MustProcess(ctx, interface{}) interface{}
loses information about the input type. You could cast back to the type you want, but generics are better IMO, since it lets you do it all in one statement.