Skip to content

Commit

Permalink
Sync changes from internal repo. (flutter#133)
Browse files Browse the repository at this point in the history
Namely, the last major external change (support for Any proto) caused a large breakage internally because of the new behavior of BuilderInfo.messageName. This getter has been reverted back to its original behavior, and a new BuilderInfo.qualifiedMessageName has been introduced.
  • Loading branch information
nichite authored Oct 2, 2018
1 parent 5e5bc71 commit 03d9d6e
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.4

* Added separate getter for `BuilderInfo.qualifiedMessageName`.

## 0.10.3

* Added type argument to `ProtobufEnum.initByValue` which allows the return
Expand Down
21 changes: 15 additions & 6 deletions lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ part of protobuf;
/// Per-message type setup.
class BuilderInfo {
/// The fully qualified name of this message.
final String messageName;
final String qualifiedMessageName;
final List<FieldInfo> byIndex = <FieldInfo>[];
final Map<int, FieldInfo> fieldInfo = new Map<int, FieldInfo>();
final Map<String, FieldInfo> byTagAsString = <String, FieldInfo>{};
Expand All @@ -17,7 +17,7 @@ class BuilderInfo {
List<FieldInfo> _sortedByTag;

BuilderInfo(String messageName, {PackageName package = const PackageName('')})
: messageName = "${package.prefix}$messageName";
: qualifiedMessageName = "${package.prefix}$messageName";

void add<T>(
int tagNumber,
Expand Down Expand Up @@ -151,9 +151,17 @@ class BuilderInfo {
return i != null ? i.valueOf : null;
}

/// Returns the FieldInfo for each field in tag number order.
/// The FieldInfo for each field in tag number order.
List<FieldInfo> get sortedByTag => _sortedByTag ??= _computeSortedByTag();

/// The message name. Also see [qualifiedMessageName].
String get messageName {
final lastDot = qualifiedMessageName.lastIndexOf('.');
return lastDot == -1
? qualifiedMessageName
: qualifiedMessageName.substring(lastDot + 1);
}

List<FieldInfo> _computeSortedByTag() {
// TODO(skybrian): perhaps the code generator should insert the FieldInfos
// in tag number order, to avoid sorting them?
Expand All @@ -165,16 +173,17 @@ class BuilderInfo {
int tagNumber, ExtensionRegistry extensionRegistry) {
CreateBuilderFunc subBuilderFunc = subBuilder(tagNumber);
if (subBuilderFunc == null && extensionRegistry != null) {
subBuilderFunc =
extensionRegistry.getExtension(messageName, tagNumber).subBuilder;
subBuilderFunc = extensionRegistry
.getExtension(qualifiedMessageName, tagNumber)
.subBuilder;
}
return subBuilderFunc();
}

_decodeEnum(int tagNumber, ExtensionRegistry registry, int rawValue) {
ValueOfFunc f = valueOfFunc(tagNumber);
if (f == null && registry != null) {
f = registry.getExtension(messageName, tagNumber).valueOf;
f = registry.getExtension(qualifiedMessageName, tagNumber).valueOf;
}
return f(rawValue);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class CodedBufferReader {
final int _sizeLimit;

CodedBufferReader(List<int> buffer,
{int recursionLimit: DEFAULT_RECURSION_LIMIT,
int sizeLimit: DEFAULT_SIZE_LIMIT})
{int recursionLimit = DEFAULT_RECURSION_LIMIT,
int sizeLimit = DEFAULT_SIZE_LIMIT})
: _buffer = buffer is Uint8List ? buffer : new Uint8List(buffer.length)
..setRange(0, buffer.length, buffer),
_recursionLimit = recursionLimit,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _FieldSet {

// Metadata about multiple fields

String get _messageName => _meta.messageName;
String get _messageName => _meta.qualifiedMessageName;
bool get _hasRequiredFields => _meta.hasRequiredFields;

/// The FieldInfo for each non-extension field.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/protobuf/mixins/map_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class PbMapMixin {
var tag = getTagNumber(key as String);
if (tag == null) {
throw new ArgumentError(
"field '${key}' not found in ${info_.messageName}");
"field '${key}' not found in ${info_.qualifiedMessageName}");
}
setField(tag, val);
}
Expand All @@ -45,6 +45,6 @@ abstract class PbMapMixin {

remove(key) {
throw new UnsupportedError(
"remove() not supported by ${info_.messageName}");
"remove() not supported by ${info_.qualifiedMessageName}");
}
}
6 changes: 3 additions & 3 deletions lib/src/protobuf/pb_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ abstract class PbListBase<E> extends ListBase<E> {
final List<E> _wrappedList;
final CheckFunc<E> check;

PbListBase._(this._wrappedList, {this.check: _checkNotNull}) {}
PbListBase._(this._wrappedList, {this.check = _checkNotNull}) {}

PbListBase._noList({this.check: _checkNotNull}) : _wrappedList = <E>[] {
PbListBase._noList({this.check = _checkNotNull}) : _wrappedList = <E>[] {
assert(check != null);
}

Expand Down Expand Up @@ -222,7 +222,7 @@ abstract class PbListBase<E> extends ListBase<E> {
bool any(bool test(E element)) => _wrappedList.any(test);

/// Creates a [List] containing the elements of this [Iterable].
List<E> toList({bool growable: true}) =>
List<E> toList({bool growable = true}) =>
_wrappedList.toList(growable: growable);

/// Creates a [Set] containing the same elements as this iterable.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/protobuf/readonly_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class ReadonlyMessageMixin {
_readonly("setField");

void _readonly(String methodName) {
String messageType = info_.messageName;
String messageType = info_.qualifiedMessageName;
frozenMessageModificationHandler(messageType, methodName);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/protobuf/unpack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void unpackIntoHelper<T extends GeneratedMessage>(
// in the type URL, for example "foo.bar.com/x/y.z" will yield type
// name "y.z".
if (!canUnpackIntoHelper(instance, typeUrl)) {
String typeName = instance.info_.messageName;
String typeName = instance.info_.qualifiedMessageName;
throw new InvalidProtocolBufferException.wrongAnyMessage(
_typeNameFromUrl(typeUrl), typeName);
}
Expand All @@ -33,7 +33,7 @@ void unpackIntoHelper<T extends GeneratedMessage>(
///
/// This is a helper method for `Any.canUnpackInto`.
bool canUnpackIntoHelper(GeneratedMessage instance, String typeUrl) {
return instance.info_.messageName == _typeNameFromUrl(typeUrl);
return instance.info_.qualifiedMessageName == _typeNameFromUrl(typeUrl);
}

String _typeNameFromUrl(String typeUrl) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: protobuf
version: 0.10.3
version: 0.10.4
author: Dart Team <[email protected]>
description: >
Runtime library for protocol buffers support.
Expand Down
22 changes: 22 additions & 0 deletions test/builder_info_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:protobuf/protobuf.dart';
import 'package:test/test.dart';

main() {
group('className', () {
final qualifiedmessageName = 'proto.test.TestMessage';
final expectedMessageName = 'TestMessage';
test('truncates qualifiedMessageName containing dots', () {
final info = new BuilderInfo(qualifiedmessageName);
expect(info.messageName, expectedMessageName);
});

test('uses qualifiedMessageName if it contains no dots', () {
final info = new BuilderInfo(expectedMessageName);
expect(info.messageName, expectedMessageName);
});
});
}

0 comments on commit 03d9d6e

Please sign in to comment.