Convert the field started with underscore and digit to a legal name (#651)
In proto it is allowed to name a field started with underscore and
digit like `_3d`. But it's illegal in Dart. Convert the field name when
generating code.
Port of cl/411592880
diff --git a/protoc_plugin/lib/names.dart b/protoc_plugin/lib/names.dart
index 170fb0b..f96d861 100644
--- a/protoc_plugin/lib/names.dart
+++ b/protoc_plugin/lib/names.dart
@@ -420,7 +420,8 @@
_memberNamesSuffix(field.number),
generateVariants: generateNameVariants);
- return FieldNames(field, index, sourcePosition, _defaultFieldName(name),
+ return FieldNames(field, index, sourcePosition,
+ avoidInitialUnderscore(_defaultFieldName(name)),
hasMethodName: _defaultHasMethodName(name),
clearMethodName: _defaultClearMethodName(name),
ensureMethodName:
diff --git a/protoc_plugin/test/names_test.dart b/protoc_plugin/test/names_test.dart
index 9fa3a0d..a9ac9ce 100644
--- a/protoc_plugin/test/names_test.dart
+++ b/protoc_plugin/test/names_test.dart
@@ -30,6 +30,12 @@
expect(msg.hasRenamedField(), false);
});
+ test('Can access a filed started with underscore and digit', () {
+ var msg = pb.UnderscoreDigitName();
+ msg.x3d = 'one';
+ expect(msg.getField(1), 'one');
+ });
+
test('Can swap field names using dart_name option', () {
var msg = pb.SwapNames();
msg.first = 'one';
diff --git a/protoc_plugin/test/protos/dart_name.proto b/protoc_plugin/test/protos/dart_name.proto
index 533d43c..a09c742 100644
--- a/protoc_plugin/test/protos/dart_name.proto
+++ b/protoc_plugin/test/protos/dart_name.proto
@@ -37,3 +37,7 @@
message Function_ {
optional string fun1 = 1;
}
+
+message UnderscoreDigitName {
+ optional string _3d = 1;
+}