diff --git a/protoc_plugin/CHANGELOG.md b/protoc_plugin/CHANGELOG.md index a0814f164..6860df557 100644 --- a/protoc_plugin/CHANGELOG.md +++ b/protoc_plugin/CHANGELOG.md @@ -3,6 +3,8 @@ * Update how we calculate import prefixes ([#1010]); import prefixes are now unique per-library instead of being unique across all generated libraries. * Ignore `unused_import` diagnostics for `*.pbjson.dart` files. +* Revert the change to not generate empty `*.pbenum.dart` files; these can be + exported from other enum files. [#1010]: https://github.com/google/protobuf.dart/issues/1010 diff --git a/protoc_plugin/Makefile b/protoc_plugin/Makefile index cacebe21d..f0fdfbb80 100644 --- a/protoc_plugin/Makefile +++ b/protoc_plugin/Makefile @@ -18,6 +18,7 @@ TEST_PROTO_LIST = \ google/protobuf/timestamp \ google/protobuf/type \ google/protobuf/unittest_import \ + google/protobuf/unittest_import_public \ google/protobuf/unittest_optimize_for \ google/protobuf/unittest_well_known_types \ google/protobuf/unittest \ @@ -69,7 +70,8 @@ TEST_PROTO_LIST = \ using_any TEST_PROTO_DIR=$(OUTPUT_DIR) TEST_PROTO_LIBS=$(foreach f, $(TEST_PROTO_LIST), \ - $(TEST_PROTO_DIR)/$(f).pb.dart \ + $(TEST_PROTO_DIR)/$(f).pb.dart \ + $(TEST_PROTO_DIR)/$(f).pbenum.dart \ $(TEST_PROTO_DIR)/$(f).pbjson.dart) TEST_PROTO_SRC_DIR=test/protos TEST_PROTO_SRCS=$(foreach proto, $(TEST_PROTO_LIST), \ diff --git a/protoc_plugin/lib/src/file_generator.dart b/protoc_plugin/lib/src/file_generator.dart index 18bba87e9..a78495817 100644 --- a/protoc_plugin/lib/src/file_generator.dart +++ b/protoc_plugin/lib/src/file_generator.dart @@ -232,11 +232,11 @@ class FileGenerator extends ProtobufContainer { } final mainWriter = generateMainFile(config); - final enumWriter = hasEnums ? generateEnumFile(config) : null; + final enumWriter = generateEnumFile(config); final files = [ makeFile('.pb.dart', mainWriter.toString()), - if (enumWriter != null) makeFile('.pbenum.dart', enumWriter.toString()), + makeFile('.pbenum.dart', enumWriter.toString()), // TODO(devoncarew): Consider not emitting empty json files. makeFile('.pbjson.dart', generateJsonFile(config)), ]; @@ -245,9 +245,8 @@ class FileGenerator extends ProtobufContainer { files.addAll([ makeFile('.pb.dart.meta', mainWriter.sourceLocationInfo.writeToJson().toString()), - if (enumWriter != null) - makeFile('.pbenum.dart.meta', - enumWriter.sourceLocationInfo.writeToJson().toString()) + makeFile('.pbenum.dart.meta', + enumWriter.sourceLocationInfo.writeToJson().toString()) ]); } if (options.useGrpc) { @@ -355,7 +354,7 @@ class FileGenerator extends ProtobufContainer { } // Export enums in main file for backward compatibility. - if (enumCount > 0) { + if (hasEnums) { final url = config.resolveImport(protoFileUri, protoFileUri, '.pbenum.dart'); importWriter.addExport(url.toString()); @@ -428,7 +427,7 @@ class FileGenerator extends ProtobufContainer { final importWriter = ImportWriter(); - if (enumCount > 0) { + if (hasEnums) { // Make sure any other symbols in dart:core don't cause name conflicts // with enums that have the same name. importWriter.addImport(_coreImportUrl, prefix: coreImportPrefix); diff --git a/protoc_plugin/lib/src/gen/dart_options.pbenum.dart b/protoc_plugin/lib/src/gen/dart_options.pbenum.dart new file mode 100644 index 000000000..528ff8ba3 --- /dev/null +++ b/protoc_plugin/lib/src/gen/dart_options.pbenum.dart @@ -0,0 +1,11 @@ +// +// Generated code. Do not modify. +// source: dart_options.proto +// +// @dart = 3.3 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names +// ignore_for_file: curly_braces_in_flow_control_structures +// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes +// ignore_for_file: non_constant_identifier_names diff --git a/protoc_plugin/lib/src/gen/google/api/http.pbenum.dart b/protoc_plugin/lib/src/gen/google/api/http.pbenum.dart new file mode 100644 index 000000000..78d2dc210 --- /dev/null +++ b/protoc_plugin/lib/src/gen/google/api/http.pbenum.dart @@ -0,0 +1,11 @@ +// +// Generated code. Do not modify. +// source: google/api/http.proto +// +// @dart = 3.3 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names +// ignore_for_file: curly_braces_in_flow_control_structures +// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes +// ignore_for_file: non_constant_identifier_names diff --git a/protoc_plugin/lib/src/gen/google/protobuf/duration.pbenum.dart b/protoc_plugin/lib/src/gen/google/protobuf/duration.pbenum.dart new file mode 100644 index 000000000..0a7e27f85 --- /dev/null +++ b/protoc_plugin/lib/src/gen/google/protobuf/duration.pbenum.dart @@ -0,0 +1,11 @@ +// +// Generated code. Do not modify. +// source: google/protobuf/duration.proto +// +// @dart = 3.3 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names +// ignore_for_file: curly_braces_in_flow_control_structures +// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes +// ignore_for_file: non_constant_identifier_names diff --git a/protoc_plugin/test/protos/google/protobuf/unittest_import.proto b/protoc_plugin/test/protos/google/protobuf/unittest_import.proto index 1e643201b..936fd34fe 100644 --- a/protoc_plugin/test/protos/google/protobuf/unittest_import.proto +++ b/protoc_plugin/test/protos/google/protobuf/unittest_import.proto @@ -1,55 +1,36 @@ // Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. -// http://code.google.com/p/protobuf/ // -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd // Author: kenton@google.com (Kenton Varda) // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. // // A proto file which is imported by unittest.proto to test importing. -// We don't put this in a package within proto2 because we need to make sure -// that the generated code doesn't depend on being in the proto2 namespace. -// In test_util.h we do -// "using namespace unittest_import = protobuf_unittest_import". syntax = "proto2"; +// We don't put this in a package within proto2 because we need to make sure +// that the generated code doesn't depend on being in the proto2 namespace. +// In test_util.h we do +// "using namespace unittest_import = proto2_unittest_import". package protobuf_unittest_import; option optimize_for = SPEED; +option cc_enable_arenas = true; -// Excercise the java_package option. +// Exercise the java_package option. option java_package = "com.google.protobuf.test"; // Do not set a java_outer_classname here to verify that Proto2 works without // one. +// Test public import +import public "google/protobuf/unittest_import_public.proto"; + message ImportMessage { optional int32 d = 1; } @@ -60,3 +41,9 @@ enum ImportEnum { IMPORT_BAZ = 9; } +// To use an enum in a map, it must has the first value as 0. +enum ImportEnumForMap { + UNKNOWN = 0; + FOO = 1; + BAR = 2; +} diff --git a/protoc_plugin/test/protos/google/protobuf/unittest_import_public.proto b/protoc_plugin/test/protos/google/protobuf/unittest_import_public.proto new file mode 100644 index 000000000..ff428143f --- /dev/null +++ b/protoc_plugin/test/protos/google/protobuf/unittest_import_public.proto @@ -0,0 +1,18 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +// Author: liujisi@google.com (Pherl Liu) + +syntax = "proto2"; + +package protobuf_unittest_import; + +option java_package = "com.google.protobuf.test"; + +message PublicImportMessage { + optional int32 e = 1; +}