Comments (5)
This turns out to be pretty nasty. Consider the following example.
File package1.proto
:
package p1;
message M {
optional int32 x = 1;
}
File package2.proto
:
package p2;
message M {
optional int32 x = 1;
}
File test.proto
:
import "package1.proto";
import "package2.proto";
message M {
optional p1.M x = 1;
optional p2.M y = 2;
}
This will generate the following test.pb.dart
Dart code for test.proto
:
///
// Generated code. Do not modify.
///
library test;
import 'package:fixnum/fixnum.dart';
import 'package:protobuf/protobuf.dart';
import 'package1.pb.dart';
import 'package2.pb.dart';
class M extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('M')
..a(1, 'x', GeneratedMessage.OM, () => new M(), () => new M())
..a(2, 'y', GeneratedMessage.OM, () => new M(), () => new M())
..hasRequiredFields = false
;
M() : super();
M.fromBuffer(List<int> i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromBuffer(i, r);
M.fromJson(String i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromJson(i, r);
M clone() => new M()..mergeFromMessage(this);
BuilderInfo get info_ => _i;
M get x => getField(1);
void set x(M v) { setField(1, v); }
bool hasX() => hasField(1);
void clearX() => clearField(1);
M get y => getField(2);
void set y(M v) { setField(2, v); }
bool hasY() => hasField(2);
void clearY() => clearField(2);
}
This is pretty wrong. The M
's are not distinguished in any way.
When generating C++ the protobuf package hierarchy (dot separated names) is generated as a namespace hierarchy (e.g. class M
from package1.proto
is ::p1::M
). For Java code the protobuf package hierarchy is turned into Java packages.
However Dart does not have this type of namespace hierarchy. When importing one can use the as
keyword to define a prefix for different libraries used.
///
// Generated code. Do not modify.
///
library test;
import 'package:fixnum/fixnum.dart';
import 'package:protobuf/protobuf.dart';
import 'package1.pb.dart' as p1;
import 'package2.pb.dart' as p2;
class M extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('M')
..a(1, 'x', GeneratedMessage.OM, () => new p1.M(), () => new p1.M())
..a(2, 'y', GeneratedMessage.OM, () => new p2.M(), () => new p2.M())
..hasRequiredFields = false
;
M() : super();
M.fromBuffer(List<int> i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromBuffer(i, r);
M.fromJson(String i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromJson(i, r);
M clone() => new M()..mergeFromMessage(this);
BuilderInfo get info_ => _i;
p1.M get x => getField(1);
void set x(p1.M v) { setField(1, v); }
bool hasX() => hasField(1);
void clearX() => clearField(1);
p2.M get y => getField(2);
void set y(p2.M v) { setField(2, v); }
bool hasY() => hasField(2);
void clearY() => clearField(2);
}
However this is just trying to locally fix the name-clash in the generated code. It does not capture the fact that the protobuf packages defines the unique names .M
, .p1.M
and .p2.M
. Also generating like this is not possible, as the information passed to the protoc plug-in does not contain information to do this.
As the protobuf packages defines unique names .M
, .p1.M
and .p2.M
we should generate unique names in Dart as well. These could be M
, p1_M
and p2_M
.
///
// Generated code. Do not modify.
///
library test;
import 'package:fixnum/fixnum.dart';
import 'package:protobuf/protobuf.dart';
import 'package1.pb.dart';
import 'package2.pb.dart';
class M extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('M')
..a(1, 'x', GeneratedMessage.OM, () => new p1_M(), () => new p1_M())
..a(2, 'y', GeneratedMessage.OM, () => new p2_M(), () => new p2_M())
..hasRequiredFields = false
;
M() : super();
M.fromBuffer(List<int> i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromBuffer(i, r);
M.fromJson(String i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromJson(i, r);
M clone() => new M()..mergeFromMessage(this);
BuilderInfo get info_ => _i;
p1_M get x => getField(1);
void set x(p1_M v) { setField(1, v); }
bool hasX() => hasField(1);
void clearX() => clearField(1);
p2_M get y => getField(2);
void set y(p2_M v) { setField(2, v); }
bool hasY() => hasField(2);
void clearY() => clearField(2);
}
Then using this will look like this:
import "test.pb.dart";
import 'package1.pb.dart';
import 'package2.pb.dart';
main() {
M m = new M();
m.x = new p1_M();
m.y = new p2_M();
}
The main problem with this is that it is a breaking change.
from protobuf.dart.
About:
However this is just trying to locally fix the name-clash in the generated code. It does not capture the fact that the protobuf packages defines the unique names .M, .p1.M and .p2.M.
I don't entirely agree here. Dart does guarantee that if two names are defined in separate libraries, they are different. So unlike Java/C++, I think we get uniqueness that way regardless of the package name. So I could see just using the package name for local disambiguation to be OK.
Also generating like this is not possible, as the information passed to the protoc plug-in does not contain information to do this.
I do agree that this is a big issue. I wonder if there is anything we could do. For example, do we compile things separately or when we see the import could we also read the sources mentioned by it and find the package prefix? Could there be anything we can pass to the compiler to help here?
from protobuf.dart.
After looking at the data available from protoc to the plugin it turned out that it is possible to generate import prefixes as described above. Proposed change in https://chromiumcodereview.appspot.com/93743006.
from protobuf.dart.
Awesome to hear it's possible, yay!
from protobuf.dart.
Fixed by change dart-archive/dart-protoc-plugin@24f06a2 to the protoc Dart plugin.
from protobuf.dart.
Related Issues (20)
- grpc-web request from linux client
- Consider generating actual `enum`s
- Unknown Fields HOT 12
- How do you generate via buf? HOT 2
- Error: Couldn't find constructor 'GrpcServiceName'. HOT 8
- Generate nullable types from WKT wrappers HOT 1
- Bad state: No element HOT 3
- GeneratedMessage constructors with `result` parameter conflict with `result` local var. HOT 1
- toProto3Json converts Int64 values to strings HOT 1
- Impossible to distinguish the `oneof` fields from non-`oneof` fields when creating/using a generated class instance. HOT 4
- How to access google.protobuf.FieldOptions extensions? HOT 1
- Prohibit ..deepCopy() call HOT 1
- Generated gRPC client method stubs get no comments or deprecation annotations HOT 1
- Incompatibility with Java-generated Serialized Protocol Buffer in Dart Protocol Buffers Library
- `protoc` does not generate built-in types HOT 1
- The `.values` field of a generated enum class contains the zero-valued unspecified value HOT 1
- Inconvenient to add an alternate name to each enum value using Enum Value Options
- [protoc_plugin] Error `The method '$_clearField' isn't defined for the class 'DartMixin'.` HOT 1
- [protoc_plugin] Plugin output is unparseable HOT 1
- Error: A value of type 'List<dynamic>' can't be returned from the function 'symbol' because it has a return type of 'PbList<ProtoOALightSymbol>'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf.dart.