OLD | NEW |
1 Protoc compiler Dart plugin | 1 Protoc compiler Dart plugin |
2 =========================== | 2 =========================== |
3 | 3 |
4 This application provides a plugin for protoc compiler which | 4 This application provides a plugin for protoc compiler which |
5 generates pure Dart library to deal with protobufs. | 5 generates pure Dart library to deal with protobufs. |
6 | 6 |
7 Please, do not forget that generated libraries depend on runtime | 7 Please, do not forget that generated libraries depend on runtime |
8 support library which can be found [here](https://github.com/dart-lang/dart-prot
obuf). | 8 support library which can be found [here](https://github.com/dart-lang/dart-prot
obuf). |
9 | 9 |
10 How to build and use | 10 How to build and use |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 --dart_out="field_name=MyMessage.has_field|HasFld:." | 58 --dart_out="field_name=MyMessage.has_field|HasFld:." |
59 | 59 |
60 Will generate the following message field accessors: | 60 Will generate the following message field accessors: |
61 | 61 |
62 String get hasFld => getField(1); | 62 String get hasFld => getField(1); |
63 void set hasFld(String v) { setField(1, v); } | 63 void set hasFld(String v) { setField(1, v); } |
64 bool hasHasFld() => hasField(1); | 64 bool hasHasFld() => hasField(1); |
65 void clearHasFld() => clearField(1); | 65 void clearHasFld() => clearField(1); |
66 | 66 |
| 67 Using protocol buffer libraries to build new libraries |
| 68 ------------------------------------------------------ |
| 69 |
| 70 The protocol buffer compiler produces one library for each `.proto` file |
| 71 it compiles. In some cases this is not exactly what is needed, e.g one |
| 72 would like to create new libraries which exposes the objects in these |
| 73 libraries or create new librares combining object definitions from |
| 74 several `.proto` libraries into one. |
| 75 |
| 76 The best way to aproach this is to create the new libraries needed and |
| 77 re-export the relevant protocol buffer classes. |
| 78 |
| 79 Say we have the file `m1.proto` with the following content |
| 80 |
| 81 message M1 { |
| 82 optional string a; |
| 83 } |
| 84 |
| 85 and `m2.proto` containing |
| 86 |
| 87 message M2 { |
| 88 optional string b; |
| 89 } |
| 90 |
| 91 Compiling these to Dart will produce two libraries in `m1.pb.dart` and |
| 92 `m2.pb.dart`. The following code shows a library M which combines |
| 93 these two protocol buffer libraries, exposes the classes `M1` and `M2` and |
| 94 adds som additional methods. |
| 95 |
| 96 library M; |
| 97 |
| 98 import "m1.pb.dart"; |
| 99 import "m2.pb.dart"; |
| 100 |
| 101 export "m1.pb.dart" show M1; |
| 102 export "m2.pb.dart" show M2; |
| 103 |
| 104 M1 createM1() => new M1(); |
| 105 M2 createM2() => new M2(); |
| 106 |
67 Hacking | 107 Hacking |
68 ------- | 108 ------- |
69 | 109 |
70 Remember to run the tests. That is as easy as `make run-tests`. | 110 Remember to run the tests. That is as easy as `make run-tests`. |
71 | 111 |
72 The default way of running the Dart protoc plugin is through the | 112 The default way of running the Dart protoc plugin is through the |
73 generated `out/protoc-gen-dart` script. However when run this way the | 113 generated `out/protoc-gen-dart` script. However when run this way the |
74 Dart code is assembled into one large Dart file using dart2dart. To | 114 Dart code is assembled into one large Dart file using dart2dart. To |
75 run with the actual source in the repository create an executable | 115 run with the actual source in the repository create an executable |
76 script called `protoc-gen-dart` with the following content: | 116 script called `protoc-gen-dart` with the following content: |
(...skipping 14 matching lines...) Expand all Loading... |
91 $ protoc --plugin=protoc-gen-dart=./plugin --dart_out=. test.proto | 131 $ protoc --plugin=protoc-gen-dart=./plugin --dart_out=. test.proto |
92 | 132 |
93 Useful references | 133 Useful references |
94 ----------------- | 134 ----------------- |
95 | 135 |
96 * [Main Dart site](http://www.dartlang.org) | 136 * [Main Dart site](http://www.dartlang.org) |
97 * [Main protobuf site](https://code.google.com/p/protobuf) | 137 * [Main protobuf site](https://code.google.com/p/protobuf) |
98 * [Protobuf runtime support project](https://github.com/dart-lang/dart-protobuf) | 138 * [Protobuf runtime support project](https://github.com/dart-lang/dart-protobuf) |
99 * [DartEditor download](http://www.dartlang.org) | 139 * [DartEditor download](http://www.dartlang.org) |
100 * [Pub documentation](http://pub.dartlang.org/doc) | 140 * [Pub documentation](http://pub.dartlang.org/doc) |
OLD | NEW |