Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion protobuf/lib/protobuf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ library protobuf;

import 'dart:collection' show ListBase, MapBase;
import 'dart:convert'
show base64Decode, base64Encode, jsonEncode, jsonDecode, Utf8Codec;
show
base64Decode,
base64Encode,
jsonEncode,
jsonDecode,
Utf8Codec,
Utf8Encoder;
import 'dart:math' as math;
import 'dart:typed_data' show TypedData, Uint8List, ByteData, Endian;

Expand Down
11 changes: 8 additions & 3 deletions protobuf/lib/src/protobuf/coded_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void _mergeFromCodedBufferReader(BuilderInfo meta, _FieldSet fs,
}

void _readPackable(BuilderInfo meta, _FieldSet fs, CodedBufferReader input,
int wireType, FieldInfo fi, Function readFunc) {
int wireType, FieldInfo fi, Object Function() readFunc) {
void readToList(List list) => list.add(readFunc());
_readPackableToList(meta, fs, input, wireType, fi, readToList);
}
Expand All @@ -222,8 +222,13 @@ void _readPackableToListEnum(
_readPackableToList(meta, fs, input, wireType, fi, readToList);
}

void _readPackableToList(BuilderInfo meta, _FieldSet fs,
CodedBufferReader input, int wireType, FieldInfo fi, Function readToList) {
void _readPackableToList(
BuilderInfo meta,
_FieldSet fs,
CodedBufferReader input,
int wireType,
FieldInfo fi,
void Function(List<Object?>) readToList) {
var list = fs._ensureRepeatedField(meta, fi);

if (wireType == WIRETYPE_LENGTH_DELIMITED) {
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CodedBufferReader {

bool isAtEnd() => _bufferPos >= _currentLimit;

void _withLimit(int byteLimit, callback) {
void _withLimit(int byteLimit, void Function() callback) {
if (byteLimit < 0) {
throw ArgumentError(
'CodedBufferReader encountered an embedded string or message'
Expand Down
10 changes: 7 additions & 3 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ class CodedBufferWriter {
_commitChunk(true);
}

void writeField(int fieldNumber, int fieldType, fieldValue) {
void writeField(int fieldNumber, int fieldType, dynamic fieldValue) {
final valueType = fieldType & ~0x07;

if ((fieldType & PbFieldType._PACKED_BIT) != 0) {
if (!fieldValue.isEmpty) {
if (fieldValue.isNotEmpty) {
_writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
final mark = _startLengthDelimited();
for (var value in fieldValue) {
Expand Down Expand Up @@ -340,7 +340,7 @@ class CodedBufferWriter {
value is TypedData ? value : Uint8List.fromList(value));
break;
case PbFieldType._STRING_BIT:
_writeBytesNoTag(_utf8.encode(value));
_writeBytesNoTag(const Utf8Encoder().convert(value));
break;
case PbFieldType._DOUBLE_BIT:
_writeDouble(value);
Expand All @@ -349,9 +349,11 @@ class CodedBufferWriter {
_writeFloat(value);
break;
case PbFieldType._ENUM_BIT:
// `value` is [ProtobufEnum].
_writeVarint32(value.value & 0xffffffff);
break;
case PbFieldType._GROUP_BIT:
// `value` is [GeneratedMessage] or [UnknownFieldSet].
value.writeToCodedBufferWriter(this);
break;
case PbFieldType._INT32_BIT:
Expand Down Expand Up @@ -386,13 +388,15 @@ class CodedBufferWriter {
break;
case PbFieldType._MESSAGE_BIT:
final mark = _startLengthDelimited();
// `value` is [GeneratedMessage].
value.writeToCodedBufferWriter(this);
_endLengthDelimited(mark);
break;
}
}

void _writeBytesNoTag(dynamic value) {
// `value` is [TypedData]&[List].
writeInt32NoTag(value.length);
writeRawBytes(value);
}
Expand Down
5 changes: 3 additions & 2 deletions protobuf/lib/src/protobuf/extension_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ T _reparseMessage<T extends GeneratedMessage>(
resultMap ??= ensureResult()._fieldSet._values[field.index!];

if (field.isRepeated) {
final messageEntries = message._fieldSet._values[field.index!];
final /*List*/ messageEntries = message._fieldSet._values[field.index!];
if (messageEntries == null) continue;
if (field.isGroupOrMessage) {
for (var i = 0; i < messageEntries.length; i++) {
Expand All @@ -146,7 +146,8 @@ T _reparseMessage<T extends GeneratedMessage>(
}
}
} else if (field is MapFieldInfo) {
final messageMap = message._fieldSet._values[field.index!];
final /*PbMap*/ messageMap = message._fieldSet._values[field.index!];

if (messageMap == null) continue;
if (_isGroupOrMessage(field.valueFieldType!)) {
for (var key in messageMap.keys) {
Expand Down
9 changes: 6 additions & 3 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class _FieldSet {
_frozenState = true;
for (var field in _meta.sortedByTag) {
if (field.isRepeated) {
final entries = _values[field.index!];
final /*PbList*/ entries = _values[field.index!];
if (entries == null) continue;
if (field.isGroupOrMessage) {
for (var subMessage in entries as List<GeneratedMessage>) {
Expand Down Expand Up @@ -668,7 +668,9 @@ class _FieldSet {
} else if (!_isEnum(fi.type)) {
hash = _HashUtils._combine(hash, value.hashCode);
} else if (fi.isRepeated) {
hash = _HashUtils._hashObjects(value.map((enm) => enm.value));
// `value` is [List].
hash = _HashUtils._hashObjects(
value.map((/*ProtobufEnum*/ enm) => enm.value));
} else {
ProtobufEnum enm = value;
hash = _HashUtils._combine(hash, enm.value);
Expand Down Expand Up @@ -802,6 +804,7 @@ class _FieldSet {
var mustClone = _isGroupOrMessage(otherFi.type);

if (fi!.isMapField) {
// `fieldValue` is [Map].
var f = fi as MapFieldInfo<dynamic, dynamic>;
mustClone = _isGroupOrMessage(f.valueFieldType!);
var map = f._ensureMapField(meta, this) as PbMap<dynamic, dynamic>;
Expand Down Expand Up @@ -832,7 +835,7 @@ class _FieldSet {
}

if (otherFi.isGroupOrMessage) {
final currentFi = isExtension!
final GeneratedMessage? currentFi = isExtension!
? _ensureExtensions()._getFieldOrNull(fi as Extension<dynamic>)
: _values[fi.index!];

Expand Down
6 changes: 5 additions & 1 deletion protobuf/lib/src/protobuf/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Map<String, dynamic> _writeToJsonMap(_FieldSet fs) {
var baseType = PbFieldType._baseType(fieldType);

if (_isRepeated(fieldType)) {
// `fieldValue` is [List].
return List.from(fieldValue.map((e) => convertToMap(e, baseType)));
}

Expand All @@ -27,23 +28,26 @@ Map<String, dynamic> _writeToJsonMap(_FieldSet fs) {
// Encode 'bytes' as a base64-encoded string.
return base64Encode(fieldValue as List<int>);
case PbFieldType._ENUM_BIT:
// `fieldValue` is [ProtobufEnum].
return fieldValue.value; // assume |value| < 2^52
case PbFieldType._INT64_BIT:
case PbFieldType._SINT64_BIT:
case PbFieldType._SFIXED64_BIT:
return fieldValue.toString();
case PbFieldType._UINT64_BIT:
case PbFieldType._FIXED64_BIT:
// `fieldValue` is [Int64].
return fieldValue.toStringUnsigned();
case PbFieldType._GROUP_BIT:
case PbFieldType._MESSAGE_BIT:
// `fieldValue` is [GeneratedMessage].
return fieldValue.writeToJsonMap();
default:
throw 'Unknown type $fieldType';
}
}

List _writeMap(dynamic fieldValue, MapFieldInfo fi) =>
List _writeMap(Map<Object?, Object?> fieldValue, MapFieldInfo fi) =>
List.from(fieldValue.entries.map((MapEntry e) => {
'${PbMap._keyFieldNumber}': convertToMap(e.key, fi.keyFieldType!),
'${PbMap._valueFieldNumber}':
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool _areMapsEqual(Map lhs, Map rhs) {
}

bool _areByteDataEqual(ByteData lhs, ByteData rhs) {
Uint8List asBytes(d) =>
Uint8List asBytes(ByteData d) =>
Uint8List.view(d.buffer, d.offsetInBytes, d.lengthInBytes);
return _areListsEqual(asBytes(lhs), asBytes(rhs));
}
Expand Down