Assisted-by: GLM 5.3 Flash
This commit is contained in:
+47
-13
@@ -68,37 +68,65 @@ func (e *SyntaxError) SourceLine(src []byte) string {
|
||||
return string(src[start:end]) + "\n" + strings.Repeat(" ", off-start) + "^"
|
||||
}
|
||||
|
||||
// A Path names a value in a document, one segment per level from the root:
|
||||
// a key contributes its name and an array element its bracketed index, so the
|
||||
// path of the weight field of the first item is the segments
|
||||
// ["items", "[0]", "weight"]. String renders the TOML notation,
|
||||
// "items[0].weight".
|
||||
type Path []string
|
||||
|
||||
// String renders the path the way a TOML document writes it: keys join with
|
||||
// dots and an index attaches to the previous segment in brackets.
|
||||
func (p Path) String() string {
|
||||
var b strings.Builder
|
||||
for _, s := range p {
|
||||
if strings.HasPrefix(s, "[") {
|
||||
b.WriteString(s)
|
||||
continue
|
||||
}
|
||||
if b.Len() > 0 {
|
||||
b.WriteByte('.')
|
||||
}
|
||||
b.WriteString(s)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// A DecodeError 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 and 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 it programmatically with errors.AsType:
|
||||
// happened. Read the path programmatically with errors.AsType:
|
||||
//
|
||||
// if de, ok := errors.AsType[*interpres.DecodeError](err); ok {
|
||||
// fmt.Println(de.Path, de.Err)
|
||||
// fmt.Println(de.Path.String(), de.Err)
|
||||
// }
|
||||
type DecodeError struct {
|
||||
// Path is the key path from the document root, outermost key first.
|
||||
Path []string
|
||||
Path Path
|
||||
// Err is the failure at that path.
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *DecodeError) Error() string { return e.Path[0] + ": " + e.Err.Error() }
|
||||
func (e *DecodeError) Error() string {
|
||||
msg := strings.TrimPrefix(e.Err.Error(), "interpres: ")
|
||||
if p := e.Path.String(); p != "" {
|
||||
return "interpres: " + p + ": " + msg
|
||||
}
|
||||
return "interpres: " + msg
|
||||
}
|
||||
|
||||
// Unwrap returns the failure the path points at.
|
||||
func (e *DecodeError) Unwrap() error { return e.Err }
|
||||
|
||||
// newDecodeError wraps err with one path segment. The rest of the path comes
|
||||
// from the DecodeError err already carries, if any: the decoder wraps each
|
||||
// key and index on its way down, so the innermost wrap holds the deepest
|
||||
// segments and each outer wrap prepends one.
|
||||
// key and index on its way down, so the wrap flattens that inner error's
|
||||
// segments onto the front and keeps the failure it pointed at, leaving one
|
||||
// path and one failure to render.
|
||||
func newDecodeError(key string, err error) *DecodeError {
|
||||
path := make([]string, 0, 4)
|
||||
path := make(Path, 0, 4)
|
||||
path = append(path, key)
|
||||
if de, ok := errors.AsType[*DecodeError](err); ok {
|
||||
path = append(path, de.Path...)
|
||||
err = de.Err
|
||||
}
|
||||
return &DecodeError{Path: path, Err: err}
|
||||
}
|
||||
@@ -110,12 +138,18 @@ func newDecodeError(key string, err error) *DecodeError {
|
||||
// unchanged by the type; read it programmatically with errors.AsType.
|
||||
type EncodeError struct {
|
||||
// Path is the key path of the failing value.
|
||||
Path string
|
||||
Path Path
|
||||
// Err is the failure at that path.
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *EncodeError) Error() string { return "interpres: " + e.Path + ": " + e.Err.Error() }
|
||||
func (e *EncodeError) Error() string {
|
||||
msg := strings.TrimPrefix(e.Err.Error(), "interpres: ")
|
||||
if p := e.Path.String(); p != "" {
|
||||
return "interpres: " + p + ": " + msg
|
||||
}
|
||||
return "interpres: " + msg
|
||||
}
|
||||
|
||||
// Unwrap returns the failure the path points at.
|
||||
func (e *EncodeError) Unwrap() error { return e.Err }
|
||||
|
||||
Reference in New Issue
Block a user