-
Notifications
You must be signed in to change notification settings - Fork 525
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
fix(resolver): reference strings of int as object #812
Conversation
For strings of numbers (with length 2 or more) that should maintain leading zeroes, add an implicit resolver to preserve the object and return as string. Examples: 0138 = '0138' 0149 = '0149' Whereas 0145 doesn't have the issue. Test mapping: { "test_strs": [ {"test_str": "0138000000000"}, {"test_str": "0149000000000"}, {"test_str": "0145000000000"}, {"test_str": "5945000000000"}, ] } Signed-off-by: erica.brauer <[email protected]>
If it's not desirable to add this resolver by default, anyone looking to reuse the solution can do so with the following
|
Adding test file for reference |
Now that I understand it happens when there's an 8 or 9 in the str I think I'm starting to follow a little better. I tested updating resolver.py line 189 from For reference, the int implicit resolver:
Testing the current state (prior to this PR) with the following:
SafeLoader loads as:
SafeDumper with canonical returns:
and SafeDumper without canonical looks like:
Which seems like, if it's a quoted string of ints, without an 8 or 9, then it dumps as a single-quoted scalar. If it has an 8 or 9, regardless if it's quoted or not, then it dumps as a non-quoted scalar. Is there a way to have the resolver identify the quoted strings of integers first, and then identify those that are not quoted? Would that still align with YAML 1.1? And now it looks like my testing isn't working so well so I guess I'll just close this out for now. |
The question is rather, why do you need the quotes? You never told us the reason.
This gets loaded as However, If you are working with a different loader than pyyaml that reads that
In case of 2) you can just use https://pypi.org/project/yamlcore/ for dumping. It will dump
In case of 1) instead of monkeypatching pyyaml's loader, it would be better to monkeypatch the dumper to add quotes around strings that start with a zero. |
For strings of numbers (with length 2 or more)
that should maintain leading zeroes, add an
implicit resolver to preserve the object and
return as string.
Examples:
0138 = '0138'
0149 = '0149'
Whereas 0145 doesn't have the issue.
Test mapping:
{
"test_strs": [
{"test_str": "0138000000000"},
{"test_str": "0149000000000"},
{"test_str": "0145000000000"},
{"test_str": "5945000000000"},
]
}