Debian Codegraph @

internal/errors/type_error.go

raw
1 //go:build go1.27 && goexperiment.jsonv2
2
3 package errors
4
5 import "strings"
6
7 // typeErrorMessage is the message of encoding/json of Go 1.27, which is made of encoding/json/v2: Struct is the
8 // root type, and Field the path from it to the value, whose array elements are their indices. A path which ends
9 // with an index is not called a struct field, and the cause of the error, Err, follows the message.
10 func typeErrorMessage(e *UnmarshalTypeError) string {
11 var s string
12 if e.Struct != "" || e.Field != "" {
13 intoWhat := "Go struct field "
14 last := e.Field[strings.LastIndexByte(e.Field, '.')+1:]
15 if last != "" && strings.TrimRight(last, "0123456789") == "" {
16 intoWhat = ""
17 }
18 s = "json: cannot unmarshal " + e.Value + " into " + intoWhat + e.Struct + "." + e.Field + " of type " + e.Type.String()
19 } else {
20 s = "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
21 }
22 if e.Err != nil {
23 s += ": " + e.Err.Error()
24 }
25 return s
26 }