add cli flag to attempt to cast to integer before boolean #11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
I ran into a scenario where an array-like collection of xml elements indexed by an attribute were being parsed as a boolean when the size of the was not greater than 2. Evidently, it is ambiguous whether something whose only instances are 0 and 1 should be cast as a boolean or not.
e.g.
becomes
ArrayIndex bool
:instead of
ArrayIndex int
:So I made a an "ObserveIntEager" flag in order to disambiguate this scenario. It essentially disallows "1" or "0" from ever being treated as a boolean at all by observing "int" first when it is enabled.
goxmlstruct -observe-int-eager <example>.xml
Though, in the process of testing regression, i think i may have discovered some unintended behaviour. It seems that if there is a single instance of 1 or 0 in a collection of integers, the spec becomes "string". The reason being that 1 and 0 are always observed as "bool" (because it is observed first), and the combination of instances of "int" + "bool" is then treated as "string".
Here are some test cases demonstrating the bug
Note that tests that include 1 or 0 fail, and the tests that do not include 1 or 0 pass.
If we have a collection of integer values that include 1 or 0 (e.g.
test_int_parse
), the spec becomes string.If none of the integer values are 1 or 0 (e.g.
test_int_parse_without_1_0
), the spec correctly becomes int.Now I am wondering whether 1 and 0 should just be excluded from the ParseBoolean check altogether. If that's the case then this PR should be closed.
P.S. Huge fan of chezmoi!