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

"Values must be emitted before tables" if array of values follows array of tables #61

Open
VOsakosa opened this issue Apr 23, 2023 · 2 comments

Comments

@VOsakosa
Copy link

When a list that is dumped inline is defined in a dict before a list of tables, dumping fails. This is duplicate of #45, which was already closed.

Minimal working example

>>>import rtoml
>>>rtoml.dumps({'bookmarks': [{'ch': 12.0}],'auto_download': ['ja']})
File .venv/lib/python3.11/site-packages/rtoml/__init__.py:49, in dumps(obj, pretty)
     46 else:
     47     serialize = _rtoml.serialize
---> 49 return serialize(obj)

TomlSerializationError: values must be emitted before tables

This does not occur if the order of the keys is switched:

>>>import rtoml
>>>print(rtoml.dumps({'auto_download': ['ja'],'bookmarks': [{'ch': 12.0}]}))
auto_download = ["ja"]

[[bookmarks]]
ch = 12.0

Taking care to define the items in the right order is for now a doable workaround.

rtoml Version: 0.9.0
Python Version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]

@samuelcolvin
Copy link
Owner

thanks for reporting, happy to accept a PR to fix this if you can find a way.

@VOsakosa
Copy link
Author

Well, basically one just has to update the sorting code in ser.rs. To correctly check if something is an Array of Tables, one in theory had to check if every item is a dict.

But as far as I know, rtoml does not serialize arrays containing both dicts and other values, so checking if the first element is a dict should be fine.

I will try, but this would be my very first PR.

VOsakosa pushed a commit to VOsakosa/rtoml that referenced this issue Apr 23, 2023
- Check if the first element of a list is a dict if true,
  order it with the dicts
- Write a test case in test_dump.py
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

No branches or pull requests

2 participants