MustProcess: panic if processing fails, return the value#114
MustProcess: panic if processing fails, return the value#114imjasonh wants to merge 1 commit intosethvargo:mainfrom
Conversation
Signed-off-by: Jason Hall <jason@chainguard.dev>
|
I think this is achievable many idiomatic ways today, so I'm not sold on adding another API function. You could use an What problem are you trying to solve by not having the parsing in |
These are all perfectly reasonable questions. If this isn't accepted, I totally understand. 😄 This came about from looking at how we've used This is ...fine. Some examples define In any case, our usage tends to be only inside My line of thinking was roughly:
That line of thinking might be flawed, or overly clever, but that's how I got to this approach. If this isn't considered an idiomatic way to use the library, I'll accept that. Thanks for this library, and for your thoughtful response 😄 |
I originally made this PR to kelseyhightower/envconfig#213 but this looks to be the supported alternative
This adds a generic
MustProcessmethod, 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
varstatement 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.