Skip to content

Commit 4e1db73

Browse files
committed
JSON unmarshaler returns UnknownFieldError error struct for unknown fields.
1 parent 4bd1920 commit 4e1db73

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

jsonpb/jsonpb.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,20 @@ func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v refle
510510
return out.err
511511
}
512512

513+
// UnknownFieldError is an error if an unknown field was encountered
514+
// during unmarshaling.
515+
type UnknownFieldError struct {
516+
// Field is the name of the JSON field.
517+
Field string
518+
519+
// TargetType is the type attempted unmarshaled.
520+
TargetType reflect.Type
521+
}
522+
523+
func (u UnknownFieldError) Error() string {
524+
return fmt.Sprintf("unknown field %q in %v", u.Field, u.TargetType)
525+
}
526+
513527
// Unmarshaler is a configurable object for converting from a JSON
514528
// representation to a protocol buffer object.
515529
type Unmarshaler struct {
@@ -701,7 +715,10 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
701715
f = fname
702716
break
703717
}
704-
return fmt.Errorf("unknown field %q in %v", f, targetType)
718+
return UnknownFieldError{
719+
Field: f,
720+
TargetType: targetType,
721+
}
705722
}
706723
return nil
707724
}

0 commit comments

Comments
 (0)