protoc_plugin 19.0.1

  • Readme
  • Changelog
  • Installing
  • 89

Dart plugin for the protoc compiler #

pub package

This repository provides a plugin for the protoc compiler. It generates Dart files for working with data in protocol buffers format.

Requirements #

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.

How to build and use #

Note: currently the workflow is POSIX-oriented.

To build standalone protoc plugin:

  • run pub install to install all dependencies
  • Now you can use the plugin either by adding the bin 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

Optionally using 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>

Options to control the generated Dart code #

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>:."

Generating Code Info #

The plugin includes the generate_kythe_info option, which, if passed at run time, will make the plugin generate metadata files alongside the .dart files generated for the proto messages and their enums. Pass this along with the other dart_out options:

--dart_out="generate_kythe_info,<other options>:."

Using protocol buffer libraries to build new libraries #

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();

Hacking #

Here are some ways to get protoc:

  • Linux: apt-get install protobuf-compiler
  • Mac homebrew: 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.

Useful references #

19.0.1 #

  • Fix: avoid naming collisions with Int64 and enum names beginning with digits after an initial underscore.

19.0.0+1 #

  • Updated protobuf dependency to '>=0.14.4 <2.0.0' to allow 1.0.0.

19.0.0 #

  • Breaking: Generates code that requires at least protobuf 0.14.4. If protoc_plugin is installed in your path with pub global activate you can upgrade with pub global activate protoc_plugin 19.0.0
    • GeneratedMessage classes now have methods ensureX for each message field X.
    • Add specialized getters for String, int, and bool with usual default values.
    • Annotate generated accessors with the tag number of the associated field.
  • Breaking: Use unmangled names for the string representation of enum values. Mangled names would lead to wrong proto3 json en- and decoding.
  • Annotate generated accessors with the tag number of the associated field.

18.0.3 #

  • Fix: Allow decoding tagnumbers of up to 29 bits. Would fail before with more than 28 bits.

18.0.2 #

  • Fix mangling of extension names, message type names, and enum names that are Dart keywords.

    Now you can have an extension called is and an enum called class.

18.0.1 #

  • Add a bin/protoc-gen-dart.bat script making it easier to compile on windows using a local checkout.

18.0.0 #

  • Breaking: Generates code that requires at least protobuf 0.14.0.

  • Generate the non-camel-case name of fields when it cannot be derived from the json name.

  • Breaking: Use the correct proto3 Json CamelCase names for the string representation of field names (also for extensions), instead of using the name of the dart identifier for that field.

    In most cases this name coincides with the name have emitted until now and require no change.

    Exceptions are:

    • Fields with a name that was disambiguated to not clash with other dart entities.
    • fields with an explicit json_name option.
    • groups have a different camel-casing scheme.

    For any field with an updated name, this might require changes to uses of GeneratedMessage.getTagNumber(String FieldName), and calls to name-related methods of GeneratedMessage._info.

    GeneratedMessage.toString() also uses the string representation. It will now print the json-name.

  • Well-known types (for now only google.protobuf.Any and google.protobuf.Timestamp) now uses a mixin to add special handling code instead of hardcoding it in the compiler.

17.0.5 #

  • Remove unnecessary cast from generated grpc stubs.

17.0.4 #

17.0.3 #

  • Fix: Copy oneof state when doing GeneratedMessage.copyWith().

17.0.2 #

  • Fix: Avoiding argument_type_not_assignable and return_of_invalid_type lint warnings.

17.0.1 #

  • Fix: Actually use prefixed imports from .pbserver, .pb.json, .pbgrpc files.

17.0.0 #

  • Breaking change: seal protobuf message classes by using an internal private constructor and changing the rest to factories.

16.0.7 #

  • Always prefix .pb.dart imports in .pbserver, .pb.json, .pbgrpc files.

16.0.6 #

  • Track the original order of proto fields and include it in metadata to ensure the correct fields are being referenced.

16.0.5 #

  • Fix generation of invalid Dart code for oneof enums by adding list of reserved enum names.

16.0.4 #

  • Generate '@Deprecated' annotations on fields that have been deprecated in the corresponding .proto file.

16.0.3 #

  • Sync Kythe metadata updates from internal repo:
  • Remove the extra 1 (field name) from generated field paths, so they refer to the whole field rather than the name.
  • Add missing proto message field metadata to the Dart proto generator (attached to setter/getter/has/clear methods). Fix a bug with indented space counting, which would cause all indented fields to use the incorrect count (off by the size of the indent)
  • Add metadata to the unnamed constructors in the generated dart proto files. Removed extra list copy constructor calls when the field path isn't being modified. The named constructors (e.g. fromJson) don't need this because, at the call site, the class name and the constructor name are two separate links.

