Fix issues with reserved names
- Get GeneratedMessage reserved names from the protobuf package.
- reserve "create" and "createRepeated" to avoid a conflict.
BUG=https://github.com/dart-lang/dart-protoc-plugin/issues/46
R=sgjesse@google.com
Review URL: https://chromiumcodereview.appspot.com//1225573004.
diff --git a/lib/message_generator.dart b/lib/message_generator.dart
index 6dc1b25..796c63b 100644
--- a/lib/message_generator.dart
+++ b/lib/message_generator.dart
@@ -16,20 +16,9 @@
'super', 'switch', 'this', 'throw', 'true', 'try', 'var', 'void',
'while', 'with'];
- // List of names which cannot be used in a subclass of GeneratedMessage.
- static final List<String> reservedNames =
- ['hashCode', 'noSuchMethod','runtimeType', 'toString',
- 'fromBuffer', 'fromJson', 'hasRequiredFields', 'isInitialized',
- 'clear', 'getTagNumber', 'check',
- 'writeToBuffer', 'writeToCodedBufferWriter',
- 'mergeFromCodedBufferReader', 'mergeFromBuffer',
- 'writeToJson', 'mergeFromJson',
- 'writeToJsonMap', 'mergeFromJsonMap',
- 'addExtension', 'getExtension', 'setExtension',
- 'hasExtension', 'clearExtension',
- 'getField', 'setField', 'hasField', 'clearField',
- 'extensionsAreInitialized', 'mergeFromMessage', 'mergeUnknownFields',
- '==', 'info_', 'GeneratedMessage', 'Object'];
+ // List of names used in the generated class itself
+ static final List<String> generatedNames =
+ ['create', 'createRepeated'];
// Returns the mixin for this message, or null if none.
static PbMixin _getMixin(DescriptorProto desc, PbMixin defaultValue) {
@@ -110,7 +99,8 @@
void generate(IndentingWriter out) {
_methodNames.clear();
_methodNames.addAll(reservedWords);
- _methodNames.addAll(reservedNames);
+ _methodNames.addAll(GeneratedMessage_reservedNames);
+ _methodNames.addAll(generatedNames);
if (mixin != null) {
_methodNames.addAll(mixin.findReservedNames());
diff --git a/lib/protoc.dart b/lib/protoc.dart
index c5160c6..9cc50e2 100644
--- a/lib/protoc.dart
+++ b/lib/protoc.dart
@@ -3,6 +3,7 @@
import 'dart:async';
import 'dart:io';
+import 'package:protobuf/meta.dart';
import 'package:protobuf/protobuf.dart';
import 'package:protobuf/mixins_meta.dart';
import 'package:path/path.dart' as path;
diff --git a/pubspec.yaml b/pubspec.yaml
index 8eeffff..fee6cae 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,12 +1,12 @@
name: protoc_plugin
-version: 0.3.10
+version: 0.3.11
author: Dart Team <misc@dartlang.org>
description: Protoc compiler plugin to generate Dart code
homepage: https://github.com/dart-lang/dart-protoc-plugin
environment:
sdk: '>=1.0.0 <2.0.0'
dependencies:
- protobuf: '>=0.3.10 <0.4.0'
+ protobuf: '>=0.3.11 <0.4.0'
path: '>=1.0.0 <2.0.0'
dev_dependencies:
unittest: '>=0.9.0 <0.11.0'
diff --git a/test/all_tests.dart b/test/all_tests.dart
index e362197..977b1c7 100755
--- a/test/all_tests.dart
+++ b/test/all_tests.dart
@@ -16,7 +16,6 @@
import 'message_generator_test.dart' as mgt;
import 'message_test.dart' as mt;
import 'protoc_options_test.dart' as pot;
-import 'reserved_names_test.dart' as rnt;
import 'service_test.dart' as st;
import 'service_generator_test.dart' as sgt;
import 'unknown_field_set_test.dart' as ufst;
@@ -37,7 +36,6 @@
mgt.main();
mt.main();
pot.main();
- rnt.main();
st.main();
sgt.main();
ufst.main();
diff --git a/test/protos/duplicate_names_import.proto b/test/protos/duplicate_names_import.proto
index 4af862a..fc0e46a 100644
--- a/test/protos/duplicate_names_import.proto
+++ b/test/protos/duplicate_names_import.proto
@@ -18,3 +18,7 @@
optional pkg1_pkg2.M m3 = 5;
optional pkg1_pkg2.M.M m3_m = 6;
}
+
+message M2 {
+ optional int32 pkg1 = 1;
+}
diff --git a/test/protos/reserved_names.proto b/test/protos/reserved_names.proto
index 2052eb9..fcee7c5 100644
--- a/test/protos/reserved_names.proto
+++ b/test/protos/reserved_names.proto
@@ -42,6 +42,8 @@
// Other conflicts.
optional int32 hash_code_1 = 40;
+ optional int32 create = 41;
+ optional int32 create_repeated = 42;
optional int32 x = 50;
optional int32 has_x = 51;
@@ -86,6 +88,8 @@
// Other conflicts.
repeated int32 hash_code_1 = 40;
+ repeated int32 create = 41;
+ repeated int32 create_repeated = 42;
repeated int32 x = 50;
repeated int32 has_x = 51;
@@ -130,6 +134,8 @@
// Other conflicts.
required int32 hash_code_1 = 40;
+ required int32 create = 41;
+ required int32 create_repeated = 42;
required int32 x = 50;
required int32 has_x = 51;
diff --git a/test/reserved_names_test.dart b/test/reserved_names_test.dart
deleted file mode 100755
index f41d176..0000000
--- a/test/reserved_names_test.dart
+++ /dev/null
@@ -1,43 +0,0 @@
-#!/usr/bin/env dart
-// Copyright (c) 2013, 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.
-
-library reserved_names_test;
-
-import 'dart:mirrors' as mirrors;
-
-import 'package:unittest/unittest.dart';
-
-import '../lib/protoc.dart' show MessageGenerator;
-
-import 'test_util.dart';
-
-void main() {
- test('testReservedNamesList', () {
- Set<String> names = new Set<String>();
- fillConflictingNames(mirrors.ClassMirror cls) {
- String className = mirrors.MirrorSystem.getName(cls.simpleName);
- names.addAll(
- cls
- .declarations
- .values
- .where((decl) => !decl.isPrivate && decl is !mirrors.VariableMirror)
- .map((m) => mirrors.MirrorSystem.getName(m.simpleName))
- .map((n) => n.startsWith(className + '.')
- ? n.substring(className.length + 1) : n)
- .toList());
- }
-
- var cls = mirrors.currentMirrorSystem()
- .libraries[Uri.parse('package:protobuf/protobuf.dart')]
- .declarations[#GeneratedMessage];
- do {
- fillConflictingNames(cls);
- cls = cls.superclass;
- } while (cls != null);
-
- var x = new Set<String>()..addAll(MessageGenerator.reservedNames);
- expect(names.toList()..sort(), equals(x.toList()..sort()));
- });
-}