feat: add function to override default envvar name#294
feat: add function to override default envvar name#294alexflint merged 4 commits intoalexflint:masterfrom
Conversation
| return os.Args[1:] | ||
| } | ||
|
|
||
| // DefaultEnvNameFunc is a function that takes a field and returns the default name for the environment variable that sets its value. |
There was a problem hiding this comment.
For now, no need to export any of the new types or functions in this file. Could the new config field just have the type func(field reflect.StructField) string directly inlined within the struct?
There was a problem hiding this comment.
Yup, no problem, I'm generally a fan of DRY but if you prefer to repeat it explicitly that's fine
| } | ||
|
|
||
| // DefaultEnvNameUpperSnake converts the given field name to upper-snake-case (e.g. FooABCBar -> FOO_ABC_BAR). | ||
| func DefaultEnvNameUpperSnake(field reflect.StructField) string { |
There was a problem hiding this comment.
The down side of having this function be part of the library is that once it's in, it can't be changed because people will start to rely on it. For now, would it be alright to leave this out?
There was a problem hiding this comment.
Yeah I can push it to my own repo. Out of curiosity, what would be the downside of leaving this implementation in the library? It's still opt-in if anyone wants to rely on it, and is covered with tests that document the expected behavior
| type DefaultEnvNameFunc func(field reflect.StructField) string | ||
|
|
||
| // DefaultEnvNameUpper converts the given field name to uppercase. | ||
| func DefaultEnvNameUpper(field reflect.StructField) string { |
There was a problem hiding this comment.
I'd say leave this out and just directly use strings.ToUpper as needed in the main code in parser.go. Will leave separate comment indicating what I mean.
| spec.env = envPrefix + value | ||
| } else { | ||
| spec.env = envPrefix + strings.ToUpper(field.Name) | ||
| if defaultEnvName == nil { |
There was a problem hiding this comment.
Just to keep things as straightforward as possible, could this chunk of code be
if defaultEnvName == nil {
spec.env = envPrefix + strings.ToUpper(field.Name)
} else {
spec.env = envPrefix + defaultEnvName(field)
}
There was a problem hiding this comment.
Pushed all the changes, let me know what you think
|
Hey @alexflint I've addressed your feedback but I've pushed a 3rd commit that I think makes this feature more powerful:
Let me know if you like it, otherwise, feel free to review up to the 2nd commit and I'll drop/revert this last commit so we can merge (I'm hopeful that the 2nd commit matches your expectations based on the previous feedback). Thanks again for your time and review! |
|
Hey @CarlosRDomin thanks for these updates. How about adding a second config variable "AllHaveEnv bool" that makes it the case that all fields have environment variables? The reason I suggest this rather than allowing empty return value from DefaultEnvName is that I think the latter would have to be implemented in most cases with an explicit list of field names to include/exclude, meaning that you'd run back into the issue of having a list of string literals somewhere that have to be kept up to date with the names of the fields on the struct, as in: func myDefaultEnvName(f reflect.StructField) string {
if f.Name == "abc" || f.Name == "xyz" || ... {
return ""
}
return snakeCaseFromFieldName(f.Name)
}In my own code, I'd make use of this DefaultEnvName feature, but without turning it on by default for all arguments. If you're up for implementing AllHaveEnv then I'd be happy to review it as part of this PR. |
|
Thanks for the feedback @alexflint! I thought about something similar to Anyway, I think |
|
Btw would you want me to re-introduce the |
|
@alexflint pushed. Let me know what you think of this new version, and whether you'd like me to include |
|
Hey this looks great thanks @CarlosRDomin |
Addresses #293