16.0.2 #

  • Generated files now import 'dart:core' with a prefix
  • Add 'camel_case_types' to analysis_options and add it in the 'ignore_for_file' header of the generated files.

16.0.1 #

  • Add DateTime conversion methods to google.protobuf.Timestamp.

16.0.0 #

  • Add ability to generate Kythe metadata files via the generate_kythe_info option.
  • Breaking change: Remove the '$checkItem' function from generated message classes and use the new method 'pc' on 'BuilderInfo' to add repeated composite fields. Generated files require package:protobuf version 0.13.4 or newer.
  • Breaking change: The generated *.pbgrpc.dart files now import package:grpc/service_api.dart instead of package:grpc/grpc.dart. Generated code needs at least package:grpc 1.0.1.

15.0.3 #

  • Add test for frozen messages with unknown fields and update protobuf dependency to 0.13.1.

15.0.2 #

  • The generated pbgrpc.dart files now import package:grpc/grpc.dart with a prefix.

15.0.1 #

  • Add test for frozen messages with extension fields and update protobuf dependency to 0.13.0.

15.0.0 #

  • Breaking change: Changed BuilderInfo call for map fields to include the BuilderInfo object for map entries. Generated files require package:protobuf version 0.12.0 or newer.

14.0.1 #

  • Remove the benchmark from the protoc_package. It now lives in [] as api_benchmark. This simplifies the dev_dependencies of this package.

14.0.0 #

  • Breaking change: generated message classes now have a new instance method 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.

13.0.1 #

  • Add test for recursive merging and update protobuf dependency to 0.10.7.

13.0.0 #

  • Breaking change: Support for oneof Generated files require package:protobuf version 0.10.6 or newer.

12.0.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__.

11.0.0 #

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

0.10.5 #

  • Generated files now import dart:async with a prefix to prevent name collisions.

0.10.4 #

  • Change the fully qualified message name of generated messages to use BuilderInfo.qualifiedMessageName. Requires package:protobuf version 0.10.4 or newer.

0.10.3 #

  • Remove runtime as check of enum valueOf by using correctly typed Map of values. Generated files must require package:protobuf version 0.10.3 or newer.

0.10.2 #

  • Add link to source file in generated code.

0.10.1 #

  • Prefix generated Dart proto imports by proto file path instead of by package. Tighten up member name checks for generated enum classes.

0.10.0 #

  • Breaking change: Support for any messages. Generated files require package:protobuf version 0.10.1 or newer. BuilderInfo.messageName will now be the fully qualified name for generated messages.

0.9.0 #

  • Breaking change: Add copyWith() to message classes and update getDefault() to use freeze(). Requires package:protobuf version 0.10.0 or newer.

0.8.2 #

  • Generated code now imports 'package:protobuf/protobuf.dart' prefixed. This avoids name clashes between user defined message names and the protobuf library.

0.8.1 #

  • Adjust dependencies to actually be compatible with Dart 2.0 stable.

0.8.0+1 #

  • Dart SDK upper constraint raised to declare compatibility with Dart 2.0 stable.

0.8.0 #

  • Breaking change: Generated RpcClient stubs use the generic invoke method. Requires package:protobuf version 0.8.0 or newer.
  • Dart 2 fixes.

0.7.11 #

  • Dart 2 fix.

0.7.10 #

  • Small performance tweak for DDC.

0.7.9 #

  • Add fast getters for common types.
  • Only pass index instead of tag and index in generated code.
  • Fix uses-dynamic-as-bottom error in generated gRPC code.

0.7.8 #

  • Added enumValues to FieldInfo.

0.7.7 #

  • Avoid name clashes between import prefix and field names.
  • Avoid name clashes between generated enum and extension class names.
  • Updated gRPC client stub generation to match latest changes to dart-lang/grpc-dart.

0.7.6 #

  • Updated gRPC client stub generation to produce code matching latest changes to dart-lang/grpc-dart.

0.7.5 #

  • Use real generic syntax instead of comment-based.
  • Support 2.0.0 dev SDKs.

0.7.4 #

  • Added call options to gRPC client stubs.

0.7.3 #

gRPC support #

  • Added gRPC stub generation.
  • Updated descriptor.proto from google/protobuf v3.3.0.

0.7.2 #

  • Added

0.7.1 #

  • Enable executable for pub global usage. Protoc plugin can now be installed by running pub global activate protoc_plugin.
  • Ensure generated extension class names don't conflict with message class names.
  • Function will soon be a reserved keyword, so don't generate classes with that name.
  • Strong mode tweaks and lint fixes.

