Skip to content

Commit 53a1448

Browse files
authored
Remove an old TODO, minor refactoring (#744)
- Remove an old TODO, clarify the code - Avoid an explicit cast in `GeneratedMessage.mergeFromJson` - Update reviver argument type in `jsonDecode` calls to avoid any potential runtime type checks - Remove a dead code - Simplify oneof handling
1 parent 19c4eb6 commit 53a1448

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

protobuf/lib/src/protobuf/builder_info.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class BuilderInfo {
2727
/// Mapping from [FieldInfo.name]s to [FieldInfo]s.
2828
final Map<String, FieldInfo> byName = <String, FieldInfo>{};
2929

30-
/// Mapping from [FieldInfo.tagNumber]s to the corresponding `oneof` indices
31-
/// (if any).
30+
/// Mapping from `oneof` field [FieldInfo.tagNumber]s to the their indices in
31+
/// [_FieldSet._oneofCases].
3232
final Map<int, int> oneofs = <int, int>{};
3333

3434
/// Whether the message has extension fields.
@@ -301,12 +301,12 @@ class BuilderInfo {
301301
: qualifiedMessageName.substring(lastDot + 1);
302302
}
303303

304-
List<FieldInfo> _computeSortedByTag() {
305-
// TODO(skybrian): perhaps the code generator should insert the FieldInfos
306-
// in tag number order, to avoid sorting them?
307-
return List<FieldInfo>.from(fieldInfo.values, growable: false)
308-
..sort((FieldInfo a, FieldInfo b) => a.tagNumber.compareTo(b.tagNumber));
309-
}
304+
List<FieldInfo> _computeSortedByTag() =>
305+
// Code generator inserts fields in tag order, but it's possible for
306+
// user-written code to insert unordered.
307+
List<FieldInfo>.from(fieldInfo.values, growable: false)
308+
..sort(
309+
(FieldInfo a, FieldInfo b) => a.tagNumber.compareTo(b.tagNumber));
310310

311311
GeneratedMessage _makeEmptyMessage(
312312
int tagNumber, ExtensionRegistry? extensionRegistry) {

protobuf/lib/src/protobuf/extension_field_set.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class _ExtensionFieldSet {
1212

1313
_ExtensionFieldSet(this._parent);
1414

15-
Extension? _getInfoOrNull(int? tagNumber) => _info[tagNumber];
15+
Extension? _getInfoOrNull(int tagNumber) => _info[tagNumber];
1616

1717
dynamic _getFieldOrDefault(Extension fi) {
1818
if (fi.isRepeated) return _getList(fi);

protobuf/lib/src/protobuf/field_set.dart

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class _FieldSet {
7070
int? get _memoizedHashCode =>
7171
_frozenState is int ? _frozenState as int : null;
7272

73-
// Maps a oneof decl index to the tag number which is currently set. If the
74-
// index is not present, the oneof field is unset.
73+
/// Maps a `oneof` field index to the tag number which is currently set. If
74+
/// the index is not present, the oneof field is unset.
7575
final Map<int, int>? _oneofCases;
7676

7777
_FieldSet(this._message, BuilderInfo meta, this._eventPlugin)
@@ -217,24 +217,26 @@ class _FieldSet {
217217
return _extensions?._hasField(tagNumber) ?? false;
218218
}
219219

220-
void _clearField(int? tagNumber) {
220+
void _clearField(int tagNumber) {
221221
_ensureWritable();
222222
final meta = _meta;
223-
var fi = _nonExtensionInfo(meta, tagNumber);
223+
final fi = _nonExtensionInfo(meta, tagNumber);
224+
224225
if (fi != null) {
225-
// clear a non-extension field
226+
assert(tagNumber == fi.tagNumber);
227+
228+
// Clear a non-extension field
226229
final eventPlugin = _eventPlugin;
227230
if (eventPlugin != null && eventPlugin.hasObservers) {
228231
eventPlugin.beforeClearField(fi);
229232
}
230233
_values[fi.index!] = null;
231234

232-
if (meta.oneofs.containsKey(fi.tagNumber)) {
233-
_oneofCases!.remove(meta.oneofs[fi.tagNumber]);
235+
final oneofIndex = meta.oneofs[tagNumber];
236+
if (oneofIndex != null) {
237+
_oneofCases![oneofIndex] = 0;
234238
}
235239

236-
var oneofIndex = meta.oneofs[fi.tagNumber];
237-
if (oneofIndex != null) _oneofCases![oneofIndex] = 0;
238240
return;
239241
}
240242

@@ -328,10 +330,13 @@ class _FieldSet {
328330

329331
/// Sets a non-extended field and fires events.
330332
void _setNonExtensionFieldUnchecked(BuilderInfo meta, FieldInfo fi, value) {
331-
var tag = fi.tagNumber;
332-
var oneofIndex = meta.oneofs[tag];
333+
final tag = fi.tagNumber;
334+
final oneofIndex = meta.oneofs[tag];
333335
if (oneofIndex != null) {
334-
_clearField(_oneofCases![oneofIndex]);
336+
final currentOneofTag = _oneofCases![oneofIndex];
337+
if (currentOneofTag != null) {
338+
_clearField(currentOneofTag);
339+
}
335340
_oneofCases![oneofIndex] = tag;
336341
}
337342

@@ -488,7 +493,10 @@ class _FieldSet {
488493
var oneofIndex = meta.oneofs[tag];
489494

490495
if (oneofIndex != null) {
491-
_clearField(_oneofCases![oneofIndex]);
496+
final currentOneofTag = _oneofCases![oneofIndex];
497+
if (currentOneofTag != null) {
498+
_clearField(currentOneofTag);
499+
}
492500
_oneofCases![oneofIndex] = tag;
493501
}
494502
_values[index] = value;

protobuf/lib/src/protobuf/generated_message.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ abstract class GeneratedMessage {
279279
/// This is a slight regression on the Dart VM.
280280
/// TODO(skybrian) we could skip the reviver if we're running
281281
/// on the Dart VM for a slight speedup.
282-
final jsonMap =
283-
jsonDecode(data, reviver: _emptyReviver) as Map<String, dynamic>;
282+
final Map<String, dynamic> jsonMap =
283+
jsonDecode(data, reviver: _emptyReviver);
284284
_mergeFromJsonMap(_fieldSet, jsonMap, extensionRegistry);
285285
}
286286

287-
static dynamic _emptyReviver(k, v) => v;
287+
static Object? _emptyReviver(Object? k, Object? v) => v;
288288

289289
/// Merges field values from a JSON object represented as a Dart map.
290290
///

protobuf/lib/src/protobuf/proto3_json.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,6 @@ void _mergeFromProto3Json(
297297
throw context.parseException(
298298
'Wrong boolean key, should be one of ("true", "false")', key);
299299
}
300-
// ignore: dead_code
301-
throw StateError('(Should have been) unreachable statement');
302300
case PbFieldType._STRING_BIT:
303301
return key;
304302
case PbFieldType._UINT64_BIT:

0 commit comments

Comments
 (0)