internal/impl: avoid preserving presence on proto3 bytes field
When setting a proto3 bytes field to an empty, non-nil bytes slice,
just store a nil slice in the underderlying storage.
This is done to avoid presenting the illusion to the user that
presence is preserved for proto3.
Updates golang/protobuf#896
Change-Id: I1b97bedd547d336863c65d9418d8f07edf69ccd6
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185577
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/internal/impl/message_test.go b/internal/impl/message_test.go
index f2fd290..4132fd1 100644
--- a/internal/impl/message_test.go
+++ b/internal/impl/message_test.go
@@ -270,6 +270,11 @@
}).ProtoReflect()},
clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
equalMessage{new(ScalarProto2).ProtoReflect()},
+
+ // Setting a bytes field nil empty bytes should preserve presence.
+ setFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
+ getFields{10: V([]byte{}), 11: V([]byte(nil)), 21: V([]byte{}), 22: V([]byte(nil))},
+ hasFields{10: true, 11: true, 21: true, 22: true},
})
// Test read-only operations on nil message.
@@ -391,6 +396,11 @@
hasFields{6: false, 7: false},
setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
hasFields{6: true, 7: true},
+
+ // Setting a bytes field to non-nil empty bytes should not preserve presence.
+ setFields{10: V([]byte{}), 11: V([]byte{}), 21: V([]byte{}), 22: V([]byte{})},
+ getFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
+ hasFields{10: false, 11: false, 21: false, 22: false},
})
// Test read-only operations on nil message.
@@ -1200,7 +1210,6 @@
}
}),
cmpopts.EquateNaNs(),
- cmpopts.EquateEmpty(),
}
func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {