Type reflect.Type // type of Go value it could not be assigned to
Offset int64 // error occurred after reading Offset bytes
Struct string // name of the struct type containing the field
- Field string // the full path from root node to the field
+ Field string // the full path from root node to the field, include embedded struct
}
func (e *UnmarshalTypeError) Error() string {
if f != nil {
subv = v
destring = f.quoted
- for _, i := range f.index {
+ if d.errorContext == nil {
+ d.errorContext = new(errorContext)
+ }
+ for i, ind := range f.index {
if subv.Kind() == reflect.Pointer {
if subv.IsNil() {
// If a struct embeds a pointer to an unexported type,
}
subv = subv.Elem()
}
- subv = subv.Field(i)
- }
- if d.errorContext == nil {
- d.errorContext = new(errorContext)
+ if i < len(f.index)-1 {
+ d.errorContext.FieldStack = append(
+ d.errorContext.FieldStack,
+ subv.Type().Field(ind).Name,
+ )
+ }
+ subv = subv.Field(ind)
}
- d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name)
d.errorContext.Struct = t
+ d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name)
} else if d.disallowUnknownFields {
d.saveError(fmt.Errorf("json: unknown field %q", key))
}
},
},
+ {
+ CaseName: Name(""),
+ in: `{"Level1a": "hello"}`,
+ ptr: new(Top),
+ err: &UnmarshalTypeError{
+ Value: "string",
+ Struct: "Top",
+ Field: "Embed0a.Level1a",
+ Type: reflect.TypeFor[int](),
+ Offset: 10,
+ },
+ },
+
// issue 15146.
// invalid inputs in wrongStringTests below.
{CaseName: Name(""), in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true},