From a5ed43c1bfcd245c904b7a26d00e2c98dbf50742 Mon Sep 17 00:00:00 2001 From: Jakob Roland Andersen Date: Fri, 4 Aug 2017 10:35:50 +0200 Subject: [PATCH 1/2] Use real generic syntax instead of comment-based. Also, update pubspec to support v2 dev SDKs. --- CHANGELOG.md | 5 ++ lib/src/protobuf/builder_info.dart | 43 +++++------ lib/src/protobuf/extension.dart | 4 +- lib/src/protobuf/extension_field_set.dart | 4 +- lib/src/protobuf/extension_registry.dart | 18 ++--- lib/src/protobuf/field_info.dart | 6 +- lib/src/protobuf/field_set.dart | 10 +-- lib/src/protobuf/generated_message.dart | 8 +- lib/src/protobuf/pb_list.dart | 93 ++++++++--------------- lib/src/protobuf/readonly_message.dart | 2 +- lib/src/protobuf/utils.dart | 2 +- pubspec.yaml | 4 +- test/coded_buffer_reader_test.dart | 8 +- 13 files changed, 82 insertions(+), 125 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 115fc77f4..ff768bf91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.5.5 + +* Use real generic syntax instead of comment-based. +* Support v2 dev SDKs. + ## 0.5.4 * Unknown enum values are ignored when parsing JSON, instead of throwing an diff --git a/lib/src/protobuf/builder_info.dart b/lib/src/protobuf/builder_info.dart index 8040e0a62..f04eee05b 100644 --- a/lib/src/protobuf/builder_info.dart +++ b/lib/src/protobuf/builder_info.dart @@ -4,9 +4,7 @@ part of protobuf; -/** - * Per-message type setup. - */ +/// Per-message type setup. class BuilderInfo { final String messageName; final Map fieldInfo = new Map(); @@ -18,22 +16,17 @@ class BuilderInfo { BuilderInfo(this.messageName); - void add/**/( - int tagNumber, - String name, - int fieldType, - dynamic defaultOrMaker, - CreateBuilderFunc subBuilder, - ValueOfFunc valueOf) { + void add(int tagNumber, String name, int fieldType, dynamic defaultOrMaker, + CreateBuilderFunc subBuilder, ValueOfFunc valueOf) { var index = fieldInfo.length; - addField(new FieldInfo/**/(name, tagNumber, index, fieldType, - defaultOrMaker, subBuilder, valueOf)); + addField(new FieldInfo(name, tagNumber, index, fieldType, defaultOrMaker, + subBuilder, valueOf)); } - void addRepeated/**/(int tagNumber, String name, int fieldType, + void addRepeated(int tagNumber, String name, int fieldType, CheckFunc check, CreateBuilderFunc subBuilder, ValueOfFunc valueOf) { var index = fieldInfo.length; - addField(new FieldInfo/**/ .repeated( + addField(new FieldInfo.repeated( name, tagNumber, index, fieldType, check, subBuilder, valueOf)); } @@ -43,39 +36,39 @@ class BuilderInfo { byName[fi.name] = fi; } - void a/**/(int tagNumber, String name, int fieldType, + void a(int tagNumber, String name, int fieldType, [dynamic defaultOrMaker, CreateBuilderFunc subBuilder, ValueOfFunc valueOf]) { - add/**/(tagNumber, name, fieldType, defaultOrMaker, subBuilder, valueOf); + add(tagNumber, name, fieldType, defaultOrMaker, subBuilder, valueOf); } // Enum. - void e/**/(int tagNumber, String name, int fieldType, - dynamic defaultOrMaker, ValueOfFunc valueOf) { - add/**/(tagNumber, name, fieldType, defaultOrMaker, null, valueOf); + void e(int tagNumber, String name, int fieldType, dynamic defaultOrMaker, + ValueOfFunc valueOf) { + add(tagNumber, name, fieldType, defaultOrMaker, null, valueOf); } // Repeated message. // TODO(skybrian): migrate to pp() and remove. - void m/**/(int tagNumber, String name, CreateBuilderFunc subBuilder, + void m(int tagNumber, String name, CreateBuilderFunc subBuilder, MakeDefaultFunc makeDefault) { - add/**/(tagNumber, name, PbFieldType._REPEATED_MESSAGE, makeDefault, + add(tagNumber, name, PbFieldType._REPEATED_MESSAGE, makeDefault, subBuilder, null); } // Repeated, not a message, group, or enum. - void p/**/(int tagNumber, String name, int fieldType) { + void p(int tagNumber, String name, int fieldType) { assert(!_isGroupOrMessage(fieldType) && !_isEnum(fieldType)); - addRepeated/**/( + addRepeated( tagNumber, name, fieldType, getCheckFunction(fieldType), null, null); } // Repeated message, group, or enum. - void pp/**/(int tagNumber, String name, int fieldType, CheckFunc check, + void pp(int tagNumber, String name, int fieldType, CheckFunc check, [CreateBuilderFunc subBuilder, ValueOfFunc valueOf]) { assert(_isGroupOrMessage(fieldType) || _isEnum(fieldType)); - addRepeated/**/(tagNumber, name, fieldType, check, subBuilder, valueOf); + addRepeated(tagNumber, name, fieldType, check, subBuilder, valueOf); } bool containsTagNumber(int tagNumber) => fieldInfo.containsKey(tagNumber); diff --git a/lib/src/protobuf/extension.dart b/lib/src/protobuf/extension.dart index cfed0a459..0348a875a 100644 --- a/lib/src/protobuf/extension.dart +++ b/lib/src/protobuf/extension.dart @@ -4,9 +4,7 @@ part of protobuf; -/** - * An object representing an extension field. - */ +/// An object representing an extension field. class Extension extends FieldInfo { final String extendee; diff --git a/lib/src/protobuf/extension_field_set.dart b/lib/src/protobuf/extension_field_set.dart index 87782624b..b96e1df95 100644 --- a/lib/src/protobuf/extension_field_set.dart +++ b/lib/src/protobuf/extension_field_set.dart @@ -38,12 +38,12 @@ class _ExtensionFieldSet { /// /// If it doesn't exist, creates the list and saves the extension. /// Suitable for public API and decoders. - List/**/ _ensureRepeatedField/**/(Extension/**/ fi) { + List _ensureRepeatedField(Extension fi) { assert(fi.isRepeated); assert(fi.extendee == _parent._messageName); var list = _values[fi.tagNumber]; - if (list != null) return list as List/**/; + if (list != null) return list as List; // Add info and create list. _validateInfo(fi); diff --git a/lib/src/protobuf/extension_registry.dart b/lib/src/protobuf/extension_registry.dart index d94225a6d..fd5303804 100644 --- a/lib/src/protobuf/extension_registry.dart +++ b/lib/src/protobuf/extension_registry.dart @@ -4,29 +4,23 @@ part of protobuf; -/** - * A collection of [Extension] objects, organized by the message type they - * extend. - */ +/// A collection of [Extension] objects, organized by the message type they +/// extend. class ExtensionRegistry { final Map> _extensions = >{}; static const ExtensionRegistry EMPTY = const _EmptyExtensionRegistry(); - /** - * Store an extension in the registry. - */ + /// Stores an [extension] in the registry. void add(Extension extension) { var map = _extensions.putIfAbsent( extension.extendee, () => new Map()); map[extension.tagNumber] = extension; } - /** - * Retrieve an extension from the registry that adds the given tag - * number to the given message type. - */ + /// Retrieve an extension from the registry that adds tag number [tagNumber] + /// to the [messageName] message type. Extension getExtension(String messageName, int tagNumber) { var map = _extensions[messageName]; if (map != null) { @@ -39,7 +33,7 @@ class ExtensionRegistry { class _EmptyExtensionRegistry implements ExtensionRegistry { const _EmptyExtensionRegistry(); - // needed to quite missing member warning + // Needed to quiet missing member warning. get _extensions => null; void add(Extension extension) { diff --git a/lib/src/protobuf/field_info.dart b/lib/src/protobuf/field_info.dart index 0c176ebec..8e8d08aa8 100644 --- a/lib/src/protobuf/field_info.dart +++ b/lib/src/protobuf/field_info.dart @@ -4,9 +4,7 @@ part of protobuf; -/** - * An object representing a protobuf message field. - */ +/// An object representing a protobuf message field. class FieldInfo { final String name; final int tagNumber; @@ -127,7 +125,7 @@ class FieldInfo { /// be overridden by a mixin. List _createRepeatedField(GeneratedMessage m) { assert(isRepeated); - return m.createRepeatedField/**/(tagNumber, this); + return m.createRepeatedField(tagNumber, this); } String toString() => name; diff --git a/lib/src/protobuf/field_set.dart b/lib/src/protobuf/field_set.dart index 1a45f9e2b..15e6a2505 100644 --- a/lib/src/protobuf/field_set.dart +++ b/lib/src/protobuf/field_set.dart @@ -210,14 +210,14 @@ class _FieldSet { /// Creates and stores the repeated field if it doesn't exist. /// If it's an extension and the list doesn't exist, validates and stores it. /// Suitable for decoders. - List/**/ _ensureRepeatedField/**/(FieldInfo/**/ fi) { + List _ensureRepeatedField(FieldInfo fi) { assert(!_isReadOnly); assert(fi.isRepeated); if (fi.index == null) { return _ensureExtensions()._ensureRepeatedField(fi); } var value = _getFieldOrNull(fi); - if (value != null) return value as List/**/; + if (value != null) return value as List; var newValue = fi._createRepeatedField(_message); _setNonExtensionFieldUnchecked(fi, newValue); @@ -235,12 +235,12 @@ class _FieldSet { // Generated method implementations /// The implementation of a generated getter. - /*=T*/ _$get/**/(int index, int tagNumber, /*=T*/ defaultValue) { + T _$get(int index, int tagNumber, T defaultValue) { assert(_nonExtensionInfo(tagNumber).index == index); var value = _values[index]; - if (value != null) return value as dynamic/*=T*/; + if (value != null) return value as T; if (defaultValue != null) return defaultValue; - return _getDefault(_nonExtensionInfo(tagNumber)) as dynamic/*=T*/; + return _getDefault(_nonExtensionInfo(tagNumber)) as T; } /// The implementation of a generated has method. diff --git a/lib/src/protobuf/generated_message.dart b/lib/src/protobuf/generated_message.dart index e29bf2882..e4657e5f5 100644 --- a/lib/src/protobuf/generated_message.dart +++ b/lib/src/protobuf/generated_message.dart @@ -221,8 +221,8 @@ abstract class GeneratedMessage { /// that the protobuf can be encoded correctly, the returned List must /// validate all items added to it. This can most easily be done /// using the FieldInfo.check function. - List/**/ createRepeatedField/**/(int tagNumber, FieldInfo/**/ fi) { - return new PbList/**/(check: fi.check); + List createRepeatedField(int tagNumber, FieldInfo fi) { + return new PbList(check: fi.check); } /// Returns the value of a field, ignoring any defaults. @@ -279,8 +279,8 @@ abstract class GeneratedMessage { void setField(int tagNumber, value) => _fieldSet._setField(tagNumber, value); /// For generated code only. - /*=T*/ $_get/**/(int index, int tagNumber, /*=T*/ defaultValue) => - _fieldSet._$get/**/(index, tagNumber, defaultValue); + T $_get(int index, int tagNumber, T defaultValue) => + _fieldSet._$get(index, tagNumber, defaultValue); /// For generated code only. bool $_has(int index, int tagNumber) => _fieldSet._$has(index, tagNumber); diff --git a/lib/src/protobuf/pb_list.dart b/lib/src/protobuf/pb_list.dart index b82b363d3..b4c8a9c8b 100644 --- a/lib/src/protobuf/pb_list.dart +++ b/lib/src/protobuf/pb_list.dart @@ -37,46 +37,34 @@ class PbList extends Object with ListMixin implements List { return hash; } - /** - * Returns an [Iterator] for the list. - */ + /// Returns an [Iterator] for the list. Iterator get iterator => _wrappedList.iterator; - /** - * Returns a new lazy [Iterable] with elements that are created by calling `f` - * on each element of this `PbList` in iteration order. - */ - Iterable/**/ map/**/(/*=T*/ f(E e)) => _wrappedList.map/**/(f); + /// Returns a new lazy [Iterable] with elements that are created by calling + /// `f` on each element of this `PbList` in iteration order. + Iterable map(T f(E e)) => _wrappedList.map(f); - /** - * Applies the function [f] to each element of this list in iteration order. - */ + /// Applies the function [f] to each element of this list in iteration order. void forEach(void f(E element)) { _wrappedList.forEach(f); } - /** - * Returns the element at the given [index] in the list or throws - * an [IndexOutOfRangeException] if [index] is out of bounds. - */ + /// Returns the element at the given [index] in the list or throws an + /// [IndexOutOfRangeException] if [index] is out of bounds. E operator [](int index) => _wrappedList[index]; - /** - * Sets the entry at the given [index] in the list to [value]. - * Throws an [IndexOutOfRangeException] if [index] is out of bounds. - */ + /// Sets the entry at the given [index] in the list to [value]. + /// Throws an [IndexOutOfRangeException] if [index] is out of bounds. void operator []=(int index, E value) { _validate(value); _wrappedList[index] = value; } - /** - * Unsupported -- violated non-null constraint imposed by protobufs. - * - * Changes the length of the list. If [newLength] is greater than - * the current [length], entries are initialized to [:null:]. Throws - * an [UnsupportedError] if the list is not extendable. - */ + /// Unsupported -- violated non-null constraint imposed by protobufs. + /// + /// Changes the length of the list. If [newLength] is greater than the current + /// [length], entries are initialized to [:null:]. Throws an + /// [UnsupportedError] if the list is not extendable. void set length(int newLength) { if (newLength > length) { throw new ArgumentError('Extending protobuf lists is not supported'); @@ -84,33 +72,24 @@ class PbList extends Object with ListMixin implements List { _wrappedList.length = newLength; } - /** - * Adds [value] at the end of the list, extending the length by - * one. Throws an [UnsupportedError] if the list is not - * extendable. - */ + /// Adds [value] at the end of the list, extending the length by one. + /// Throws an [UnsupportedError] if the list is not extendable. void add(E value) { _validate(value); _wrappedList.add(value); } - /** - * Appends all elements of the [collection] to the end of list. - * Extends the length of the list by the length of [collection]. - * Throws an [UnsupportedError] if the list is not - * extendable. - */ + /// Appends all elements of the [collection] to the end of list. + /// Extends the length of the list by the length of [collection]. + /// Throws an [UnsupportedError] if the list is not extendable. void addAll(Iterable collection) { collection.forEach(_validate); _wrappedList.addAll(collection); } - /** - * Copies [:end - start:] elements of the [from] array, starting - * from [skipCount], into [:this:], starting at [start]. - * Throws an [UnsupportedError] if the list is - * not extendable. - */ + /// Copies [:end - start:] elements of the [from] array, starting from + /// [skipCount], into [:this:], starting at [start]. + /// Throws an [UnsupportedError] if the list is not extendable. void setRange(int start, int end, Iterable from, [int skipCount = 0]) { // NOTE: In case `take()` returns less than `end - start` elements, the // _wrappedList will fail with a `StateError`. @@ -118,39 +97,31 @@ class PbList extends Object with ListMixin implements List { _wrappedList.setRange(start, end, from, skipCount); } - /** - * Inserts a new element in the list. - * The element must be valid (and not nullable) for the PbList type. - */ + /// Inserts a new element in the list. + /// The element must be valid (and not nullable) for the PbList type. void insert(int index, E element) { _validate(element); _wrappedList.insert(index, element); } - /** - * Inserts all elements of [iterable] at position [index] in the list. - * - * Elements in [iterable] must be valid and not nullable for the PbList type. - */ + /// Inserts all elements of [iterable] at position [index] in the list. + /// + /// Elements in [iterable] must be valid and not nullable for the PbList type. void insertAll(int index, Iterable iterable) { iterable.forEach(_validate); _wrappedList.insertAll(index, iterable); } - /** - * Overwrites elements of `this` with elements of [iterable] starting at - * position [index] in the list. - * - * Elements in [iterable] must be valid and not nullable for the PbList type. - */ + /// Overwrites elements of `this` with elements of [iterable] starting at + /// position [index] in the list. + /// + /// Elements in [iterable] must be valid and not nullable for the PbList type. void setAll(int index, Iterable iterable) { iterable.forEach(_validate); _wrappedList.setAll(index, iterable); } - /** - * Returns the number of elements in this collection. - */ + /// Returns the number of elements in this collection. int get length => _wrappedList.length; void _validate(E val) { diff --git a/lib/src/protobuf/readonly_message.dart b/lib/src/protobuf/readonly_message.dart index 57cf76930..b8f03e0fa 100644 --- a/lib/src/protobuf/readonly_message.dart +++ b/lib/src/protobuf/readonly_message.dart @@ -17,7 +17,7 @@ abstract class ReadonlyMessageMixin { void clearField(int tagNumber) => _readonly("clearField"); - List/**/ createRepeatedField/**/(int tagNumber, FieldInfo/**/ fi) { + List createRepeatedField(int tagNumber, FieldInfo fi) { _readonly("createRepeatedField"); return null; // not reached } diff --git a/lib/src/protobuf/utils.dart b/lib/src/protobuf/utils.dart index a9993e3dd..fb6ee44a2 100644 --- a/lib/src/protobuf/utils.dart +++ b/lib/src/protobuf/utils.dart @@ -35,4 +35,4 @@ bool _areByteDataEqual(ByteData lhs, ByteData rhs) { return _areListsEqual(asBytes(lhs), asBytes(rhs)); } -List/**/ sorted/**/(Iterable/**/ list) => new List.from(list)..sort(); +List sorted(Iterable list) => new List.from(list)..sort(); diff --git a/pubspec.yaml b/pubspec.yaml index 458317b7d..48ae1b4cd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: protobuf -version: 0.5.4 +version: 0.5.5 author: Dart Team description: Runtime library for protocol buffers support. homepage: https://github.com/dart-lang/protobuf environment: - sdk: '>=1.13.0 <2.0.0' + sdk: '>=1.21.0 <2.0.0-dev.infinity' dependencies: fixnum: '>=0.9.0 <0.11.0' dev_dependencies: diff --git a/test/coded_buffer_reader_test.dart b/test/coded_buffer_reader_test.dart index 7a30bee05..3e46cecbd 100755 --- a/test/coded_buffer_reader_test.dart +++ b/test/coded_buffer_reader_test.dart @@ -122,11 +122,9 @@ void main() { }, throwsInvalidProtocolBufferException); }); - /** - * Tests that if we read a string that contains invalid UTF-8, no exception - * is thrown. Instead, the invalid bytes are replaced with the Unicode - * 'replacement character' U+FFFD. - */ + /// Tests that if we read a string that contains invalid UTF-8, no exception + /// is thrown. Instead, the invalid bytes are replaced with the Unicode + /// 'replacement character' U+FFFD. test('testReadInvalidUtf8', () { CodedBufferReader input = new CodedBufferReader([1, 0x80]); String text = input.readString(); From da45a973621accb8df4ed7db291e76d7b3b08859 Mon Sep 17 00:00:00 2001 From: Jakob Roland Andersen Date: Mon, 14 Aug 2017 13:12:26 +0200 Subject: [PATCH 2/2] Review feedback. --- lib/src/protobuf/extension_registry.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/protobuf/extension_registry.dart b/lib/src/protobuf/extension_registry.dart index fd5303804..667052d48 100644 --- a/lib/src/protobuf/extension_registry.dart +++ b/lib/src/protobuf/extension_registry.dart @@ -19,7 +19,7 @@ class ExtensionRegistry { map[extension.tagNumber] = extension; } - /// Retrieve an extension from the registry that adds tag number [tagNumber] + /// Retrieves an extension from the registry that adds tag number [tagNumber] /// to the [messageName] message type. Extension getExtension(String messageName, int tagNumber) { var map = _extensions[messageName];