refactor: unify the error paths in one Path type
Test / test (push) Canceled after 1m31s

Assisted-by: GLM 5.3 Flash
This commit is contained in:
2026-09-22 00:34:48 +02:00
parent a8a2fcf8c3
commit b02471c09a
6 changed files with 119 additions and 69 deletions
+15 -9
View File
@@ -738,26 +738,32 @@ if se, ok := errors.AsType[*interpres.SyntaxError](err); ok {
followed by a caret line marking the column, for messages the reader sees
under the input.
### `type DecodeError struct{ Path []string; Err error }`
### `type DecodeError struct{ Path Path; Err error }`
Wraps a decoding failure with the key path at which it happened. `Path` lists
one segment per level from the document root, the outermost key first: a key
contributes its name, an array element its bracketed index, so the path of the
`weight` field in the first item reads `["items", "[0]", "weight"]`. The
rendered message is unchanged by the type; read the fields instead of parsing
the message:
`weight` field in the first item reads `["items", "[0]", "weight"]` and its
`String()` renders `items[0].weight`. Read the fields instead of parsing the
message:
```go
if de, ok := errors.AsType[*interpres.DecodeError](err); ok {
fmt.Println(de.Path, de.Err)
fmt.Println(de.Path.String(), de.Err)
}
```
### `type EncodeError struct{ Path string; Err error }`
### `type EncodeError struct{ Path Path; Err error }`
Wraps an encoding failure with the key path of the value that failed, in the
document's own notation: `server.ports[2]`. Read it with `errors.AsType` the
same way.
Wraps an encoding failure with the key path of the value that failed, the
same `Path` type the decode error carries, so `server.ports[2]` reads the
same on both sides. Read it with `errors.AsType` the same way.
### `type Path []string`
The path both error wrappers carry, one segment per level from the document
root. `String()` renders the TOML notation: keys join with dots, an index
attaches to the previous segment in brackets, `items[0].weight`.
### `type Decoder`