encoding/protojson, encoding/prototext: set wire unmarshal resolver

The text and JSON encodings for the google.protobuf.Any well-known type
require a call to proto.Unmarshal. Plumb through the resolver from the
UnmarshalOptions.

Change-Id: Iccc1a9d56acd9dd214f2b289216bd50acc2ef074
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/182980
Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
Damien Neil 2019-06-19 10:03:37 -07:00
parent 3631e225f8
commit 95faac20ee
3 changed files with 4 additions and 2 deletions

View File

@ -39,6 +39,7 @@ type MarshalOptions struct {
// Resolver is used for looking up types when expanding google.protobuf.Any
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
Resolver interface {
protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
}

View File

@ -178,12 +178,12 @@ func (o MarshalOptions) marshalAny(m pref.Message) error {
}
em := emt.New()
// TODO: Need to set types registry in binary unmarshaling.
// TODO: If binary unmarshaling returns required not set error, need to
// return another required not set error that contains both the path to this
// field and the path inside the embedded message.
err = proto.UnmarshalOptions{
AllowPartial: o.AllowPartial,
Resolver: o.Resolver,
}.Unmarshal(valueVal.Bytes(), em.Interface())
if !nerr.Merge(err) {
return errors.New("%s: unable to unmarshal %q: %v", m.Descriptor().FullName(), typeURL, err)

View File

@ -42,6 +42,7 @@ type MarshalOptions struct {
// Resolver is used for looking up types when expanding google.protobuf.Any
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
Resolver interface {
protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
}
}
@ -353,12 +354,12 @@ func (o MarshalOptions) marshalAny(m pref.Message) (text.Value, error) {
return text.Value{}, err
}
em := emt.New().Interface()
// TODO: Need to set types registry in binary unmarshaling.
// TODO: If binary unmarshaling returns required not set error, need to
// return another required not set error that contains both the path to this
// field and the path inside the embedded message.
err = proto.UnmarshalOptions{
AllowPartial: o.AllowPartial,
Resolver: o.Resolver,
}.Unmarshal(value.Bytes(), em)
if !nerr.Merge(err) {
return text.Value{}, err