0.7.0 - Not released #

  • Change how to customize the Dart name of a field to using a dart_name option.
  • Implemented support for adding external mixins to generate Dart protos.

0.6.0+1 - Not released #

  • Fix missing import when an extension uses an enum in the same .proto file.

0.6.0 - Not released #

  • Move protobuf enums to a separate .pbenum.dart file.
  • Move server-side stubs to .pbserver.dart.

0.5.2 - Not released #

  • Generate separate .pbjson.dart files for constants.

0.5.1 #

Strong mode and Bazel #

  • Fixed all analyzer diagnostics in strong mode.
  • Added experimental support for Bazel.

0.5.0 #

Performance improvements #

This release requires 0.5.0 of the protobuf library.

  • significantly improved performance for getters, setters, and hazzers
  • Each enum type now has a $json constant that contains its metadata.

0.4.1 #

Fixed imports, $checkItem, $json #

  • Fixed all warnings, including in generated code.
  • Started generating $checkItem function for verifying the values of repeated fields
  • Fixed service stubs to work when a message is in a different package
  • Started generating JSON constants to get the original descriptor data for services

0.4.0 #

Getters for message fields changed #

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(); = 123; This is because "" would call "new Foo()" and return it without saving it.

The example can be fixed like this:

var msg = new SomeMessage(); = new Foo(); = 123; Or equivalently:

var msg = new SomeMessage() = (new Foo() = 123); Starting in 0.4.0, the default value of "" is an immutable instance of Foo. You can read the default value of a field the same as before, but writes will throw UnsupportedError.

0.3.11 #

  • Fixes issues with reserved names

0.3.10 #

  • Adds support for generating stubs from service definitions.

0.3.9 #

  • Modify dart_options support so that it supports alternate mixins.
  • Move the experimental map implementation to PbMapMixin

For now, new mixins can only be added using a patch:

  • add the new class to the protobuf library
  • add the class to the list in mixin.dart.

0.3.8 #

Added option for experimental map API #

  • Changed the Map API so that operator [] and operator []= support dotted keys such as "".

This new API is subject to change without notice, but if you want to try it out anyway, see the unit test.

0.3.7 - Unreleased #

Added option for experimental map API #

  • Added an option to have GeneratedMessage subclasses implement Map.

0.3.6 #

Added writeToJsonMap and mergeFromJsonMap to reservedNames #

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.

0.3.5 #

Protobuf changes for smaller dart2js code, Int64 fixes

This change is paired with

Reduces code size for one app by 0.9%

  1. Allow constants for the default value to avoid many trivial closures.
  2. Generate and use static M.create() and M.createRepeated() methods on message classes M to ensure there is a shared copy of these closures rather than one copy per use.
  3. Parse Int64 values rather than generate from 'int' to ensure no truncation errors in JavaScript.

0.3.4 #

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)

0.3.3 #

Update the version number

Use this package as an executable

1. Install it

You can install the package from the command line:

$ pub global activate protoc_plugin

2. Use it

The package has the following executables:

$ protoc-gen-dart

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  protoc_plugin: ^19.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

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/mixins.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';
import 'package:protoc_plugin/well_known_types.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 13, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Analysis suggestions

Package not compatible with runtime flutter-web on Web


  • package:protoc_plugin/bazel.dart that imports:
  • package:protoc_plugin/protoc.dart that imports:
  • package:dart_style/dart_style.dart that imports:
  • package:dart_style/src/exceptions.dart that imports:
  • package:analyzer/error/error.dart that imports:
  • package:analyzer/src/generated/source.dart that imports:
  • package:analyzer/src/generated/source_io.dart that imports:
  • package:analyzer/src/generated/java_io.dart that imports:
  • dart:io

Package not compatible with runtime js


  • package:protoc_plugin/bazel.dart that imports:
  • package:protoc_plugin/protoc.dart that imports:
  • package:dart_style/dart_style.dart that imports:
  • package:dart_style/src/exceptions.dart that imports:
  • package:analyzer/error/error.dart that imports:
  • package:analyzer/src/generated/source.dart that imports:
  • package:analyzer/src/generated/source_io.dart that imports:
  • package:analyzer/src/generated/java_io.dart that imports:
  • dart:io

Maintenance suggestions

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/

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.3.6
fixnum ^0.10.9 0.10.11
path ^1.0.0 1.7.0
protobuf >=0.14.4 <2.0.0 1.0.1
Transitive dependencies
_fe_analyzer_shared 5.0.0
analyzer 0.39.12
args 1.6.0
async 2.4.2
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
js 0.6.2
meta 1.2.2 1.3.0-nullsafety
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
pedantic 1.9.2
pub_semver 1.4.4
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
test ^1.3.0