This repository provides a plugin for the protoc compiler. It generates Dart files for working with data in protocol buffers format.
We only support the full proto2 schema. Proto3 should work due to backwards compatibility. See this issue list for proto3 schema features which are currently missing.
To compile a .proto file, you must use the 'protoc' command which is installed separately. Protobuf 3.0.0 or above is required.
The generated files are pure Dart code that run in either in the Dart VM or in a browser (using dart2js). They depend the protobuf Dart package. A Dart project that includes generated files should add "protobuf" to its pubspec.yaml file.
Note: currently the workflow is POSIX-oriented.
To build standalone protoc
plugin:
pub install
to install all dependenciesbin
directory to your PATH
,
or passing it directly with protoc
's --plugin
option.Please, remember that the plugin is pure Dart script and requires the presence
of dart
executable in your PATH
.
When both the dart
executable and bin/protoc-gen-dart
are in the
PATH
the protocol buffer compiler can be invoked to generate like this:
$ protoc --dart_out=. test.proto
pub global
$ pub global activate protoc_plugin
And then add .pub-cache/bin
in your home dir to your PATH
if you haven't already.
This will activate the latest published version of the plugin. If you wish to use a local working copy, use
$ pub global activate -s path <path/to/your/dart-protoc-plugin>
The protocol buffer compiler accepts options for each plugin. For the
Dart plugin, these options are passed together with the --dart_out
option. The individial options are separated using comma, and the
final output directive is separated from the options using colon. Pass
options <option 1>
and <option 2>
like this:
--dart_out="<option 1>,<option 2>:."
The protocol buffer compiler produces several files for each .proto
file
it compiles. In some cases this is not exactly what is needed, e.g one
would like to create new libraries which exposes the objects in these
libraries or create new librares combining object definitions from
several .proto
libraries into one.
The best way to aproach this is to create the new libraries needed and re-export the relevant protocol buffer classes.
Say we have the file m1.proto
with the following content
message M1 {
optional string a;
}
and m2.proto
containing
message M2 {
optional string b;
}
Compiling these to Dart will produce two libraries in m1.pb.dart
and
m2.pb.dart
. The following code shows a library M which combines
these two protocol buffer libraries, exposes the classes M1
and M2
and
adds som additional methods.
library M;
import "m1.pb.dart";
import "m2.pb.dart";
export "m1.pb.dart" show M1;
export "m2.pb.dart" show M2;
M1 createM1() => new M1();
M2 createM2() => new M2();
Here are some ways to get protoc:
apt-get install protobuf-compiler
brew install protobuf
If the version installed this way doesn't work, an alternative is to compile protoc from source.
Remember to run the tests. That is as easy as make run-tests
.
pbgrpc.dart
files now import package:grpc/grpc.dart
with a prefix.api_benchmark
.
This simplifies the dev_dependencies of this package.createEmptyInstance
that is used to support the new toBuilder()
semantics of protobuf 0.11.0.
The generated code requires at least protobuf 0.11.0.Breaking change: Handle identifiers starting with a leading underscore. This covers message names, enum names, enum value identifiers and file names.
Before, these would appear in the generated Dart code as private identifiers. Now the underscore is moved to the end.
Field names and extension field names already had all underscores removed, and these are not affected by this change.
If there is a conflicting name with a trailing underscore defined later in the same scope, a disambiguation will happen that can potentially lead to existing identifiers getting a new name in the generated Dart.
For example:
message _Foo {}
message Foo_ {}
_Foo
will get the name Foo_
and Foo_
will now end up being called Foo__
.
Breaking change: Support for map fields Generated files require package:protobuf version 0.10.5 or newer. Protobuf map fields such as:
message Foo { map<int32, string> map_field = 1; } are now no longer represented as List<Foo_MapFieldEntry> but as Map<int, String>.
All code handling these fields needs to be updated.
Accidentally published as 11.0.0 instead of 0.11.0. Continuing from here.
dart:async
with a prefix to prevent name
collisions.BuilderInfo.qualifiedMessageName
.
Requires package:protobuf version 0.10.4 or newer.as
check of enum valueOf
by using correctly typed Map
of
values.
Generated files must require package:protobuf version 0.10.3 or newer.BuilderInfo.messageName
will now be the fully qualified name for generated messages.copyWith()
to message classes and update getDefault()
to use freeze()
.
Requires package:protobuf version 0.10.0 or newer.pub global
usage. Protoc plugin can now be installed by running pub global activate protoc_plugin
.Function
will soon be a reserved keyword, so don't generate classes with that name.dart_name
option.This release requires 0.5.0 of the protobuf library.
This release changes how getters work for message fields, to detect a common mistake.
Previously, the protobuf API didn't report any error for an incorrect usage of setters. For example, if field "foo" is a message field of type Foo, this code would silently have no effect:
var msg = new SomeMessage(); msg.foo.bar = 123; This is because "msg.foo" would call "new Foo()" and return it without saving it.
The example can be fixed like this:
var msg = new SomeMessage(); msg.foo = new Foo(); msg.foo.bar = 123; Or equivalently:
var msg = new SomeMessage() ..foo = (new Foo()..bar = 123); Starting in 0.4.0, the default value of "msg.foo" is an immutable instance of Foo. You can read the default value of a field the same as before, but writes will throw UnsupportedError.
For now, new mixins can only be added using a patch:
This new API is subject to change without notice, but if you want to try it out anyway, see the unit test.
The 0.3.6 version of the dart-protobuf library added two new functions, so this release changes the protobuf compiler to avoid using them.
Protobuf changes for smaller dart2js code, Int64 fixes
This change is paired with https://chromiumcodereview.appspot.com/814213003
Reduces code size for one app by 0.9%
Parameterize uri resolution and parsing of options, use package:path.
This helps make the compiler more configurable to embed it in other systems (like pub transformers)
Update the version number
You can install the package from the command line:
$ pub global activate protoc_plugin
The package has the following executables:
$ protoc-gen-dart
Add this to your package's pubspec.yaml file:
dependencies:
protoc_plugin: ^15.0.3
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:protoc_plugin/base_type.dart';
import 'package:protoc_plugin/bazel.dart';
import 'package:protoc_plugin/client_generator.dart';
import 'package:protoc_plugin/code_generator.dart';
import 'package:protoc_plugin/const_generator.dart';
import 'package:protoc_plugin/enum_generator.dart';
import 'package:protoc_plugin/extension_generator.dart';
import 'package:protoc_plugin/file_generator.dart';
import 'package:protoc_plugin/grpc_generator.dart';
import 'package:protoc_plugin/indenting_writer.dart';
import 'package:protoc_plugin/linker.dart';
import 'package:protoc_plugin/message_generator.dart';
import 'package:protoc_plugin/names.dart';
import 'package:protoc_plugin/options.dart';
import 'package:protoc_plugin/output_config.dart';
import 'package:protoc_plugin/protobuf_field.dart';
import 'package:protoc_plugin/protoc.dart';
import 'package:protoc_plugin/service_generator.dart';
import 'package:protoc_plugin/testing/mixins.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
15.0.3 | Feb 4, 2019 |
|
|
15.0.2 | Jan 24, 2019 |
|
|
15.0.1 | Jan 22, 2019 |
|
|
15.0.0 | Jan 15, 2019 |
|
|
14.0.1 | Jan 10, 2019 |
|
|
14.0.0 | Jan 8, 2019 |
|
|
13.0.1 | Jan 8, 2019 |
|
|
13.0.0 | Jan 2, 2019 |
|
|
12.0.0 | Dec 6, 2018 |
|
|
11.0.0 | Dec 4, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
80
|
Health:
Code health derived from static analysis.
[more]
|
98
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
74
|
Overall:
Weighted score of the above.
[more]
|
84
|
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, other
Platform components identified in package:
io
.
Fix lib/message_generator.dart
. (-1.49 points)
Analysis of lib/message_generator.dart
reported 3 hints:
line 67 col 26: Use isEmpty instead of length
line 303 col 13: Use isNotEmpty instead of length
line 378 col 9: Use isNotEmpty instead of length
Fix lib/options.dart
. (-0.50 points)
Analysis of lib/options.dart
reported 1 hint:
line 41 col 7: Use isEmpty instead of length
The package description is too short. (-16 points)
Add more detail to the description
field of pubspec.yaml
. Use 60 to 180 characters to describe the package, what it does, and its target use case.
Maintain an example. (-10 points)
Create a short demo in the example/
directory to show how to use this package.
Common filename patterns include main.dart
, example.dart
, and protoc_plugin.dart
. Packages with multiple examples should provide example/README.md
.
For more information see the pub package layout conventions.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
dart_style | ^1.0.6 | 1.2.3 | |
fixnum | ^0.10.5 | 0.10.9 | |
path | ^1.0.0 | 1.6.2 | |
protobuf | ^0.13.1 | 0.13.3 | |
Transitive dependencies | |||
analyzer | 0.35.1 | ||
args | 1.5.1 | ||
async | 2.0.8 | ||
charcode | 1.1.2 | ||
collection | 1.14.11 | ||
convert | 2.1.1 | ||
crypto | 2.0.6 | ||
csslib | 0.14.6 | ||
front_end | 0.1.11 | ||
glob | 1.1.7 | ||
html | 0.13.3+3 | ||
kernel | 0.3.11 | ||
logging | 0.11.3+2 | ||
meta | 1.1.7 | ||
package_config | 1.0.5 | ||
plugin | 0.2.0+3 | ||
pub_semver | 1.4.2 | ||
source_span | 1.5.4 | ||
string_scanner | 1.0.4 | ||
term_glyph | 1.1.0 | ||
typed_data | 1.1.6 | ||
utf | 0.9.0+5 | ||
watcher | 0.9.7+10 | ||
yaml | 2.1.15 | ||
Dev dependencies | |||
test | ^1.3.0 |