Skip to content
Merged
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
9 changes: 9 additions & 0 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 4.2.0-wip

* Internal refactoring to split the package into libraries. This allows
conditionally importing different libraries and improving performance by
using different encoding/decoding libraries based on the target platform.
([#1026])

[#1026]: https://github.com/google/protobuf.dart/pull/1026

## 4.1.1

* Minimum SDK dependency bumped from 3.6.0 to 3.7.0. ([#1024])
Expand Down
68 changes: 10 additions & 58 deletions protobuf/lib/protobuf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,13 @@
/// [1]: https://developers.google.com/protocol-buffers
library;

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

import 'package:fixnum/fixnum.dart' show Int64;
import 'package:meta/meta.dart' show UseResult;

import 'src/protobuf/json_parsing_context.dart';
import 'src/protobuf/permissive_compare.dart';
import 'src/protobuf/type_registry.dart';

export 'src/protobuf/type_registry.dart' show TypeRegistry;

part 'src/protobuf/annotations.dart';
part 'src/protobuf/builder_info.dart';
part 'src/protobuf/coded_buffer.dart';
part 'src/protobuf/coded_buffer_reader.dart';
part 'src/protobuf/coded_buffer_writer.dart';
part 'src/protobuf/consts.dart';
part 'src/protobuf/exceptions.dart';
part 'src/protobuf/extension.dart';
part 'src/protobuf/extension_field_set.dart';
part 'src/protobuf/extension_registry.dart';
part 'src/protobuf/field_error.dart';
part 'src/protobuf/field_info.dart';
part 'src/protobuf/field_set.dart';
part 'src/protobuf/field_type.dart';
part 'src/protobuf/generated_message.dart';
part 'src/protobuf/generated_service.dart';
part 'src/protobuf/json.dart';
part 'src/protobuf/message_set.dart';
part 'src/protobuf/pb_list.dart';
part 'src/protobuf/pb_map.dart';
part 'src/protobuf/proto3_json.dart';
part 'src/protobuf/protobuf_enum.dart';
part 'src/protobuf/rpc_client.dart';
part 'src/protobuf/unknown_field_set.dart';
part 'src/protobuf/unpack.dart';
part 'src/protobuf/utils.dart';
part 'src/protobuf/wire_format.dart';

// TODO(sra): Use `Int64.parse()` when available:
// https://github.com/dart-lang/fixnum/issues/18.
/// @nodoc
Int64 parseLongInt(String text) {
if (text.startsWith('0x')) return Int64.parseHex(text.substring(2));
if (text.startsWith('+0x')) return Int64.parseHex(text.substring(3));
if (text.startsWith('-0x')) return -Int64.parseHex(text.substring(3));
return Int64.parseInt(text);
}
export 'src/protobuf/internal.dart'
hide
BuilderInfoInternalExtension,
ExtensionFieldSet,
ExtensionFieldSetInternalExtension,
FieldInfoInternalExtension,
FieldSet,
FieldSetInternalExtension,
GeneratedMessageInternalExtension,
MapFieldInfoInternalExtension;
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// Annotation for marking accessors that belong together.
class TagNumber {
Expand Down
17 changes: 15 additions & 2 deletions protobuf/lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// Per-message type setup.
class BuilderInfo {
Expand All @@ -28,7 +28,7 @@ class BuilderInfo {
final Map<String, FieldInfo> byName = <String, FieldInfo>{};

/// Mapping from `oneof` field [FieldInfo.tagNumber]s to the their indices in
/// [_FieldSet._oneofCases].
/// [FieldSet._oneofCases].
final Map<int, int> oneofs = <int, int>{};

/// Whether the message has extension fields.
Expand Down Expand Up @@ -487,3 +487,16 @@ class BuilderInfo {
?.call(rawValue);
}
}

extension BuilderInfoInternalExtension on BuilderInfo {
GeneratedMessage makeEmptyMessage(
int tagNumber,
ExtensionRegistry? extensionRegistry,
) => _makeEmptyMessage(tagNumber, extensionRegistry);

ProtobufEnum? decodeEnum(
int tagNumber,
ExtensionRegistry? registry,
int rawValue,
) => _decodeEnum(tagNumber, registry, rawValue);
}
12 changes: 6 additions & 6 deletions protobuf/lib/src/protobuf/coded_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

void _writeToCodedBufferWriter(_FieldSet fs, CodedBufferWriter out) {
void _writeToCodedBufferWriter(FieldSet fs, CodedBufferWriter out) {
// Sorting by tag number isn't required, but it sometimes enables
// performance optimizations for the receiver. See:
// https://developers.google.com/protocol-buffers/docs/encoding?hl=en#order
Expand All @@ -17,7 +17,7 @@ void _writeToCodedBufferWriter(_FieldSet fs, CodedBufferWriter out) {

final extensions = fs._extensions;
if (extensions != null) {
for (final tagNumber in _sorted(extensions._tagNumbers)) {
for (final tagNumber in sorted(extensions._tagNumbers)) {
final fi = extensions._getInfoOrNull(tagNumber)!;
out.writeField(tagNumber, fi.type, extensions._getFieldOrNull(fi));
}
Expand All @@ -31,7 +31,7 @@ void _writeToCodedBufferWriter(_FieldSet fs, CodedBufferWriter out) {

void _mergeFromCodedBufferReader(
BuilderInfo meta,
_FieldSet fs,
FieldSet fs,
CodedBufferReader input,
ExtensionRegistry registry,
) {
Expand Down Expand Up @@ -404,7 +404,7 @@ void _mergeFromCodedBufferReader(
void _readPackableToListEnum(
List list,
BuilderInfo meta,
_FieldSet fs,
FieldSet fs,
CodedBufferReader input,
int wireType,
int tagNumber,
Expand All @@ -426,7 +426,7 @@ void _readPackableToListEnum(
void _readRepeatedEnum(
List list,
BuilderInfo meta,
_FieldSet fs,
FieldSet fs,
CodedBufferReader input,
int tagNumber,
ExtensionRegistry registry,
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 @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// Reader used for converting binary-encoded protobufs into
/// [GeneratedMessage]s.
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// ignore_for_file: constant_identifier_names

part of '../../protobuf.dart';
part of 'internal.dart';

/// Writer used for converting [GeneratedMessage]s into binary
/// representation.
Expand Down
8 changes: 3 additions & 5 deletions protobuf/lib/src/protobuf/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';

/// Constant string value of `double.infinity.toString()` and the infinity
/// value recognized by `double.parse(..)`.
const _infinity = 'Infinity';
const infinity = 'Infinity';

/// Constant string value of `double.negativeInfinity.toString()` and the
/// negative infinity value recognized by `double.parse(..)`.
const _negativeInfinity = '-Infinity';
const negativeInfinity = '-Infinity';

/// Constant string value of `double.nan.toString()` and the NaN (not a number)
/// value recognized by `double.parse(..)`.
const _nan = 'NaN';
const nan = 'NaN';
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

const _truncatedMessageText = '''
While parsing a protocol message, the input ended unexpectedly
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// An object representing an extension field.
class Extension<T> extends FieldInfo<T> {
Expand Down
20 changes: 13 additions & 7 deletions protobuf/lib/src/protobuf/extension_field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

class _ExtensionFieldSet {
final _FieldSet _parent;
class ExtensionFieldSet {
final FieldSet _parent;
final Map<int, Extension> _info = <int, Extension>{};
final Map<int, dynamic> _values = <int, dynamic>{};
bool _isReadOnly = false;

_ExtensionFieldSet(this._parent);
ExtensionFieldSet(this._parent);

Extension? _getInfoOrNull(int tagNumber) => _info[tagNumber];

Expand Down Expand Up @@ -157,16 +157,16 @@ class _ExtensionFieldSet {

bool get _hasValues => _values.isNotEmpty;

bool _equalValues(_ExtensionFieldSet? other) =>
other != null && _areMapsEqual(_values, other._values);
bool _equalValues(ExtensionFieldSet? other) =>
other != null && areMapsEqual(_values, other._values);

void _clearValues() => _values.clear();

/// Makes a shallow copy of all values from [original] to this.
///
/// Repeated fields are copied.
/// Extensions cannot contain map fields.
void _shallowCopyValues(_ExtensionFieldSet original) {
void _shallowCopyValues(ExtensionFieldSet original) {
for (final tagNumber in original._tagNumbers) {
final extension = original._getInfoOrNull(tagNumber)!;
_addInfoUnchecked(extension);
Expand Down Expand Up @@ -212,3 +212,9 @@ class _ExtensionFieldSet {
}
}
}

extension ExtensionFieldSetInternalExtension on ExtensionFieldSet {
Map<int, dynamic> get values => _values;
Iterable<int> get tagNumbers => _tagNumbers;
Extension? getInfoOrNull(int tagNumber) => _getInfoOrNull(tagNumber);
}
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/extension_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// A collection of [Extension] objects, organized by the message type they
/// extend.
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/field_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// Returns the error message for an invalid field value,
/// or null if it's valid.
Expand Down
22 changes: 16 additions & 6 deletions protobuf/lib/src/protobuf/field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of '../../protobuf.dart';
part of 'internal.dart';

/// An object representing a protobuf message field.
class FieldInfo<T> {
Expand Down Expand Up @@ -60,7 +60,7 @@ class FieldInfo<T> {
/// `tagNumber` of `result_per_page` field is 3.
final int tagNumber;

/// Index of the field in [_FieldSet._values] list of this field's message.
/// Index of the field in [FieldSet._values] list of this field's message.
///
/// The value is `null` for extension fields.
final int? index;
Expand Down Expand Up @@ -148,7 +148,7 @@ class FieldInfo<T> {
assert(!_isEnum(type) || valueOf != null);

static MakeDefaultFunc? findMakeDefault(int type, dynamic defaultOrMaker) {
if (defaultOrMaker == null) return PbFieldType._defaultForType(type);
if (defaultOrMaker == null) return PbFieldType.defaultForType(type);
if (defaultOrMaker is MakeDefaultFunc) return defaultOrMaker;
return () => defaultOrMaker;
}
Expand Down Expand Up @@ -238,15 +238,20 @@ class FieldInfo<T> {
}

/// Convenience method to thread this FieldInfo's reified type parameter to
/// `_FieldSet._ensureRepeatedField`.
PbList<T> _ensureRepeatedField(BuilderInfo meta, _FieldSet fs) {
/// `FieldSet._ensureRepeatedField`.
PbList<T> _ensureRepeatedField(BuilderInfo meta, FieldSet fs) {
return fs._ensureRepeatedField<T>(meta, this);
}

@override
String toString() => name;
}

extension FieldInfoInternalExtension<T> on FieldInfo<T> {
List<T> ensureRepeatedField(BuilderInfo meta, FieldSet fs) =>
_ensureRepeatedField(meta, fs);
}

final RegExp _upperCase = RegExp('[A-Z]');

String _unCamelCase(String name) {
Expand Down Expand Up @@ -304,7 +309,7 @@ class MapFieldInfo<K, V> extends FieldInfo<PbMap<K, V>?> {
FieldInfo get valueFieldInfo =>
mapEntryBuilderInfo.fieldInfo[PbMap._valueFieldNumber]!;

PbMap<K, V> _ensureMapField(BuilderInfo meta, _FieldSet fs) {
PbMap<K, V> _ensureMapField(BuilderInfo meta, FieldSet fs) {
return fs._ensureMapField<K, V>(meta, this);
}

Expand All @@ -313,3 +318,8 @@ class MapFieldInfo<K, V> extends FieldInfo<PbMap<K, V>?> {
return PbMap<K, V>(keyFieldType, valueFieldType);
}
}

extension MapFieldInfoInternalExtension<K, V> on MapFieldInfo<K, V> {
Map<K, V> ensureMapField(BuilderInfo meta, FieldSet fs) =>
_ensureMapField(meta, fs);
}
Loading
Loading