Skip to content

Commit 7fcd48b

Browse files
authored
Sync some of the internal changes (#1084)
- Remove redundant `break`s in `switch` statements. - Move type annotations from variables to lists: `final T x = [...]` to `final x = <T>[...]` - Copy a dart2js optimization when accessing `GeneratedMessage._fieldSet` - Sort exports in `internal.dart`.
1 parent 3ed04a9 commit 7fcd48b

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

protobuf/lib/src/protobuf/generated_message.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ typedef ValueOfFunc = ProtobufEnum? Function(int value);
2525
/// `GeneratedMessage_reservedNames` and should be unlikely to be used in a
2626
/// proto file.
2727
abstract class GeneratedMessage {
28-
FieldSet? __fieldSet;
29-
30-
@pragma('dart2js:tryInline')
31-
FieldSet get _fieldSet => __fieldSet!;
28+
// The pragma tells dart2js that the late checks for `__fieldSet` are
29+
// unnecessary. The field is always initialized before use, but dart2js can't
30+
// see that. One problem is that `this.info_` is called before the
31+
// initializing assignment, and potentially one of the many overrides for
32+
// `get:info_` could access the field before it is initialized, or call one of
33+
// the methods that accesses the field. The code generated for the `get:info_`
34+
// methods does not do this, but it is hard to determine from first
35+
// principles.
36+
@pragma('dart2js:late:trust')
37+
late final FieldSet __fieldSet;
38+
39+
@pragma('dart2js:prefer-inline')
40+
FieldSet get _fieldSet => __fieldSet;
3241

3342
GeneratedMessage() {
3443
__fieldSet = FieldSet(this, info_);

protobuf/lib/src/protobuf/internal.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import 'type_registry.dart';
2727
import 'utils.dart';
2828

2929
export 'annotations.dart' show TagNumber, GrpcServiceName;
30+
export 'exceptions.dart' show InvalidProtocolBufferException;
3031
export 'pb_list.dart' show PbList;
3132
export 'pb_map.dart' show PbMap;
3233
export 'type_registry.dart' show TypeRegistry;
33-
export 'exceptions.dart' show InvalidProtocolBufferException;
3434

3535
part 'builder_info.dart';
3636
part 'coded_buffer.dart';

protobuf/lib/src/protobuf/json/json.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,16 @@ dynamic _convertJsonValue(
276276
}
277277
}
278278
expectedType = 'bool (true, false, "true", "false", 1, 0)';
279-
break;
280279
case PbFieldType.BYTES_BIT:
281280
if (value is String) {
282281
return base64Decode(value);
283282
}
284283
expectedType = 'Base64 String';
285-
break;
286284
case PbFieldType.STRING_BIT:
287285
if (value is String) {
288286
return value;
289287
}
290288
expectedType = 'String';
291-
break;
292289
case PbFieldType.FLOAT_BIT:
293290
case PbFieldType.DOUBLE_BIT:
294291
// Allow quoted values, although we don't emit them.
@@ -300,7 +297,6 @@ dynamic _convertJsonValue(
300297
return double.parse(value);
301298
}
302299
expectedType = 'num or stringified num';
303-
break;
304300
case PbFieldType.ENUM_BIT:
305301
// Allow quoted values, although we don't emit them.
306302
if (value is String) {
@@ -313,14 +309,12 @@ dynamic _convertJsonValue(
313309
return meta.decodeEnum(tagNumber, registry, value);
314310
}
315311
expectedType = 'int or stringified int';
316-
break;
317312
case PbFieldType.INT32_BIT:
318313
case PbFieldType.SINT32_BIT:
319314
case PbFieldType.SFIXED32_BIT:
320315
if (value is int) return value;
321316
if (value is String) return int.parse(value);
322317
expectedType = 'int or stringified int';
323-
break;
324318
case PbFieldType.UINT32_BIT:
325319
case PbFieldType.FIXED32_BIT:
326320
int? validatedValue;
@@ -331,7 +325,6 @@ dynamic _convertJsonValue(
331325
}
332326
if (validatedValue != null) return validatedValue;
333327
expectedType = 'int or stringified int';
334-
break;
335328
case PbFieldType.INT64_BIT:
336329
case PbFieldType.SINT64_BIT:
337330
case PbFieldType.UINT64_BIT:
@@ -340,7 +333,6 @@ dynamic _convertJsonValue(
340333
if (value is int) return Int64(value);
341334
if (value is String) return Int64.parseInt(value);
342335
expectedType = 'int or stringified int';
343-
break;
344336
case PbFieldType.GROUP_BIT:
345337
case PbFieldType.MESSAGE_BIT:
346338
if (value is Map) {
@@ -350,7 +342,6 @@ dynamic _convertJsonValue(
350342
return subMessage;
351343
}
352344
expectedType = 'nested message or group';
353-
break;
354345
default:
355346
throw ArgumentError('Unknown type $fieldType');
356347
}

protobuf/lib/src/protobuf/json/json_web.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ JSObject _writeToRawJs(FieldSet fs) {
174174
}
175175
final unknownJsonData = fs.unknownJsonData;
176176
if (unknownJsonData != null) {
177-
unknownJsonData.forEach((key, value) {
177+
unknownJsonData.forEach((String key, Object? value) {
178178
result.setProperty(key.toJS, value.jsify());
179179
});
180180
}

protobuf/lib/src/protobuf/mixins/well_known.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ mixin ListValueMixin implements GeneratedMessage {
537537
if (json is List) {
538538
final subBuilder = message.info_.subBuilder(_valueFieldTagNumber)!;
539539
for (var i = 0; i < json.length; i++) {
540-
final Object element = json[i];
540+
final Object? element = json[i];
541541
final v = subBuilder() as ValueMixin;
542542
context.addListIndex(i);
543543
ValueMixin.fromProto3JsonHelper(v, element, typeRegistry, context);

protobuf/lib/src/protobuf/unknown_field_set.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ class UnknownFieldSetField {
404404
}
405405

406406
UnknownFieldSetField _deepCopy() {
407-
final List<List<int>> newLengthDelimited = List.from(_lengthDelimited);
408-
final List<Int64> newVarints = List.from(_varints);
409-
final List<int> newFixed32s = List.from(_fixed32s);
410-
final List<Int64> newFixed64s = List.from(_fixed64s);
407+
final newLengthDelimited = List<List<int>>.from(_lengthDelimited);
408+
final newVarints = List<Int64>.from(_varints);
409+
final newFixed32s = List<int>.from(_fixed32s);
410+
final newFixed64s = List<Int64>.from(_fixed64s);
411411

412-
final List<UnknownFieldSet> newGroups = [];
412+
final newGroups = <UnknownFieldSet>[];
413413
for (final group in _groups) {
414414
newGroups.add(group._deepCopy());
415415
}

0 commit comments

Comments
 (0)