-
Notifications
You must be signed in to change notification settings - Fork 5
map with no values does not return error #13
Comments
I'm not sure whether this can be considered a bug. Thinking of string, it's valid and desired behavior of the package to parse environment variable set to empty string as empty string. Using the same logic here, we should allow empty map parsing, as empty map is also a valid map. |
I agree that dealing with map is tricky but I think this is indeed bug. If env is not provided at all I would expect it fails, at least this is described in package motivation that all envs are considered as required. I was not looking deep in the code so I dont have any proposal yet. |
Well yeah, but how would you then parse an empty map? Also how does this empty map problem differs from the typo issue described above? |
Yes, you're kinda right. But As @jan-dubsky said, I don't see any elegant way how to parse it, since the keys are baked in the env var name. A solution might be to specify the map as "required" by some extra annotation, and in such case, it would override the default behavior and would check if the map contains at least one key. But I am not sure if this would improve the package in general (we want to keep it simple). This would be a little bit hairy. To add an example: Imagine you have a map representing a black-list. If you don't want to blacklist anything, empty map is completely valid input. So you definitely don't want to enforce it as default behavior. Then you have the only option to make the whole thing configurable as described above. Once you do that, there is no way back. So we should really think about it, if it's really the way to go. Do you have any concrete use-case in mind which has put you in trouble? |
Moreover even the On the other hand you are right that the README presents whole env package as safe and the map behavior doesn't actually fulfill this promise. I will try to write a section of README explaining mostly what I wrote here and warning the user that maps are slightly more error prone than the rest of the package. |
For black-list example I would add go tag, something like: https://play.golang.org/p/eFZbvlA0qrF I get it now why it behaves differently but still It was big surprise for me. |
* Describe key optionality property of maps Closes #13 * CR * Add headline to the README - Beware! Map keys are optional Co-authored-by: Jan Dubsky <[email protected]> Co-authored-by: Ondřej Hrubý <[email protected]>
If there are no map values, package does not return any error.
https://play.golang.org/p/hbIQe_nTST0
The text was updated successfully, but these errors were encountered: