Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export GeneratedMessageGenericExtensions in generated files #907

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading