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>
diff --git a/cmd/protoc-gen-go/internal_gengo/well_known_types.go b/cmd/protoc-gen-go/internal_gengo/well_known_types.go
index 8b12823..7f6ca2c 100644
--- a/cmd/protoc-gen-go/internal_gengo/well_known_types.go
+++ b/cmd/protoc-gen-go/internal_gengo/well_known_types.go
@@ -54,13 +54,13 @@
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 @@
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()
diff --git a/types/known/anypb/any.pb.go b/types/known/anypb/any.pb.go
index 061a9ee..47bc555 100644
--- a/types/known/anypb/any.pb.go
+++ b/types/known/anypb/any.pb.go
@@ -194,13 +194,13 @@
//
// 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)
}
diff --git a/types/known/fieldmaskpb/field_mask.pb.go b/types/known/fieldmaskpb/field_mask.pb.go
index ae1be0e..da32aa5 100644
--- a/types/known/fieldmaskpb/field_mask.pb.go
+++ b/types/known/fieldmaskpb/field_mask.pb.go
@@ -307,7 +307,7 @@
// 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