Skip to content

Commit

Permalink
Export GeneratedMessageGenericExtensions in generated files (#907)
Browse files Browse the repository at this point in the history
Currently when a user calls deprecated methods `clone` or `copyWith`,
the deprecation message points to `GeneratedMessageGenericExtensions`
`deepCopy` and `rebuild` methods.

However we can't just replace `clone` with `deepCopy` as currently the
extension is not exported by the generated files. Instead we need to
import the library explicitly in the use site.

We could mention this in the deprecation message ("Use rebuild from
protobuf library instead"), but it's more convenient to just export the
extension in the generated message files.

Closes #503.
  • Loading branch information
osa1 authored Jan 6, 2024
1 parent 20ec685 commit c4fd596
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 36 deletions.
1 change: 0 additions & 1 deletion benchmarks/bin/deep_copy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.

import 'package:protobuf/protobuf.dart';
import 'package:protobuf_benchmarks/benchmark_base.dart';
import 'package:protobuf_benchmarks/generated/google_message1_proto2.pb.dart'
as p2;
Expand Down
4 changes: 4 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
fields is now `PbMap` (instead of `Map`). ([#903])

This change requires protobuf-4.0.0.
* Generated files now export `GeneratedMessageGenericExtensions` from the
protobuf library. ([#503], [#907])

[#738]: https://github.com/google/protobuf.dart/issues/738
[#903]: https://github.com/google/protobuf.dart/pull/903
[#503]: https://github.com/google/protobuf.dart/issues/503
[#907]: https://github.com/google/protobuf.dart/pull/907

## 21.1.2

Expand Down
25 changes: 19 additions & 6 deletions protoc_plugin/lib/indenting_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,16 @@ class ImportWriter {
final Set<String> _dartImports = SplayTreeSet<String>();
final Set<String> _packageImports = SplayTreeSet<String>();
final Set<String> _fileImports = SplayTreeSet<String>();
final Set<String> _exports = SplayTreeSet<String>();
final Set<String> _packageExports = SplayTreeSet<String>();
final Set<String> _fileExports = SplayTreeSet<String>();

/// Whether any imports were written.
bool get hasImports =>
_dartImports.isNotEmpty ||
_packageImports.isNotEmpty ||
_fileImports.isNotEmpty ||
_exports.isNotEmpty;
_packageExports.isNotEmpty ||
_fileExports.isNotEmpty;

/// Add an import with an optional import prefix.
void addImport(String url, {String? prefix}) {
Expand All @@ -196,8 +198,15 @@ class ImportWriter {
}

/// And an export.
void addExport(String url) {
_exports.add("export '$url';");
void addExport(String url, {List<String> members = const []}) {
final directive = members.isNotEmpty
? "export '$url' show ${members.join(', ')};"
: "export '$url';";
if (url.startsWith('package:')) {
_packageExports.add(directive);
} else {
_fileExports.add(directive);
}
}

/// Return the generated text for the set of imports.
Expand All @@ -215,9 +224,13 @@ class ImportWriter {
if (buf.isNotEmpty) buf.writeln();
_fileImports.forEach(buf.writeln);
}
if (_exports.isNotEmpty) {
if (_packageExports.isNotEmpty) {
if (buf.isNotEmpty) buf.writeln();
_packageExports.forEach(buf.writeln);
}
if (_fileExports.isNotEmpty) {
if (buf.isNotEmpty) buf.writeln();
_exports.forEach(buf.writeln);
_fileExports.forEach(buf.writeln);
}

return buf.toString();
Expand Down
3 changes: 3 additions & 0 deletions protoc_plugin/lib/src/file_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ class FileGenerator extends ProtobufContainer {
_addImport(importWriter, config, target, '.pbenum.dart');
}

importWriter.addExport(_protobufImportUrl,
members: ['GeneratedMessageGenericExtensions']);

for (final publicDependency in descriptor.publicDependency) {
_addExport(importWriter, config,
Uri.file(descriptor.dependency[publicDependency]), '.pb.dart');
Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/grpc_service.pb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'dart:core' as $core;

import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

class Empty extends $pb.GeneratedMessage {
factory Empty() => create();
Empty._() : super();
Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/header_in_package.pb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ import 'dart:core' as $core;

import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/header_with_fixnum.pb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ import 'dart:core' as $core;
import 'package:fixnum/fixnum.dart' as $fixnum;
import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/imports.pb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'package:protobuf/protobuf.dart' as $pb;
import 'package1.pb.dart' as $1;
import 'package2.pb.dart' as $2;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

class M extends $pb.GeneratedMessage {
factory M() => create();
M._() : super();
Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/int64.pb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'dart:core' as $core;
import 'package:fixnum/fixnum.dart' as $fixnum;
import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

class Int64 extends $pb.GeneratedMessage {
factory Int64() => create();
Int64._() : super();
Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/oneMessage.pb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'dart:core' as $core;

import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

class PhoneNumber extends $pb.GeneratedMessage {
factory PhoneNumber() => create();
PhoneNumber._() : super();
Expand Down
56 changes: 28 additions & 28 deletions protoc_plugin/test/goldens/oneMessage.pb.meta
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,121 @@ annotation: {
path: 4
path: 0
sourceFile: test
begin: 445
end: 456
begin: 526
end: 537
}
annotation: {
path: 4
path: 0
sourceFile: test
begin: 527
end: 538
begin: 608
end: 619
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile: test
begin: 2247
end: 2253
begin: 2328
end: 2334
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile: test
begin: 2295
end: 2301
begin: 2376
end: 2382
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile: test
begin: 2374
end: 2383
begin: 2455
end: 2464
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile: test
begin: 2426
end: 2437
begin: 2507
end: 2518
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile: test
begin: 2495
end: 2499
begin: 2576
end: 2580
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile: test
begin: 2541
end: 2545
begin: 2622
end: 2626
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile: test
begin: 2620
end: 2627
begin: 2701
end: 2708
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile: test
begin: 2670
end: 2679
begin: 2751
end: 2760
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile: test
begin: 2740
end: 2744
begin: 2821
end: 2825
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile: test
begin: 2791
end: 2795
begin: 2872
end: 2876
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile: test
begin: 2868
end: 2875
begin: 2949
end: 2956
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile: test
begin: 2918
end: 2927
begin: 2999
end: 3008
}
2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/service.pb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'dart:core' as $core;

import 'package:protobuf/protobuf.dart' as $pb;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

class Empty extends $pb.GeneratedMessage {
factory Empty() => create();
Empty._() : super();
Expand Down
2 changes: 2 additions & 0 deletions protoc_plugin/test/goldens/topLevelEnum.pb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@

import 'dart:core' as $core;

export 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

export 'test.pbenum.dart';

1 change: 0 additions & 1 deletion protoc_plugin/test/oneof_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.

import 'package:protobuf/protobuf.dart';
import 'package:test/test.dart';

import '../out/protos/oneof.pb.dart';
Expand Down

0 comments on commit c4fd596

Please sign in to comment.