-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add notempty struct tag #44
Conversation
Sorry, I don't really understand the use case here. To be honest, this feels like a bug in docker-compose. An unset environment variable and an environment variable set to the empty string are intentionally different constructs. |
Ok maybe my use case is too specific but it was just an example. The behavior of docker-compose is actually not a bug but probably not taking full adavatage of docker-compose features. Unfortunately I currently have no easy way of solving this. Nontheless I still think checking for zero value in genaral is a valid use case. There could be multible different reasons why an variable is set empty by accident. A few I can think of right now are for example a misbehaving setup scripts or you forgotten value in a .env files, possibly even a misconfigured system. While values which are converted into a different type might error straigt away string values will be empty. On fields you know that can not be empty like for example a database connection string you still have to check. This can be cumbersome for large structs and might negate the gains from using this lib in the first place. |
All of the examples you described are upstream bugs (misbehaving setup scripts, forgotten values in .env, misconfigured system). I don't think it's the responsibility of a library to handle those misconfigurations. This library isn't a replacement for configuration validation - you still need to do that. For example, you still need to make sure the value given for a database string actually looks like a database string. If you really wanted this functionality, you could implement a custom type and decoder: type AlwaysString string
func (a AlwaysString) EnvDecode(val string) error {
if val == "" {
return fmt.Errorf("...")
}
*a = val
return nil
} But I don't think this makes sense as a general-purpose functionality in the library. |
Well ok but what is required then for? This also a kind of validation which you only need when you have a so called upstream bug. Why even error out when type conversion does not work. I mean its an upstream bug just make sure you provide the correct value. Why do you need default values? You could just check the value yourself. What I'm getting at here is that I think checking for an empty environment variable is well whithin scope of a library like this. (Btw similar libs support this feature). It's not like fully fletched regex-validatation or doing fancy log4j-style bloat. I really like the style and simplicity (like prefixes and you only use one struct tag) of this library and would hate to not use it. And to be honest writing |
FOO= and unset FOO In the first,
I disagree. Feel free to use those other libs.
This library is small and simple because I reject changes like these. |
Even Bash (and most programs I know of) treats empty and unset variables mostly the same. There is not that much difference. It's by far not out of scope. |
Yes of course thats why I said mostly. I have yet to actually differenciate between empty and unset in bash. In your provided links even the easiest way of checking for unset is checking if the variable is zero length aka empty |
As I probably cannot convince you otherwise I would like to know why you think validating if a variable is set with |
Hey, so I realize my last few comments sounded a bit sour, and I apologize for that. It's just a bit frustrating not to understand the issue because to me your arguments and existing code are somewhat contradicting. Yes I am a relatively novice programmer, and I'm genuinely just trying to learn and improve. With your employment history you are probably very experienced, and I don't and never wanted to undermine that fact. So trying to clear things up: What I do understand:
What I cannot wrap my head around:
Summary: I don't understand the general difference between I am grateful for any response helping me to resolve my confusion. Sorry for the wall of text 🍪 |
Currently there is no easy way of knowing if a environment variable is set with an empty value except for checking the value yourself.
In certain workflows like for example when using docker-compose with variable substituion this can be a problem.
In this case a missing system variable is replaced by an empty string. The resulting container sees a set but empty variable. Therefore the
required
keyword does not result in an error and is useless.notempty
usesrequired
and errors on a empty variable with message "empty value"