types/known: minor adjustments to error handling

Trivial adjustments to error handling to reduce differences between
the generated helpers and the legacy ptypes package.

Change-Id: Ib3022eb50d9a9c0906b7809fe7e8011ee9399b0a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/238009
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2020-06-16 23:52:22 -07:00
parent a0d1c75183
commit 5f5c066b7f
3 changed files with 8 additions and 8 deletions

View File

@ -54,13 +54,13 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("//")
g.P("// If no options are specified, call src.UnmarshalTo instead.")
g.P("func UnmarshalTo(src *Any, dst ", protoPackage.Ident("Message"), ", opts ", protoPackage.Ident("UnmarshalOptions"), ") error {")
g.P(" if src.GetTypeUrl() == \"\" {")
g.P(" return ", protoimplPackage.Ident("X"), ".NewError(\"invalid empty type URL\")")
g.P(" if src == nil {")
g.P(" return ", protoimplPackage.Ident("X"), ".NewError(\"invalid nil source message\")")
g.P(" }")
g.P(" if !src.MessageIs(dst) {")
g.P(" got := dst.ProtoReflect().Descriptor().FullName()")
g.P(" want := src.MessageName()")
g.P(" return ", protoimplPackage.Ident("X"), ".NewError(\"mismatching message types: got %q, want %q\", got, want)")
g.P(" return ", protoimplPackage.Ident("X"), ".NewError(\"mismatched message type: got %q, want %q\", got, want)")
g.P(" }")
g.P(" return opts.Unmarshal(src.GetValue(), dst)")
g.P("}")
@ -608,7 +608,7 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("// It reports false for a nil FieldMask.")
g.P("func (x *FieldMask) IsValid(m ", protoPackage.Ident("Message"), ") bool {")
g.P(" paths := x.GetPaths()")
g.P(" return numValidPaths(m, paths) == len(paths)")
g.P(" return x != nil && numValidPaths(m, paths) == len(paths)")
g.P("}")
g.P()

View File

@ -194,13 +194,13 @@ func MarshalFrom(dst *Any, src proto.Message, opts proto.MarshalOptions) error {
//
// If no options are specified, call src.UnmarshalTo instead.
func UnmarshalTo(src *Any, dst proto.Message, opts proto.UnmarshalOptions) error {
if src.GetTypeUrl() == "" {
return protoimpl.X.NewError("invalid empty type URL")
if src == nil {
return protoimpl.X.NewError("invalid nil source message")
}
if !src.MessageIs(dst) {
got := dst.ProtoReflect().Descriptor().FullName()
want := src.MessageName()
return protoimpl.X.NewError("mismatching message types: got %q, want %q", got, want)
return protoimpl.X.NewError("mismatched message type: got %q, want %q", got, want)
}
return opts.Unmarshal(src.GetValue(), dst)
}

View File

@ -307,7 +307,7 @@ func Intersect(mx *FieldMask, my *FieldMask, ms ...*FieldMask) *FieldMask {
// It reports false for a nil FieldMask.
func (x *FieldMask) IsValid(m proto.Message) bool {
paths := x.GetPaths()
return numValidPaths(m, paths) == len(paths)
return x != nil && numValidPaths(m, paths) == len(paths)
}
// Append appends a list of paths to the mask and verifies that each one