From 1d0dd7427ed767a1528d9cc5813d105f4f56be88 Mon Sep 17 00:00:00 2001 From: Jakob Andersen Date: Wed, 25 Oct 2017 12:43:05 +0200 Subject: [PATCH] Sync changes from internal repo. (#96) * Added enumValues to FieldInfo. Fixes dart-lang/protobuf#63. --- CHANGELOG.md | 4 ++++ lib/code_generator.dart | 3 ++- lib/extension_generator.dart | 7 ++++--- lib/protobuf_field.dart | 6 +++--- lib/src/descriptor.pb.dart | 36 ++++++++++++++++++++++++-------- pubspec.yaml | 4 ++-- test/message_generator_test.dart | 2 +- 7 files changed, 43 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03d8c0f..27c3a9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.7.8 - 2017-10-25 + +* Added enumValues to FieldInfo. + ## 0.7.7 - 2017-09-25 * Avoid name clashes between import prefix and field names. diff --git a/lib/code_generator.dart b/lib/code_generator.dart index 8c17f01..862ac82 100644 --- a/lib/code_generator.dart +++ b/lib/code_generator.dart @@ -46,7 +46,8 @@ class CodeGenerator extends ProtobufContainer { Dart_options.registerAllExtensions(extensions); _streamIn - .fold(new BytesBuilder(), (builder, data) => builder..add(data)) + .fold(new BytesBuilder(), + (BytesBuilder builder, data) => builder..add(data)) .then((builder) => builder.takeBytes()) .then((List bytes) { var request = new CodeGeneratorRequest.fromBuffer(bytes, extensions); diff --git a/lib/extension_generator.dart b/lib/extension_generator.dart index 977bffa..3ae54ae 100644 --- a/lib/extension_generator.dart +++ b/lib/extension_generator.dart @@ -85,7 +85,8 @@ class ExtensionGenerator { if (type.isMessage || type.isGroup) { out.println(', $dartType.$checkItem, $dartType.create);'); } else if (type.isEnum) { - out.println(', $dartType.$checkItem, null, $dartType.valueOf);'); + out.println(', $dartType.$checkItem, null, ' + '$dartType.valueOf, $dartType.values);'); } else { out.println(", getCheckFunction(${_field.typeConstant}));"); } @@ -102,8 +103,8 @@ class ExtensionGenerator { out.println(', $initializer, $dartType.create);'); } else if (type.isEnum) { var dartEnum = type.getDartType(package); - String valueOf = '(var v) => $dartEnum.valueOf(v)'; - out.println(", $initializer, null, $valueOf);"); + String enumParams = '(var v) => $dartEnum.valueOf(v), $dartEnum.values'; + out.println(", $initializer, null, $enumParams);"); } else if (initializer != null) { out.println(", $initializer);"); } else { diff --git a/lib/protobuf_field.dart b/lib/protobuf_field.dart index 0238cfc..98a06a8 100644 --- a/lib/protobuf_field.dart +++ b/lib/protobuf_field.dart @@ -110,7 +110,7 @@ class ProtobufField { ' $type.$checkItem, $type.create)'; } else if (baseType.isEnum) { return '..pp<$type>($number, $quotedName, $typeConstant,' - ' $type.$checkItem, null, $type.valueOf)'; + ' $type.$checkItem, null, $type.valueOf, $type.values)'; } else { return '..p<$type>($number, $quotedName, $typeConstant)'; } @@ -118,9 +118,9 @@ class ProtobufField { String makeDefault = generateDefaultFunction(package); if (baseType.isEnum) { - String valueOf = '$type.valueOf'; + String enumParams = '$type.valueOf, $type.values'; return '..e<$type>(' - '$number, $quotedName, $typeConstant, $makeDefault, $valueOf)'; + '$number, $quotedName, $typeConstant, $makeDefault, $enumParams)'; } String prefix = '..a<$type>($number, $quotedName, $typeConstant'; diff --git a/lib/src/descriptor.pb.dart b/lib/src/descriptor.pb.dart index b5ad945..e2c15b6 100644 --- a/lib/src/descriptor.pb.dart +++ b/lib/src/descriptor.pb.dart @@ -354,13 +354,15 @@ class FieldDescriptorProto extends GeneratedMessage { 'label', PbFieldType.OE, FieldDescriptorProto_Label.LABEL_OPTIONAL, - FieldDescriptorProto_Label.valueOf) + FieldDescriptorProto_Label.valueOf, + FieldDescriptorProto_Label.values) ..e( 5, 'type', PbFieldType.OE, FieldDescriptorProto_Type.TYPE_DOUBLE, - FieldDescriptorProto_Type.valueOf) + FieldDescriptorProto_Type.valueOf, + FieldDescriptorProto_Type.values) ..a(6, 'typeName', PbFieldType.OS) ..a(7, 'defaultValue', PbFieldType.OS) ..a(8, 'options', PbFieldType.OM, FieldOptions.getDefault, @@ -786,8 +788,13 @@ class FileOptions extends GeneratedMessage { static final BuilderInfo _i = new BuilderInfo('FileOptions') ..a(1, 'javaPackage', PbFieldType.OS) ..a(8, 'javaOuterClassname', PbFieldType.OS) - ..e(9, 'optimizeFor', PbFieldType.OE, - FileOptions_OptimizeMode.SPEED, FileOptions_OptimizeMode.valueOf) + ..e( + 9, + 'optimizeFor', + PbFieldType.OE, + FileOptions_OptimizeMode.SPEED, + FileOptions_OptimizeMode.valueOf, + FileOptions_OptimizeMode.values) ..a(10, 'javaMultipleFiles', PbFieldType.OB) ..a(11, 'goPackage', PbFieldType.OS) ..a(16, 'ccGenericServices', PbFieldType.OB) @@ -1041,13 +1048,23 @@ class _ReadonlyMessageOptions extends MessageOptions with ReadonlyMessageMixin { class FieldOptions extends GeneratedMessage { static final BuilderInfo _i = new BuilderInfo('FieldOptions') - ..e(1, 'ctype', PbFieldType.OE, - FieldOptions_CType.STRING, FieldOptions_CType.valueOf) + ..e( + 1, + 'ctype', + PbFieldType.OE, + FieldOptions_CType.STRING, + FieldOptions_CType.valueOf, + FieldOptions_CType.values) ..a(2, 'packed', PbFieldType.OB) ..a(3, 'deprecated', PbFieldType.OB) ..a(5, 'lazy', PbFieldType.OB) - ..e(6, 'jstype', PbFieldType.OE, - FieldOptions_JSType.JS_NORMAL, FieldOptions_JSType.valueOf) + ..e( + 6, + 'jstype', + PbFieldType.OE, + FieldOptions_JSType.JS_NORMAL, + FieldOptions_JSType.valueOf, + FieldOptions_JSType.values) ..a(10, 'weak', PbFieldType.OB) ..pp(999, 'uninterpretedOption', PbFieldType.PM, UninterpretedOption.$checkItem, UninterpretedOption.create) @@ -1307,7 +1324,8 @@ class MethodOptions extends GeneratedMessage { 'idempotencyLevel', PbFieldType.OE, MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN, - MethodOptions_IdempotencyLevel.valueOf) + MethodOptions_IdempotencyLevel.valueOf, + MethodOptions_IdempotencyLevel.values) ..pp(999, 'uninterpretedOption', PbFieldType.PM, UninterpretedOption.$checkItem, UninterpretedOption.create) ..hasExtensions = true; diff --git a/pubspec.yaml b/pubspec.yaml index 58b2eb0..48fd204 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: protoc_plugin -version: 0.7.7 +version: 0.7.8 author: Dart Team description: Protoc compiler plugin to generate Dart code homepage: https://github.com/dart-lang/dart-protoc-plugin @@ -8,7 +8,7 @@ environment: dependencies: fixnum: ^0.10.5 path: ^1.0.0 - protobuf: ^0.5.5 + protobuf: ^0.6.0 dart_style: ^1.0.6 dev_dependencies: browser: any diff --git a/test/message_generator_test.dart b/test/message_generator_test.dart index 5f0d570..c207869 100755 --- a/test/message_generator_test.dart +++ b/test/message_generator_test.dart @@ -44,7 +44,7 @@ class PhoneNumber_PhoneType extends ProtobufEnum { class PhoneNumber extends GeneratedMessage { static final BuilderInfo _i = new BuilderInfo('PhoneNumber') ..a(1, 'number', PbFieldType.QS) - ..e(2, 'type', PbFieldType.OE, PhoneNumber_PhoneType.MOBILE, PhoneNumber_PhoneType.valueOf) + ..e(2, 'type', PbFieldType.OE, PhoneNumber_PhoneType.MOBILE, PhoneNumber_PhoneType.valueOf, PhoneNumber_PhoneType.values) ..a(3, 'name', PbFieldType.OS, '\$') ;