feat(encode): add the InlineTables option
Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
+30
-3
@@ -429,9 +429,10 @@ the basic form, so the output always re-parses to the same value.
|
||||
|
||||
### Inline tables
|
||||
|
||||
A table element of a value array is written as one `{a = 1, b = 2}` line while
|
||||
it fits. An inline table that would pass the hundredth column carries newlines
|
||||
and a trailing comma instead, which TOML 1.1 allows:
|
||||
A table element of a value array, and a sub-table inlined by
|
||||
[`InlineTables`](#compact-documents), is written as one `{a = 1, b = 2}` line
|
||||
while it fits. An inline table that would pass the hundredth column carries
|
||||
newlines and a trailing comma instead, which TOML 1.1 allows:
|
||||
|
||||
```toml
|
||||
arr = [1, {
|
||||
@@ -444,6 +445,30 @@ The closing brace and the entries are indented one tab per nesting level, a
|
||||
nested table is measured on its own line, and the output re-parses to the same
|
||||
value either way.
|
||||
|
||||
### Compact documents
|
||||
|
||||
`InlineTables(threshold)` writes a sub-table as an inline table when its
|
||||
single-line rendering is at most `threshold` bytes, and as a table header
|
||||
section when it is longer. A document of small tables therefore grows shorter:
|
||||
|
||||
```go
|
||||
out, err := interpres.NewEncoder().InlineTables(60).Marshal(cfg)
|
||||
```
|
||||
|
||||
With `60` and a table of three short entries, the same value is written
|
||||
|
||||
```toml
|
||||
server = {host = "127.0.0.1", port = 9090, tls = {on = false}}
|
||||
```
|
||||
|
||||
instead of three lines under a `[server]` header and a `[server.tls]` section.
|
||||
A nested sub-table takes part in the same way, and the whole option is off at
|
||||
`0` or less. Two limits are deliberate. An array of tables keeps the `[[a]]`
|
||||
header form, because its inline form re-parses as a value array and would change
|
||||
the value's Go type. And because an inlined table is a value line, every one of
|
||||
them precedes the first header of its document, so a table inlined next to a
|
||||
header is not read back as part of that header's section.
|
||||
|
||||
### Cancellation
|
||||
|
||||
`MarshalContext` and `(*Encoder).MarshalContext` accept a `context.Context`. The
|
||||
@@ -536,12 +561,14 @@ encoder:
|
||||
| `GroupByKind(v bool)` | `true` | group entries as scalars, then sub-tables, then arrays of tables; `false` preserves declaration order |
|
||||
| `OmitEmptyArrays()` | off | skip `key = []` for empty scalar arrays |
|
||||
| `UseLiteralMultiline(threshold int)` | `0` | emit multi-line strings of at least `threshold` bytes as literal `'''...'''` |
|
||||
| `InlineTables(threshold int)` | `0` | write a sub-table inline when its single-line form is at most `threshold` bytes |
|
||||
|
||||
```go
|
||||
out, err := interpres.NewEncoder().
|
||||
GroupByKind(false).
|
||||
OmitEmptyArrays().
|
||||
UseLiteralMultiline(80).
|
||||
InlineTables(60).
|
||||
MarshalContext(ctx, cfg)
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user