Repository navigation
Conversation
A Value holding a float with an integral value (1.0) was serialized as an integer: the dynamic number branch probes as_i64/as_u64 before as_f64, and facet-value's integer getters answered for F64-tagged numbers. JSON wrote `1`, and tagged formats emitted the I64 dynamic value tag. - facet-value: dyn_get_i64/dyn_get_u64 only answer for integer numbers - facet-format: integer-typed targets still accept integral floats from dynamic values, now via as_f64 - facet-json: without the `fast` feature, write f64 with Debug so 1.0 is written as `1.0`, matching the zmij output Fixes facet-rs#68
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Author
|
Hey @fasterthanlime, could you take a look at this when you get a chance? It's been open for a couple of weeks. Happy to change anything. Thanks! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #68.
What's wrong
If a
facet_value::Valueholds a float like1.0, it gets serialized as an integer. So1.0turns into1after a JSON round trip, and postcard writes it with theI64tag instead ofF64, so it comes back as an integer.The cause: the serializer asks the value for
as_i64()first, thenas_u64(), thenas_f64(). facet-value happily answersas_i64()withSome(1)for a stored1.0, so the float case is never reached.What I changed
dyn_get_i64/dyn_get_u64now only answer for numbers that were stored as integers. Floats only come out throughget_f64.VNumber::to_i64()and friends are untouched.serialize_scalar_from_dynamic(a dynamic value serialized into an integer-typed field) relied on the old lenient behavior, so it now falls back toas_f64()for whole-number floats. That path behaves the same as before.fastfeature,f64was printed withDisplay, which writes1.0as1. It now usesDebug, which prints1.0, same as thefastpath. Without this, the repro from the issue still fails in the default config. This also affects plainf64fields:x: f64 = 1.0now comes out as1.0either way.I went with the first option from the issue (the integer getters return
Nonefor floats), since the other option would need a change to the dynamic value vtable in facet-core.Heads up
Anything calling
as_i64()/as_u64()on aPeekDynamicValue/PokeDynamicValuebacked by aValuenow getsNonefor floats, even whole-number ones. In this repo the only callers are the two serializer paths above.Tests
New tests in
facet-postcard/tests/integration/issue_68.rs(it's the one crate that has both facet-json and facet-value as dev-dependencies). They cover the JSON repro from the issue, integers staying integers, and a postcard round trip keeping the float tag. Three of the four fail onmain.cargo nextest run --workspace: 2780 passedcargo nextest run -p facet-postcard -p facet-json -p facet-value --features facet-json/fast: 627 passedjust clippyandcargo fmt --checkare clean