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

Question, is there any plans, interest regarding AoT support? #12

Closed
abravalheri opened this issue Oct 10, 2021 · 2 comments
Closed

Question, is there any plans, interest regarding AoT support? #12

abravalheri opened this issue Oct 10, 2021 · 2 comments

Comments

@abravalheri
Copy link
Contributor

abravalheri commented Oct 10, 2021

Hello, thank you very much for the package!

I was wondering if tomli-w has Array of Tables (AoT) support (or at least if you consider this feature something nice to have in the roadmap).
For example, if we take the following conversion:

>>> import tomli_w
>>> example = {'table': {'nested_table': [{'array_options': [1, 2, 3]}, {'another_array': [1, 2]}, {'c': 3}]}}
>>> print(tomli_w.dumps(example))
[table]
nested_table = [
    { array_options = [
    1,
    2,
    3,
] },
    { another_array = [
    1,
    2,
] },
    { c = 3 },
]

The output looks very weird, and a AoT would make it much easier to read... I had to check the standards to believe it is not following a wrong syntax 😝 (it is explicitly discouraged to have line breaks inside inline tables, though)

In the case AoT is supported, the following would read much nicer:

[[table.nested_table]]
array_options = [
    1,
    2,
    3,
]

[[table.nested_table]]
another_array = [
    1,
    2,
]

[[table.nested_table]]
c = 3

In can see that currently nested tables are automatically handled:

>>> example = {'table': {'nested_table': {'value': 42}}}
>>> print(tomli_w.dumps(example))
[table.nested_table]
value = 42
# instead of "[table]\nnested_table = { value = 42 }"

which conceptually is not very different from AoT, right?

@hukkin
Copy link
Owner

hukkin commented Oct 10, 2021

Hi, thanks for the issue!

I think any style improving changes would be nice to have. That said, the biggest goal of this package for me is having exactly 0 bugs. A simpler style (and fewer lines of code) make that a more realistic goal.

I won't be working on this myself. If someone else does, I'll consider merging it if it doesn't add too much complexity, is well tested, and seems 100% bug free.

Generally I'd advise using tomlkit package instead if formatting style is important for you.

abravalheri added a commit to abravalheri/tomli-w that referenced this issue Oct 15, 2021
Inline tables are a cool feature of the TOML, and very useful as
elements of an array.

However there are some circumstances that they are explicitly
discouraged by spec itself.

For example when dumping a list of dicts that in turn
have lists inside of them, `tomli-w` will introduce line breaks
inside of inline tables, which is not recommended by the spec

This situation was first described in issue hukkin#12.

Please notice this usage pattern is not that uncommon. We can find
examples of this nesting in the wild, like the ones used in `mypy`
configuration:

https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file
hukkin added a commit that referenced this issue Oct 19, 2021
* Adds minimal support for Array of Tables (AoT)

Inline tables are a cool feature of the TOML, and very useful as
elements of an array.

However there are some circumstances that they are explicitly
discouraged by spec itself.

For example when dumping a list of dicts that in turn
have lists inside of them, `tomli-w` will introduce line breaks
inside of inline tables, which is not recommended by the spec

This situation was first described in issue #12.

Please notice this usage pattern is not that uncommon. We can find
examples of this nesting in the wild, like the ones used in `mypy`
configuration:

https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file

* Account for more complicated AoT nesting

* Remove unnecessary logic condition grouping

* Apply suggestions from code review

Co-authored-by: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com>

* Update tomli_w/_writer.py

* Revert check to not-all instead of any-not

There might be a chance any-not is slightly more costly than not-all
(although if it exists, the performance difference should be negligible)

* Simplify is_suitable_inline_table condition

* Add test for empty array

* Satisfy typechecker concern about booleans

* Remove unused 'name' arg from 'is_suitable_inline_table'

* Make line length calculation more precise

* Update tomli_w/_writer.py

Co-authored-by: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com>
@hukkin
Copy link
Owner

hukkin commented Oct 19, 2021

AoT support added in #15

Thanks @abravalheri

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