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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | lib/src/protobuf/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of protobuf; 5 part of protobuf;
6 6
7 final _emptyList = new List.unmodifiable([]); 7 final _emptyList = new List.unmodifiable([]);
8 8
9 /// All the data in a GeneratedMessage. 9 /// All the data in a GeneratedMessage.
10 /// 10 ///
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 _eventPlugin.beforeClearField(fi); 300 _eventPlugin.beforeClearField(fi);
301 } 301 }
302 } 302 }
303 } 303 }
304 if (_values.isNotEmpty) _values.fillRange(0, _values.length, null); 304 if (_values.isNotEmpty) _values.fillRange(0, _values.length, null);
305 if (_hasExtensions) _extensions._clearValues(); 305 if (_hasExtensions) _extensions._clearValues();
306 } 306 }
307 307
308 bool _equals(_FieldSet o) { 308 bool _equals(_FieldSet o) {
309 if (_meta != o._meta) return false; 309 if (_meta != o._meta) return false;
310 if (!_areListsEqual(_values, o._values)) return false; 310 for (var i = 0; i < _values.length; i++) {
311 if (!_equalFieldValues(_values[i], o._values[i])) return false;
312 }
311 313
312 if (!_hasExtensions || !_extensions._hasValues) { 314 if (!_hasExtensions || !_extensions._hasValues) {
313 // Check if other extensions are logically empty. 315 // Check if other extensions are logically empty.
314 // (Don't create them unnecessarily.) 316 // (Don't create them unnecessarily.)
315 if (o._hasExtensions && o._extensions._hasValues) { 317 if (o._hasExtensions && o._extensions._hasValues) {
316 return false; 318 return false;
317 } 319 }
318 } else { 320 } else {
319 if (!_extensions._equalValues(o._extensions)) return false; 321 if (!_extensions._equalValues(o._extensions)) return false;
320 } 322 }
321 323
322 if (_unknownFields == null || _unknownFields.isEmpty) { 324 if (_unknownFields == null || _unknownFields.isEmpty) {
323 // Check if other unknown fields is logically empty. 325 // Check if other unknown fields is logically empty.
324 // (Don't create them unnecessarily.) 326 // (Don't create them unnecessarily.)
325 if (o._unknownFields != null && o._unknownFields.isNotEmpty) return false; 327 if (o._unknownFields != null && o._unknownFields.isNotEmpty) return false;
326 } else { 328 } else {
327 // Check if the other unknown fields has the same fields. 329 // Check if the other unknown fields has the same fields.
328 if (_unknownFields != o._unknownFields) return false; 330 if (_unknownFields != o._unknownFields) return false;
329 } 331 }
330 332
331 return true; 333 return true;
332 } 334 }
333 335
336 bool _equalFieldValues(left, right) {
337 if (left != null && right != null) return _deepEquals(left, right);
338
339 var val = left ?? right;
340
341 // Two uninitialized fields are equal.
342 if (val == null) return true;
343
344 // One field is null. We are comparing an initialized field
345 // with its default value.
346
347 // An empty repeated field is the same as uninitialized.
348 // This is because accessing a repeated field automatically creates it.
349 // We don't want reading a field to change equality comparisons.
350 if (val is List && val.isEmpty) return true;
351
352 // For now, initialized and uninitialized fields are different.
353 // TODO(skybrian) consider other cases; should we compare with the
354 // default value or not?
355 return false;
356 }
357
334 /// Calculates a hash code based on the contents of the protobuf. 358 /// Calculates a hash code based on the contents of the protobuf.
335 /// 359 ///
336 /// The hash may change when any field changes (recursively). 360 /// The hash may change when any field changes (recursively).
337 /// Therefore, protobufs used as map keys shouldn't be changed. 361 /// Therefore, protobufs used as map keys shouldn't be changed.
338 int get _hashCode { 362 int get _hashCode {
339 int hash; 363 int hash;
340 364
341 void hashEnumList(PbList enums) { 365 void hashEnumList(PbList enums) {
342 enums.forEach((ProtobufEnum enm) { 366 enums.forEach((ProtobufEnum enm) {
343 hash = (31 * hash + enm.value) & 0x3fffffff; 367 hash = (31 * hash + enm.value) & 0x3fffffff;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 void _appendInvalidFields(List<String> problems, String prefix) { 535 void _appendInvalidFields(List<String> problems, String prefix) {
512 if (!_hasRequiredFields) return; 536 if (!_hasRequiredFields) return;
513 for (var fi in _infos) { 537 for (var fi in _infos) {
514 var value = _values[fi.index]; 538 var value = _values[fi.index];
515 fi._appendInvalidFields(problems, value, prefix); 539 fi._appendInvalidFields(problems, value, prefix);
516 } 540 }
517 // TODO(skybrian): search extensions as well 541 // TODO(skybrian): search extensions as well
518 // https://github.com/dart-lang/dart-protobuf/issues/46 542 // https://github.com/dart-lang/dart-protobuf/issues/46
519 } 543 }
520 } 544 }
OLDNEW
« 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