diff --git a/CHANGELOG.md b/CHANGELOG.md index ce7c6017f1cde..7f257de7090dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/src/protobuf/builder_info.dart b/lib/src/protobuf/builder_info.dart index 0670c56241b59..95fa4d41d4f02 100644 --- a/lib/src/protobuf/builder_info.dart +++ b/lib/src/protobuf/builder_info.dart @@ -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 byIndex = []; final Map fieldInfo = new Map(); final Map byTagAsString = {}; @@ -17,7 +17,7 @@ class BuilderInfo { List _sortedByTag; BuilderInfo(String messageName, {PackageName package = const PackageName('')}) - : messageName = "${package.prefix}$messageName"; + : qualifiedMessageName = "${package.prefix}$messageName"; void add( int tagNumber, @@ -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 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 _computeSortedByTag() { // TODO(skybrian): perhaps the code generator should insert the FieldInfos // in tag number order, to avoid sorting them? @@ -165,8 +173,9 @@ 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(); } @@ -174,7 +183,7 @@ class BuilderInfo { _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); } diff --git a/lib/src/protobuf/coded_buffer_reader.dart b/lib/src/protobuf/coded_buffer_reader.dart index c448e85c00c67..bab15f6a6b539 100644 --- a/lib/src/protobuf/coded_buffer_reader.dart +++ b/lib/src/protobuf/coded_buffer_reader.dart @@ -17,8 +17,8 @@ class CodedBufferReader { final int _sizeLimit; CodedBufferReader(List 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, diff --git a/lib/src/protobuf/field_set.dart b/lib/src/protobuf/field_set.dart index 9448fecae105d..883511aef3cd4 100644 --- a/lib/src/protobuf/field_set.dart +++ b/lib/src/protobuf/field_set.dart @@ -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. diff --git a/lib/src/protobuf/mixins/map_mixin.dart b/lib/src/protobuf/mixins/map_mixin.dart index 7079aa4716766..87c38bb74ca67 100644 --- a/lib/src/protobuf/mixins/map_mixin.dart +++ b/lib/src/protobuf/mixins/map_mixin.dart @@ -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); } @@ -45,6 +45,6 @@ abstract class PbMapMixin { remove(key) { throw new UnsupportedError( - "remove() not supported by ${info_.messageName}"); + "remove() not supported by ${info_.qualifiedMessageName}"); } } diff --git a/lib/src/protobuf/pb_list.dart b/lib/src/protobuf/pb_list.dart index 8517910f38d8f..7c2c1c365ec86 100644 --- a/lib/src/protobuf/pb_list.dart +++ b/lib/src/protobuf/pb_list.dart @@ -154,9 +154,9 @@ abstract class PbListBase extends ListBase { final List _wrappedList; final CheckFunc check; - PbListBase._(this._wrappedList, {this.check: _checkNotNull}) {} + PbListBase._(this._wrappedList, {this.check = _checkNotNull}) {} - PbListBase._noList({this.check: _checkNotNull}) : _wrappedList = [] { + PbListBase._noList({this.check = _checkNotNull}) : _wrappedList = [] { assert(check != null); } @@ -222,7 +222,7 @@ abstract class PbListBase extends ListBase { bool any(bool test(E element)) => _wrappedList.any(test); /// Creates a [List] containing the elements of this [Iterable]. - List toList({bool growable: true}) => + List toList({bool growable = true}) => _wrappedList.toList(growable: growable); /// Creates a [Set] containing the same elements as this iterable. diff --git a/lib/src/protobuf/readonly_message.dart b/lib/src/protobuf/readonly_message.dart index a711961a35009..f6edf1ecb805e 100644 --- a/lib/src/protobuf/readonly_message.dart +++ b/lib/src/protobuf/readonly_message.dart @@ -53,7 +53,7 @@ abstract class ReadonlyMessageMixin { _readonly("setField"); void _readonly(String methodName) { - String messageType = info_.messageName; + String messageType = info_.qualifiedMessageName; frozenMessageModificationHandler(messageType, methodName); } } diff --git a/lib/src/protobuf/unpack.dart b/lib/src/protobuf/unpack.dart index 142ebcd10a059..7fb20cef6e59b 100644 --- a/lib/src/protobuf/unpack.dart +++ b/lib/src/protobuf/unpack.dart @@ -21,7 +21,7 @@ void unpackIntoHelper( // 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); } @@ -33,7 +33,7 @@ void unpackIntoHelper( /// /// 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) { diff --git a/pubspec.yaml b/pubspec.yaml index 018e537326ae2..c8eba3eb57754 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: protobuf -version: 0.10.3 +version: 0.10.4 author: Dart Team description: > Runtime library for protocol buffers support. diff --git a/test/builder_info_test.dart b/test/builder_info_test.dart new file mode 100644 index 0000000000000..6df57ab82d6c3 --- /dev/null +++ b/test/builder_info_test.dart @@ -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); + }); + }); +}