Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(654)

Unified Diff: lib/src/protobuf/field_set.dart

Issue 1852983002: Fix issues with protobuf equality comparisons (Closed) Base URL: git@github.com:dart-lang/dart-protobuf.git@master
Patch Set: tweak _deepEquals, improve tests Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/src/protobuf/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/protobuf/field_set.dart
diff --git a/lib/src/protobuf/field_set.dart b/lib/src/protobuf/field_set.dart
index db5d9189c931021ad816bc199a55f4891af1d842..e2481adb6f6f0eaeeb6fea985052ec5314f5cf33 100644
--- a/lib/src/protobuf/field_set.dart
+++ b/lib/src/protobuf/field_set.dart
@@ -307,7 +307,9 @@ class _FieldSet {
bool _equals(_FieldSet o) {
if (_meta != o._meta) return false;
- if (!_areListsEqual(_values, o._values)) return false;
+ for (var i = 0; i < _values.length; i++) {
+ if (!_equalFieldValues(_values[i], o._values[i])) return false;
+ }
if (!_hasExtensions || !_extensions._hasValues) {
// Check if other extensions are logically empty.
@@ -331,6 +333,28 @@ class _FieldSet {
return true;
}
+ bool _equalFieldValues(left, right) {
+ if (left != null && right != null) return _deepEquals(left, right);
+
+ var val = left ?? right;
+
+ // Two uninitialized fields are equal.
+ if (val == null) return true;
+
+ // One field is null. We are comparing an initialized field
+ // with its default value.
+
+ // An empty repeated field is the same as uninitialized.
+ // This is because accessing a repeated field automatically creates it.
+ // We don't want reading a field to change equality comparisons.
+ if (val is List && val.isEmpty) return true;
+
+ // For now, initialized and uninitialized fields are different.
+ // TODO(skybrian) consider other cases; should we compare with the
+ // default value or not?
+ return false;
+ }
+
/// Calculates a hash code based on the contents of the protobuf.
///
/// The hash may change when any field changes (recursively).
« no previous file with comments | « no previous file | lib/src/protobuf/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698