-
Notifications
You must be signed in to change notification settings - Fork 116
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
Problem with bool variable in YAML file #61
Comments
i made some research and found place where shit happened. |
Hi @dikkini, this is kinda expected behavior: A possible solution is to use a pointer instead: type AppConfig struct {
IsDebug *bool `env:"RE_IS_DEBUG" env-required:"true"`
} yep, it doesn't look so convenient, but this way you may Go be able to clearly differentiate between the zero value (default value) of the variable and the absence of the variable. The same goes for json, toml, etc. In theory, it is possible to check if the field is missing instead of filling it with zero value, but in Go it is made by means of pointers. So if you want to apply the same checks for the value instead of the pointer, it would require writing a custom unmarshaller for each supported file format, which is definitely out of the library scope for now. |
You can create map for required values and check is key exists in map. What do you think? |
And i pretty sure that other libraries dont have problems with negative bool variables, but this library does not allow it's users to use negative bool variable - it means - it does not support bool variables at all. |
I do not agree with your point about bool variable support - if you don't use pointers, zero value always treated like "not provided" - same with json, yaml, etc. But I will think about this proposal, maybe we will give it a try, or will find a better solution. |
I agree, but use bool variable without PTR useless, so is it worth to write about it in README?
I still has an error. type AppConfig struct {
IsDebug *bool `env:"RE_IS_DEBUG" env-required:"true"`
}
config = &AppConfig{}
err := cleanenv.ReadConfig("config.yml", &config) error: wrong type ptr |
@dikkini its because you have a double pointer to |
Hello. There is any way to use it with "env-default" and "yaml"? Is there any other solution for using bool type? |
No, the pointer is the only way I know to differentiate between true, false and "not provided" in this case. |
Regarding this issue, I'm trying to print the description of
Is it related? Is there a fix for this? I don't think I can use pointers in my case, as I'm reading from a literal value. EDIT: |
@mhSangar it has to be quoted, this is how tags in Golang work |
Closed because no better way to differentiate missing bool field and false bool field found. Use bool pointer. |
It should be mentioned in the ReadMe file :(. |
@haithngn makes sense. I'll add a note. |
yaml file
golang struct
i got an error
field \"IsDebug\" is required but the value is not provided"
, but if setisdebug: true
- all ok, config parse without any errors.The text was updated successfully, but these errors were encountered: