Skip to content

Commit

Permalink
Make protobuf message and field names dependent on a fromEnvironment …
Browse files Browse the repository at this point in the history
…constants (google#403)
  • Loading branch information
iinozemtsev authored Sep 1, 2020
1 parent e96efd5 commit 8702755
Show file tree
Hide file tree
Showing 22 changed files with 876 additions and 319 deletions.
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/extension_field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class _ExtensionFieldSet {
List<T> _ensureRepeatedField<T>(Extension<T> fi) {
assert(!_isReadOnly);
assert(fi.isRepeated);
assert(fi.extendee == _parent._messageName);
assert(fi.extendee == '' || fi.extendee == _parent._messageName);

var list = _values[fi.tagNumber];
if (list != null) return list as List<T>;
Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

If a target is built with `dart_env = {"protobuf.omit_enum_names": "true"}`
enum names will not be present in the compiled binary.
* Make message and field names dependenc on a fromEnvironment constants
`protobuf.omit_message_names` and `protobuf.omit_field_names` respectively.
* Omit type on a left hand side of generated static fields for extensions,
which results in stricter type (`Extension<ExtensionType>` instead of just
`Extension`).
Expand Down
8 changes: 3 additions & 5 deletions protoc_plugin/lib/enum_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ class EnumGenerator extends ProtobufContainer {
for (var i = 0; i < _canonicalValues.length; i++) {
var val = _canonicalValues[i];
final name = dartNames[val.name];
final conditionalValName = configurationDependent(
'protobuf.omit_enum_names', quoted(val.name));
out.printlnAnnotated(
'static const ${classname} $name = '
'${classname}._(${val.number}, '
'const $_coreImportPrefix.bool.fromEnvironment'
'(\'protobuf.omit_enum_names\') ? \'\' : '
'${singleQuote(val.name)}'
');',
'${classname}._(${val.number}, $conditionalValName);',
[
NamedLocation(
name: name,
Expand Down
9 changes: 7 additions & 2 deletions protoc_plugin/lib/extension_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ class ExtensionGenerator {
if (_field == null) throw StateError("resolve not called");

var name = _extensionName;
final conditionalName = configurationDependent(
'protobuf.omit_field_names', quoted(_extensionName));
var type = _field.baseType;
var dartType = type.getDartType(fileGen);
final conditionalExtendedName = configurationDependent(
'protobuf.omit_message_names', quoted(_extendedFullName));

String invocation;
var positionals = <String>[];
positionals.add("'$_extendedFullName'");
positionals.add("'${_descriptor.jsonName}'");
positionals.add(conditionalExtendedName);
positionals.add(conditionalName);
positionals.add('${_field.number}');
positionals.add(_field.typeConstant);

Expand Down
9 changes: 9 additions & 0 deletions protoc_plugin/lib/file_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ const String _coreImport = "import 'dart:core' as $_coreImportPrefix;";
const String _grpcImport =
"import 'package:grpc/service_api.dart' as $_grpcImportPrefix;";

/// Generates code that will evaluate to the empty string if
/// `const bool.fromEnvironment(envName)` is `true` and evaluate to [value]
/// otherwise.
String configurationDependent(String envName, String value) {
return 'const $_coreImportPrefix.bool.fromEnvironment(${quoted(envName)})'
' ? \'\' '
': $value';
}

/// Generates the Dart output files for one .proto input file.
///
/// Outputs include .pb.dart, pbenum.dart, and .pbjson.dart.
Expand Down
14 changes: 10 additions & 4 deletions protoc_plugin/lib/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ class MessageGenerator extends ProtobufContainer {
mixinClause = ' with ${mixinNames.join(", ")}';
}

var packageClause = package == ''
? ''
: ', package: const $_protobufImportPrefix.PackageName(\'$package\')';
final conditionalPackageName = 'const $_protobufImportPrefix.PackageName(' +
configurationDependent('protobuf.omit_message_names', quoted(package)) +
')';

var packageClause =
package == '' ? '' : ', package: $conditionalPackageName';
var proto3JsonClause = (mixin?.hasProto3JsonHelpers ?? false)
? ', toProto3Json: $_mixinImportPrefix.${mixin.name}.toProto3JsonHelper, '
'fromProto3Json: $_mixinImportPrefix.${mixin.name}.fromProto3JsonHelper'
Expand All @@ -318,9 +321,12 @@ class MessageGenerator extends ProtobufContainer {
out.println('0 : ${oneof.oneofEnumName}.notSet');
});
}
final conditionalMessageName = configurationDependent(
'protobuf.omit_message_names', quoted(messageName));
out.addBlock(
'static final $_protobufImportPrefix.BuilderInfo _i = '
'$_protobufImportPrefix.BuilderInfo(\'${messageName}\'$packageClause'
'$_protobufImportPrefix.BuilderInfo($conditionalMessageName'
'$packageClause'
', createEmptyInstance: create'
'$proto3JsonClause)',
';', () {
Expand Down
7 changes: 4 additions & 3 deletions protoc_plugin/lib/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ class ProtobufField {
/// [fileGen] represents the .proto file where the code will be evaluated.
String generateBuilderInfoCall(FileGenerator fileGen, String package) {
assert(descriptor.hasJsonName());
// JSON names should be serialized as-is, but '$' can cause Dart to try to
// perform string interpolation on non-existent variables.
var quotedName = "'${descriptor.jsonName.replaceAll(r'$', r'\$')}'";
var quotedName = configurationDependent(
'protobuf.omit_field_names',
quoted(descriptor.jsonName),
);

var type = baseType.getDartType(fileGen);

Expand Down
115 changes: 93 additions & 22 deletions protoc_plugin/lib/src/dart_options.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ import 'dart:core' as $core;
import 'package:protobuf/protobuf.dart' as $pb;

class DartMixin extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo('DartMixin',
package: const $pb.PackageName('dart_options'),
static final $pb.BuilderInfo _i = $pb.BuilderInfo(
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'DartMixin',
package: const $pb.PackageName(
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'dart_options'),
createEmptyInstance: create)
..aOS(1, 'name')
..aOS(2, 'importFrom')
..aOS(3, 'parent')
..aOS(
1,
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'name')
..aOS(
2,
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'importFrom')
..aOS(
3,
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'parent')
..hasRequiredFields = false;

DartMixin._() : super();
Expand Down Expand Up @@ -84,10 +102,21 @@ class DartMixin extends $pb.GeneratedMessage {
}

class Imports extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo('Imports',
package: const $pb.PackageName('dart_options'),
static final $pb.BuilderInfo _i = $pb.BuilderInfo(
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'Imports',
package: const $pb.PackageName(
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'dart_options'),
createEmptyInstance: create)
..pc<DartMixin>(1, 'mixins', $pb.PbFieldType.PM,
..pc<DartMixin>(
1,
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'mixins',
$pb.PbFieldType.PM,
subBuilder: DartMixin.create)
..hasRequiredFields = false;

Expand Down Expand Up @@ -125,37 +154,79 @@ class Imports extends $pb.GeneratedMessage {

class Dart_options {
static final imports = $pb.Extension<Imports>(
'google.protobuf.FileOptions', 'imports', 28125061, $pb.PbFieldType.OM,
defaultOrMaker: Imports.getDefault, subBuilder: Imports.create);
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FileOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'imports',
28125061,
$pb.PbFieldType.OM,
defaultOrMaker: Imports.getDefault,
subBuilder: Imports.create);
static final defaultMixin = $pb.Extension<$core.String>(
'google.protobuf.FileOptions',
'defaultMixin',
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FileOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'defaultMixin',
96128839,
$pb.PbFieldType.OS);
static final mixin = $pb.Extension<$core.String>(
'google.protobuf.MessageOptions', 'mixin', 96128839, $pb.PbFieldType.OS);
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.MessageOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'mixin',
96128839,
$pb.PbFieldType.OS);
static final overrideGetter = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideGetter',
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FieldOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'overrideGetter',
28205290,
$pb.PbFieldType.OB);
static final overrideSetter = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideSetter',
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FieldOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'overrideSetter',
28937366,
$pb.PbFieldType.OB);
static final overrideHasMethod = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideHasMethod',
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FieldOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'overrideHasMethod',
28937461,
$pb.PbFieldType.OB);
static final overrideClearMethod = $pb.Extension<$core.bool>(
'google.protobuf.FieldOptions',
'overrideClearMethod',
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FieldOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'overrideClearMethod',
28907907,
$pb.PbFieldType.OB);
static final dartName = $pb.Extension<$core.String>(
'google.protobuf.FieldOptions', 'dartName', 28700919, $pb.PbFieldType.OS);
const $core.bool.fromEnvironment('protobuf.omit_message_names')
? ''
: 'google.protobuf.FieldOptions',
const $core.bool.fromEnvironment('protobuf.omit_field_names')
? ''
: 'dartName',
28700919,
$pb.PbFieldType.OS);
static void registerAllExtensions($pb.ExtensionRegistry registry) {
registry.add(imports);
registry.add(defaultMixin);
Expand Down
Loading

0 comments on commit 8702755

Please sign in to comment.