feat: add the Document edit pipeline with comment-preserving write
Test / test (push) Successful in 1m35s
Test / test (push) Successful in 1m35s
Assisted-by: GLM 5.3 Flash
This commit is contained in:
+35
-2
@@ -150,6 +150,37 @@ document and from `doc.Map()` is the same value.
|
||||
array of tables, and the inline tables inside a value array, with `nil` for the
|
||||
elements that are not tables.
|
||||
|
||||
### Editing a document
|
||||
|
||||
The document is writable, which makes the read-change-write loop a round trip
|
||||
through one value. The typed getters read with one call:
|
||||
|
||||
| Getter | Returns |
|
||||
|---|---|
|
||||
| `GetString(key)` | `(string, bool)` |
|
||||
| `GetInt(key)` | `(int64, bool)` |
|
||||
| `GetFloat(key)` | `(float64, bool)` |
|
||||
| `GetBool(key)` | `(bool, bool)` |
|
||||
| `GetArray(key)` | `([]any, bool)` |
|
||||
| `GetTable(key)` | `(*Table, bool)` |
|
||||
|
||||
`Set(key, value)` stores a value, keeping an existing key's position and
|
||||
comments and appending a new key to the end; a `map[string]any` value becomes
|
||||
a table of its own under a header, its keys in sorted order. `Delete(key)`
|
||||
removes a key and everything it holds. Every method exists on `Document` for
|
||||
the root table and on `Table` for the table itself.
|
||||
|
||||
`Marshal` writes the document back as it stands: keys in written order, the
|
||||
comments above the lines and headers they belonged to, tables that were
|
||||
written inline written inline again. `UnmarshalDocument(doc, v)` decodes the
|
||||
edited document into a typed destination without parsing again.
|
||||
|
||||
```go
|
||||
doc, err := interpres.Parse(data)
|
||||
doc.Set("port", 9090)
|
||||
out, err := interpres.Marshal(doc)
|
||||
```
|
||||
|
||||
### Comments
|
||||
|
||||
A comment belongs to the line it precedes or follows, and to the node that line
|
||||
@@ -868,8 +899,10 @@ long as no setter races with a call.
|
||||
|
||||
### `type Document`, `type Table`, `type Entry`
|
||||
|
||||
See [Documents](#documents). A `Document` is what `Parse` returns, and it is
|
||||
not a value `Marshal` accepts.
|
||||
See [Documents](#documents). A `Document` is what `Parse` returns, and
|
||||
`Marshal` writes it back: the keys in written order, the comments in place,
|
||||
the inline tables inline. `UnmarshalDocument(doc, v)` decodes it without
|
||||
parsing again.
|
||||
|
||||
### `type Marshaler interface{ MarshalTOML() (any, error) }`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user