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

Add minimal support for flattening roundtrips through maps #455

Merged
merged 7 commits into from
May 1, 2023

Conversation

juntyr
Copy link
Member

@juntyr juntyr commented Apr 24, 2023

Supersedes #403, #453, and #454 to add minimal support for #[serde(flatten)] in RON. Instead of many workarounds to make flattened structs serialise into nice RON struct syntax and deserialise from them, both of which are really difficult since serde gives us no information to work with and just delegates to map serialisation/deserialisation, this PR just ensures that at least flattened structs can roundtrip through RON.

For this, just one hack is necessary. If we detect that a map is deserialised into what looks like a struct (thanks @clouds56 for contributing the detection in #454), we enforce that map key identifiers are actually strings written exactly as ", identifier, ". This seems to be enough to get the tests to work that I initially designed for #403.

@torkleyy I'd be in favour of landing this instead of going down the rabbit hole of #403 since that just fixes one of the two parts and getting flattened structs to serialise correctly would require hacks of another order.

This would not really fix #115 but at least get some working if imperfect solution.

  • Add tests for the new error cases, i.e. when flattening goes wrong
  • I've included my change in CHANGELOG.md

@codecov-commenter
Copy link

codecov-commenter commented Apr 24, 2023

Codecov Report

Patch coverage: 97.50% and project coverage change: -0.24 ⚠️

Comparison is base (484fcab) 87.06% compared to head (bcbb027) 86.83%.

❗ Current head bcbb027 differs from pull request most recent head 775c39d. Consider uploading reports for the commit 775c39d to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #455      +/-   ##
==========================================
- Coverage   87.06%   86.83%   -0.24%     
==========================================
  Files          60       61       +1     
  Lines        7601     7967     +366     
==========================================
+ Hits         6618     6918     +300     
- Misses        983     1049      +66     
Impacted Files Coverage Δ
src/de/mod.rs 73.73% <80.48%> (-3.19%) ⬇️
src/de/id.rs 87.65% <100.00%> (-12.35%) ⬇️
tests/115_minimal_flattening.rs 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

src/de/mod.rs Outdated Show resolved Hide resolved
@juntyr juntyr merged commit c0d7421 into ron-rs:master May 1, 2023
@juntyr juntyr deleted the 115-targeted-struct-flattening branch May 1, 2023 09:56
juntyr added a commit to juntyr/ron that referenced this pull request Aug 16, 2023
…n-rs#455)

* Detect flattened structs in `Deserializer::deserialize_map`

* Add minimal and targeted support for `#[serde(flatten)]`

* Fix the serde flatten canary for MSRV 1.57

* Added extra tests for missing quotation marks in flattened struct field names

* Clean up the serde flatten canary capture

* Document the #[serde(flatten)] hack

* Final small cleanup

---------

Co-authored-by: Clouds Flowing <[email protected]>
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 this pull request may close these issues.

Struct flattening not working
3 participants