Is possible add pretty print to a file? #705
-
Hi 😃 Your work is amazing but I have a question/idea is possible make a "pretty print" of a file like pass from: [foo]
data1 = { x = 1, y = 2 }
data2 = { x = 1, y = 2 }
[bar]
data1 = { x = 2, y = 1 }
data2 = { x = 2, y = 1 } To a beautifull formatted file like this: [foo]
data1 = { x = 1, y = 2 }
data2 = { x = 1, y = 2 }
[bar]
data1 = { x = 2, y = 1 }
data2 = { x = 2, y = 1 } Exists any function or way to make this? Or if this is not avaliable can you add this :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi! The best you can probably do right now is to decode into an This will emit a "standard" (as far as go-toml is concerned) representation of the data. Because TOML has multiple ways to represent the same data structure, the encoder has to make some default choices, which may not match the style of the input document – for example it does not use inline tables by default. Today, the only way to have a bit more control over the output is to know in advance as much of the structure as possible. Instead of decoding into an To build a general purpose linter that respects the original style of the input document, there needs to be a kind of document structure (to preserve information such as whether a table is inline, or an array). A full-fledged implementation of such structure was initially planned for go-toml v2, but I removed it from scope. The good news is the parser was built in a way that it should be reasonably easy to build such a feature. I'd be happy to guide and take a pull request! |
Beta Was this translation helpful? Give feedback.
Hi! The best you can probably do right now is to decode into an
interface{}
, then encode that value back: https://go.dev/play/p/KlPdwxijdt_IThis will emit a "standard" (as far as go-toml is concerned) representation of the data. Because TOML has multiple ways to represent the same data structure, the encoder has to make some default choices, which may not match the style of the input document – for example it does not use inline tables by default.
Today, the only way to have a bit more control over the output is to know in advance as much of the structure as possible. Instead of decoding into an
interface{}
, decode into astruct
that provides struct field tags to tweak the output style.T…