Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft of typed getExtension #357

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions protobuf/lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BuilderInfo {
var index = byIndex.length;
final fieldInfo = (tagNumber == 0)
? FieldInfo.dummy(index)
: FieldInfo<T>(name, tagNumber, index, fieldType,
: FieldInfo<T, T>(name, tagNumber, index, fieldType,
defaultOrMaker: defaultOrMaker,
subBuilder: subBuilder,
valueOf: valueOf,
Expand Down Expand Up @@ -77,7 +77,7 @@ class BuilderInfo {
List<ProtobufEnum> enumValues,
{String protoName}) {
var index = byIndex.length;
_addField(FieldInfo<T>.repeated(
_addField(FieldInfo<T, List<T>>.repeated(
name, tagNumber, index, fieldType, check, subBuilder,
valueOf: valueOf, enumValues: enumValues, protoName: protoName));
}
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 @@ -5,7 +5,7 @@
part of protobuf;

/// An object representing an extension field.
class Extension<T> extends FieldInfo<T> {
class Extension<T, U> extends FieldInfo<T, U> {
final String extendee;

Extension(this.extendee, String name, int tagNumber, int fieldType,
Expand Down
4 changes: 2 additions & 2 deletions protobuf/lib/src/protobuf/extension_field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class _ExtensionFieldSet {
///
/// If it doesn't exist, creates the list and saves the extension.
/// Suitable for public API and decoders.
List<T> _ensureRepeatedField<T>(Extension<T> fi) {
List<T> _ensureRepeatedField<T, S>(Extension<T, S> fi) {
assert(!_isReadOnly);
assert(fi.isRepeated);
assert(fi.extendee == _parent._messageName);
Expand All @@ -47,7 +47,7 @@ class _ExtensionFieldSet {
return _addInfoAndCreateList(fi);
}

List<T> _getList<T>(Extension<T> fi) {
List<T> _getList<T, S>(Extension<T, S> fi) {
var value = _values[fi.tagNumber];
if (value != null) return value as List<T>;
if (_isReadOnly) return List<T>.unmodifiable(const []);
Expand Down
23 changes: 12 additions & 11 deletions protobuf/lib/src/protobuf/field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
part of protobuf;

/// An object representing a protobuf message field.
class FieldInfo<T> {
/// For a repeated field S is List<T> for a non-repeated field S=T.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some type parameters S and U throughout the PR. Are they meant to be separate types? The comment above mentions S, but the parameter is U here. I am guessing either is meant to represent the List as mentioned

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - I did a rename, but forgot to rename the comment.

class FieldInfo<T, U> {
FrozenPbList<T> _emptyList;

/// Name of this field as the `json_name` reported by protoc.
Expand All @@ -24,9 +25,9 @@ class FieldInfo<T> {

// Constructs the default value of a field.
// (Only used for repeated fields where check is null.)
final MakeDefaultFunc makeDefault;
final U Function() makeDefault;

// Creates an empty message or group when decoding a message.
// Creates an empty message when decoding a message.
// Not used for other types.
// see GeneratedMessage._getEmptyMessage
final CreateBuilderFunc subBuilder;
Expand Down Expand Up @@ -74,7 +75,7 @@ class FieldInfo<T> {
FieldInfo.repeated(this.name, this.tagNumber, this.index, this.type,
this.check, this.subBuilder,
{this.valueOf, this.enumValues, String protoName})
: makeDefault = (() => PbList<T>(check: check)),
: makeDefault = (() => PbList<T>(check: check) as U),
protoName = protoName ?? _unCamelCase(name) {
assert(name != null);
assert(tagNumber != null);
Expand All @@ -83,7 +84,7 @@ class FieldInfo<T> {
assert(!_isEnum(type) || valueOf != null);
}

static MakeDefaultFunc findMakeDefault(int type, dynamic defaultOrMaker) {
static S Function() findMakeDefault<S>(int type, dynamic defaultOrMaker) {
if (defaultOrMaker == null) return PbFieldType._defaultForType(type);
if (defaultOrMaker is MakeDefaultFunc) return defaultOrMaker;
return () => defaultOrMaker;
Expand All @@ -101,9 +102,9 @@ class FieldInfo<T> {

/// Returns a read-only default value for a field.
/// (Unlike getField, doesn't create a repeated field.)
dynamic get readonlyDefault {
U get readonlyDefault {
if (isRepeated) {
return _emptyList ??= FrozenPbList._([]);
return (_emptyList ??= FrozenPbList<T>._([])) as U;
}
return makeDefault();
}
Expand Down Expand Up @@ -165,19 +166,19 @@ class FieldInfo<T> {
/// be overridden by a mixin.
List<T> _createRepeatedField(GeneratedMessage m) {
assert(isRepeated);
return m.createRepeatedField<T>(tagNumber, this);
return m.createRepeatedField<T, U>(tagNumber, this);
}

/// Same as above, but allow a tighter typed List to be created.
List<S> _createRepeatedFieldWithType<S extends T>(GeneratedMessage m) {
assert(isRepeated);
return m.createRepeatedField<S>(tagNumber, this);
return m.createRepeatedField<S, U>(tagNumber, this);
}

/// Convenience method to thread this FieldInfo's reified type parameter to
/// _FieldSet._ensureRepeatedField.
List<T> _ensureRepeatedField(_FieldSet fs) {
return fs._ensureRepeatedField<T>(this);
return fs._ensureRepeatedField<T, U>(this);
}

@override
Expand All @@ -191,7 +192,7 @@ String _unCamelCase(String name) {
_upperCase, (match) => '_${match.group(0).toLowerCase()}');
}

class MapFieldInfo<K, V> extends FieldInfo<PbMap<K, V>> {
class MapFieldInfo<K, V> extends FieldInfo<PbMap<K, V>, PbMap<K, V>> {
final int keyFieldType;
final int valueFieldType;

Expand Down
8 changes: 4 additions & 4 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class _FieldSet {
return value;
}

List<T> _getDefaultList<T>(FieldInfo<T> fi) {
List<T> _getDefaultList<T, U>(FieldInfo<T, U> fi) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the new parameter plumbed through anywhere from here?

assert(fi.isRepeated);
if (_isReadOnly) return List.unmodifiable(const []);

Expand Down Expand Up @@ -359,11 +359,11 @@ 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<T> _ensureRepeatedField<T>(FieldInfo<T> fi) {
List<T> _ensureRepeatedField<T, U>(FieldInfo<T, U> fi) {
assert(!_isReadOnly);
assert(fi.isRepeated);
if (fi.index == null) {
return _ensureExtensions()._ensureRepeatedField(fi);
return _ensureExtensions()._ensureRepeatedField(fi as Extension<T, U>);
}
var value = _getFieldOrNull(fi);
if (value != null) return value as List<T>;
Expand Down Expand Up @@ -439,7 +439,7 @@ class _FieldSet {
List<T> _$getList<T>(int index) {
var value = _values[index];
if (value != null) return value as List<T>;
return _getDefaultList<T>(_nonExtensionInfoByIndex(index));
return _getDefaultList<T, dynamic>(_nonExtensionInfoByIndex(index));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this be dynamic and not List<T> here?

}

/// The implementation of a generated getter for map fields.
Expand Down
4 changes: 2 additions & 2 deletions protobuf/lib/src/protobuf/generated_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ abstract class GeneratedMessage {
/// Returns the value of [extension].
///
/// If not set, returns the extension's default value.
dynamic getExtension(Extension extension) {
U getExtension<T, U>(Extension<T, U> extension) {
return _fieldSet._ensureExtensions()._getFieldOrDefault(extension);
}

Expand All @@ -327,7 +327,7 @@ 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<T> createRepeatedField<T>(int tagNumber, FieldInfo<T> fi) {
List<T> createRepeatedField<T, U>(int tagNumber, FieldInfo<T, U> fi) {
return PbList<T>(check: fi.check);
}

Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/readonly_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class ReadonlyMessageMixin {

void clearField(int tagNumber) => _readonly('clearField');

List<T> createRepeatedField<T>(int tagNumber, FieldInfo<T> fi) {
List<T> createRepeatedField<T, U>(int tagNumber, FieldInfo<T, U> fi) {
_readonly('createRepeatedField');
return null; // not reached
}
Expand Down
12 changes: 8 additions & 4 deletions protoc_plugin/lib/extension_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class ExtensionGenerator {
String name = _extensionName;
var type = _field.baseType;
var dartType = type.getDartType(fileGen);
var possiblyRepeatedDartType =
_field.isRepeated ? '$_coreImportPrefix.List<$dartType>' : dartType;
var typeArgs = '$dartType, $possiblyRepeatedDartType';
String invocation;
List<String> positionals = <String>[];
positionals.add("'$_extendedFullName'");
Expand All @@ -107,7 +110,7 @@ class ExtensionGenerator {
Map<String, String> named = <String, String>{};
named['protoName'] = _field.quotedProtoName;
if (_field.isRepeated) {
invocation = '$_protobufImportPrefix.Extension<$dartType>.repeated';
invocation = '$_protobufImportPrefix.Extension<$typeArgs>.repeated';
named['check'] =
'$_protobufImportPrefix.getCheckFunction(${_field.typeConstant})';
if (type.isMessage || type.isGroup) {
Expand All @@ -117,7 +120,7 @@ class ExtensionGenerator {
named['enumValues'] = '$dartType.values';
}
} else {
invocation = '$_protobufImportPrefix.Extension<$dartType>';
invocation = '$_protobufImportPrefix.Extension<$typeArgs>';
named['defaultOrMaker'] = _field.generateDefaultFunction(fileGen);
if (type.isMessage || type.isGroup) {
named['subBuilder'] = '$dartType.create';
Expand All @@ -129,13 +132,14 @@ class ExtensionGenerator {
}
assert(invocation != null);
out.printAnnotated(
'static final $_protobufImportPrefix.Extension $name = '
'static final $_protobufImportPrefix.Extension<$typeArgs> $name = '
'$invocation(${ProtobufField._formatArguments(positionals, named)});\n',
[
NamedLocation(
name: name,
fieldPathSegment: List.from(fieldPath),
start: 'static final $_protobufImportPrefix.Extension '.length)
start: 'static final $_protobufImportPrefix.Extension<$typeArgs> '
.length)
]);
}
}
62 changes: 29 additions & 33 deletions protoc_plugin/lib/src/dart_options.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// source: dart_options.proto
//
// @dart = 2.3
// ignore_for_file: camel_case_types,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type
// ignore_for_file: camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type

import 'dart:core' as $core;

Expand Down Expand Up @@ -110,38 +110,34 @@ class Imports extends $pb.GeneratedMessage {
}

class Dart_options {
static final $pb.Extension imports = $pb.Extension<Imports>(
'google.protobuf.FileOptions', 'imports', 28125061, $pb.PbFieldType.OM,
defaultOrMaker: Imports.getDefault, subBuilder: Imports.create);
static final $pb.Extension defaultMixin = $pb.Extension<$core.String>(
'google.protobuf.FileOptions',
'defaultMixin',
96128839,
$pb.PbFieldType.OS);
static final $pb.Extension mixin = $pb.Extension<$core.String>(
'google.protobuf.MessageOptions', 'mixin', 96128839, $pb.PbFieldType.OS);
static final $pb.Extension overrideGetter = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideGetter',
28205290,
$pb.PbFieldType.OB);
static final $pb.Extension overrideSetter = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideSetter',
28937366,
$pb.PbFieldType.OB);
static final $pb.Extension overrideHasMethod = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideHasMethod',
28937461,
$pb.PbFieldType.OB);
static final $pb.Extension overrideClearMethod = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideClearMethod',
28907907,
$pb.PbFieldType.OB);
static final $pb.Extension dartName = $pb.Extension<$core.String>(
'google.protobuf.FieldOptions', 'dartName', 28700919, $pb.PbFieldType.OS);
static final $pb.Extension<Imports, Imports> imports =
$pb.Extension<Imports, Imports>('google.protobuf.FileOptions', 'imports',
28125061, $pb.PbFieldType.OM,
defaultOrMaker: Imports.getDefault, subBuilder: Imports.create);
static final $pb.Extension<$core.String, $core.String> defaultMixin =
$pb.Extension<$core.String, $core.String>('google.protobuf.FileOptions',
'defaultMixin', 96128839, $pb.PbFieldType.OS);
static final $pb.Extension<$core.String, $core.String> mixin =
$pb.Extension<$core.String, $core.String>(
'google.protobuf.MessageOptions',
'mixin',
96128839,
$pb.PbFieldType.OS);
static final $pb.Extension<$core.bool, $core.bool> overrideGetter =
$pb.Extension<$core.bool, $core.bool>('google.protobuf.FieldOptions',
'overrideGetter', 28205290, $pb.PbFieldType.OB);
static final $pb.Extension<$core.bool, $core.bool> overrideSetter =
$pb.Extension<$core.bool, $core.bool>('google.protobuf.FieldOptions',
'overrideSetter', 28937366, $pb.PbFieldType.OB);
static final $pb.Extension<$core.bool, $core.bool> overrideHasMethod =
$pb.Extension<$core.bool, $core.bool>('google.protobuf.FieldOptions',
'overrideHasMethod', 28937461, $pb.PbFieldType.OB);
static final $pb.Extension<$core.bool, $core.bool> overrideClearMethod =
$pb.Extension<$core.bool, $core.bool>('google.protobuf.FieldOptions',
'overrideClearMethod', 28907907, $pb.PbFieldType.OB);
static final $pb.Extension<$core.String, $core.String> dartName =
$pb.Extension<$core.String, $core.String>('google.protobuf.FieldOptions',
'dartName', 28700919, $pb.PbFieldType.OS);
static void registerAllExtensions($pb.ExtensionRegistry registry) {
registry.add(imports);
registry.add(defaultMixin);
Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/lib/src/descriptor.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// source: descriptor.proto
//
// @dart = 2.3
// ignore_for_file: camel_case_types,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type
// ignore_for_file: camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type

import 'dart:core' as $core;

Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/lib/src/descriptor.pbenum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// source: descriptor.proto
//
// @dart = 2.3
// ignore_for_file: camel_case_types,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type
// ignore_for_file: camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type

// ignore_for_file: UNDEFINED_SHOWN_NAME,UNUSED_SHOWN_NAME
import 'dart:core' as $core;
Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/lib/src/plugin.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// source: plugin.proto
//
// @dart = 2.3
// ignore_for_file: camel_case_types,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type
// ignore_for_file: camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type

import 'dart:core' as $core;

Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dev_dependencies:

executables:
protoc-gen-dart: protoc_plugin

dependency_overrides: {protobuf: {path: ../protobuf}}
6 changes: 3 additions & 3 deletions protoc_plugin/test/repeated_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ void main() {
var msg = TestAllTypes();
expect(
(msg.info_.byName["repeatedNestedMessage"]
as FieldInfo<TestAllTypes_NestedMessage>)
as FieldInfo<TestAllTypes_NestedMessage, List<TestAllTypes_NestedMessage>>)
.check,
isNotNull);

expect(
(msg.info_.byName["repeatedgroup"]
as FieldInfo<TestAllTypes_RepeatedGroup>)
as FieldInfo<TestAllTypes_RepeatedGroup, List<TestAllTypes_RepeatedGroup>>)
.check,
isNotNull);

expect(
(msg.info_.byName["repeatedNestedEnum"]
as FieldInfo<TestAllTypes_NestedEnum>)
as FieldInfo<TestAllTypes_NestedEnum, List<TestAllTypes_NestedEnum>>)
.check,
isNotNull);
});
Expand Down