tomlkit icon indicating copy to clipboard operation
tomlkit copied to clipboard

Multiline array option for `dumps`

Open dbohdan opened this issue 1 year ago • 2 comments

It would be useful for my project Remarshal and likely for other downstream users of TOML Kit to be able to tell toml.dumps to output multiline arrays. For example, you could call toml.dumps(foo, multiline=True) for only multiline arrays, call toml.dumps(foo, multiline=5) and make arrays with five or more items multiline, or have it based on the line length. Without this feature, you either get output like my TOML example or have to construct the document manually.

The option could also apply to inline tables if a future TOML spec added them.

dbohdan avatar Nov 11 '24 18:11 dbohdan

I also have a need to write out arrays in multiple lines and discovered the multiline() method on the Array class, which sets a flag to say the Array should be serialized in multiple lines.

What I do is to get a DOM-like representation of my data using item(data) and then I get from that the instances of Array I am interested in, call multiline(True) on them. Finally, I call as_string() on the whole thing to get the serialized TOML that I can write out to a file.

Hope this helps. I know this is not exactly what you asked for but it works for my use case, perhaps it works for you.

alexvoss avatar Jan 03 '25 00:01 alexvoss

@alexvoss Thanks for suggesting this! It works for me, too. I have implemented this approach in my project and credited you for the suggestion: https://github.com/remarshal-project/remarshal/commit/a4560cd20eecd7d1a01096d3370dbc35643797f2.

dbohdan avatar Jan 17 '25 12:01 dbohdan