feat!: rework the public API to json/v2-style variadic options
Test / test (push) Successful in 1m49s
Test / test (push) Successful in 1m49s
Assisted-by: GLM 5.3 Flash
This commit is contained in:
@@ -15,7 +15,7 @@ the entire official [toml-test](https://github.com/toml-lang/toml-test) suite:
|
||||
optional as of 1.1; arrays and inline tables, multi-line as of 1.1.
|
||||
- **Decoding and encoding**: `Parse` for an untyped tree, `Unmarshal` and
|
||||
`Marshal` for structs and maps, mirroring `encoding/json`.
|
||||
- **Strict decoding**: `NewDecoder().DisallowUnknownFields()` rejects keys that
|
||||
- **Strict decoding**: `RejectUnknownFields(true)` rejects keys that
|
||||
match no destination field, at every struct depth.
|
||||
- **Custom types**: `Marshaler` and `Unmarshaler` let a type control its own
|
||||
TOML representation in both directions, and `encoding.TextMarshaler` and
|
||||
@@ -101,9 +101,7 @@ which is the layout that re-parses to the same tree.
|
||||
### Strict decoding
|
||||
|
||||
```go
|
||||
err := interpres.NewDecoder().
|
||||
DisallowUnknownFields().
|
||||
Decode(data, &cfg)
|
||||
err := interpres.Unmarshal(data, &cfg, interpres.RejectUnknownFields(true))
|
||||
```
|
||||
|
||||
A key with no matching field becomes an error instead of a silent drop.
|
||||
@@ -132,16 +130,20 @@ func (ip *IP) UnmarshalTOML(data any) error {
|
||||
The value `MarshalTOML` returns is encoded in place of the receiver;
|
||||
`UnmarshalTOML` receives the parsed value verbatim.
|
||||
|
||||
### Encoder options
|
||||
### Options
|
||||
|
||||
```go
|
||||
out, err := interpres.NewEncoder().
|
||||
Layout(interpres.LayoutKindDeclaration), // preserve declaration order
|
||||
OmitEmptyArrays(). // skip empty scalar arrays
|
||||
LiteralMultiline(80), // long multi-line strings as literal blocks
|
||||
Marshal(cfg)
|
||||
out, err := interpres.Marshal(cfg,
|
||||
interpres.Layout(interpres.LayoutKindDeclaration), // preserve declaration order
|
||||
interpres.OmitEmptyArrays(true), // skip empty scalar arrays
|
||||
interpres.LiteralMultiline(80), // long multi-line strings as literal blocks
|
||||
)
|
||||
```
|
||||
|
||||
The decode and encode calls take variadic options, the shape
|
||||
encoding/json/v2 uses for its own. `UnmarshalRead(r, v, opts...)` and
|
||||
`MarshalWrite(w, v, opts...)` are the streaming forms.
|
||||
|
||||
### Cancellation
|
||||
|
||||
```go
|
||||
@@ -151,8 +153,7 @@ defer cancel()
|
||||
out, err := interpres.MarshalContext(ctx, cfg)
|
||||
```
|
||||
|
||||
`ParseContext`, `UnmarshalContext`, `(*Decoder).DecodeContext` and
|
||||
`(*Encoder).MarshalContext` follow the same pattern.
|
||||
`ParseContext`, `UnmarshalContext` and `MarshalContext` follow the same pattern.
|
||||
|
||||
The full rules for field matching, numeric conversion and emission live in
|
||||
[docs/API.md](docs/API.md).
|
||||
|
||||
Reference in New Issue
Block a user