Skip to content
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

Struct tag boolean values are not parsed properly when specified with double quotes #49

Open
fchikwekwe opened this issue Jan 18, 2024 · 4 comments · May be fixed by #51
Open

Struct tag boolean values are not parsed properly when specified with double quotes #49

fchikwekwe opened this issue Jan 18, 2024 · 4 comments · May be fixed by #51

Comments

@fchikwekwe
Copy link

The README shows this example in the Sample struct:

...
Working bool   `default:"true"`
...

When running the tests in the defaults_test.go boolean values are not properly being parsed when specified in double quotes above. The following pattern passes:

...
Working bool   `default:true`
...

However this is not a preferred way to specify tags in yaml. I'm validating whether this is based on a recent change to the defaults library behavior or if it is related to a specific go version. If I can validate the isssue, I may open a PR related to this soon.

@robbkidd
Copy link

I've reproduced in a draft PR the ultimate issue: a person cannot set a false value on a boolean field that has a default of true. @fchikwekwe and I were able to sort of work around this by flubbing the default: tag annotation syntax.

@robbkidd
Copy link

Updated the PR to demonstrate that a bool pointer is necessary to be able to have a boolean field that defaults to true and still be able for a user to set the field to false. If this is the only or best way to accomplish default-true booleans, we can open a new PR or update the existing one to not add tests but instead update the README documentation to warn users about defaulting a bool field to true and recommend using a *bool.

@fchikwekwe
Copy link
Author

The scope of this issue is now to document the boolean pointer workaround in the README. I'll open a PR for that and link it shortly.

@robbkidd
Copy link

Then I discover #15 and #30 which describe the same situation. In addition to documenting "bool fields set to default:'true' will not behave how you might think!" would it be helpful for the library to warn or error when setting a field of type bool whose default is true?

@fchikwekwe fchikwekwe linked a pull request Jan 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants