MustProcessT: return the typed spec #213
Closed
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.
This adds a generic
MustProcessT
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
MustProcessT(string, 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.This change requires
go.mod
to state the version of Go that's needed. Otherwise,go build
defaults to an old version, which doesn't have type parameters:I chose the latest Go, but if you think it should be 1.18 I'm fine with that too.