internal/impl: treat a nil oneof wrapper as if it were unset
The old implementation had the behavior where a nil wrapper value:
m := new(foopb.Message)
m.OneofField = (*foopb.Message_OneofUint32)(nil)
was functionally equivalent to it being directly set to nil:
m := new(foopb.Message)
m.OneofField = nil
preserve this semantic in both the table-drive implementation
and the reflection implementation.
Change-Id: Ie44d51e044d4822e61d0e646fbc44aa8d9b90c1f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189559
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/proto/encode_test.go b/proto/encode_test.go
index 466d3d1..b5af461 100644
--- a/proto/encode_test.go
+++ b/proto/encode_test.go
@@ -13,6 +13,7 @@
"google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/proto"
+ testpb "google.golang.org/protobuf/internal/testprotos/test"
test3pb "google.golang.org/protobuf/internal/testprotos/test3"
)
@@ -130,7 +131,7 @@
}
}
-func TestMarshalAppend(t *testing.T) {
+func TestEncodeAppend(t *testing.T) {
want := []byte("prefix")
got := append([]byte(nil), want...)
got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
@@ -143,3 +144,14 @@
t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
}
}
+
+func TestEncodeOneofNilWrapper(t *testing.T) {
+ m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
+ b, err := proto.Marshal(m)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(b) > 0 {
+ t.Errorf("Marshal return non-empty, want empty")
+ }
+}