Git Product home page Git Product logo

Comments (5)

sgjesse avatar sgjesse commented on May 5, 2024

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.

sigmundch avatar sigmundch commented on May 5, 2024

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.

sgjesse avatar sgjesse commented on May 5, 2024

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.

sigmundch avatar sigmundch commented on May 5, 2024

Awesome to hear it's possible, yay!

from protobuf.dart.

sgjesse avatar sgjesse commented on May 5, 2024

Fixed by change dart-archive/dart-protoc-plugin@24f06a2 to the protoc Dart plugin.

from protobuf.dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.