@@ -8,9 +8,42 @@ Protocol Buffer compiler and runtime for Kotlin.
8
8
9
9
Supports only version 3 of the Protocol Buffers language.
10
10
11
- #### Features
11
+ * [ Overview] ( #overview )
12
+ + [ Features] ( #features )
13
+ + [ Not yet implemented] ( #not-yet-implemented )
14
+ + [ Compatibility] ( #compatibility )
15
+ * [ Usage] ( #usage )
16
+ + [ Gradle] ( #gradle )
17
+ + [ Generated Code] ( #generated-code )
18
+ * [ Runtime Notes] ( #runtime-notes )
19
+ + [ Package] ( #package )
20
+ + [ Message] ( #message )
21
+ + [ Enums] ( #enums )
22
+ - [ Representation] ( #representation )
23
+ - [ Naming] ( #naming )
24
+ + [ Other Notes] ( #other-notes )
25
+ * [ Extensions] ( #extensions )
26
+ + [ Wrapper Types] ( #wrapper-types )
27
+ + [ Non-null fields] ( #non-null-fields )
28
+ + [ Interface implementation] ( #interface-implementation )
29
+ - [ Messages] ( #messages )
30
+ - [ Oneof Fields] ( #oneof-fields )
31
+ + [ BytesSlice] ( #bytesslice )
32
+ * [ gRPC code generation] ( #grpc-code-generation )
33
+ + [ Generated gRPC code] ( #generated-grpc-code )
34
+ + [ Integrating with gRPC's Java API] ( #integrating-with-grpcs-java-api )
35
+ + [ Integrating with gRPC's Kotlin API] ( #integrating-with-grpcs-kotlin-api )
36
+ * [ IntelliJ integration] ( #intellij-integration )
37
+ * [ Command line code generation] ( #command-line-code-generation )
38
+ * [ Contribution] ( #contribution )
39
+ * [ Acknowledgements] ( #acknowledgements )
40
+ + [ Authors] ( #authors )
41
+
42
+ ## Overview
43
+
44
+ ### Features
12
45
- Idiomatic and concise [ Kotlin builder DSL] ( #generated-code )
13
- - Protokt-specific options: [ non-null types] ( #nonnull -fields ) ,
46
+ - Protokt-specific options: [ non-null types] ( #non-null -fields ) ,
14
47
[ wrapper types] ( #wrapper-types ) ,
15
48
[ interface implementation] ( #interface-implementation ) ,
16
49
and more
@@ -22,25 +55,25 @@ CodedOutputStream for best performance
22
55
for use with [ grpc-java] ( #integrating-with-grpcs-java-api ) or
23
56
[ grpc-kotlin] ( #integrating-with-grpcs-kotlin-api ) (see examples in [ examples] ( examples ) )
24
57
25
- #### Not yet implemented
58
+ ### Not yet implemented
26
59
27
60
- Reflection support (in progress)
28
61
- Kotlin/Native support
29
62
- Kotlin/JS support
30
63
- Protobuf JSON support
31
64
32
- See examples in [ testing] ( testing ) .
33
-
34
- #### Compatibility
65
+ ### Compatibility
35
66
36
67
The Gradle plugin requires Java 8+ and Gradle 5.6+. It runs on recent versions of
37
68
MacOS, Linux, and Windows.
38
69
39
70
The runtime and generated code are compatible with Kotlin 1.4+, Java 8+, and Android 4.4+.
40
71
41
- ### Usage
72
+ ## Usage
42
73
43
- #### Gradle
74
+ See examples in [ testing] ( testing ) .
75
+
76
+ ### Gradle
44
77
45
78
``` groovy
46
79
plugins {
99
132
compileJava.enabled = false
100
133
```
101
134
102
- #### Generated Code
135
+ ### Generated Code
103
136
104
137
Generated code is placed in ` <buildDir>/generated-sources/<sourceSet.name>/protokt ` .
105
138
106
139
A simple example:
107
140
108
- ``` proto
141
+ ``` protobuf
109
142
syntax = "proto3";
110
143
111
144
package toasttab.protokt.sample;
@@ -249,9 +282,9 @@ any escaping mutability of the provided collection. The Java protobuf
249
282
implementation takes a similar approach; it only exposes mutation methods on the
250
283
builder and not assignment. Mutating the builder does a similar copy operation.
251
284
252
- ### Runtime Notes
285
+ ## Runtime Notes
253
286
254
- #### Package
287
+ ### Package
255
288
256
289
By default, the Kotlin package of a generated file is the same as the protobuf
257
290
package. Second in precedence is the declared ` java_package ` option, which can
@@ -270,7 +303,7 @@ use to prevent duplicate class issues.
270
303
271
304
Highest precedence is given to the ` (protokt.file).kotlin_package ` option:
272
305
273
- ``` proto
306
+ ``` protobuf
274
307
syntax = "proto3";
275
308
276
309
import "protokt/protokt.proto";
@@ -282,7 +315,7 @@ option (protokt.file).kotlin_package = "com.example";
282
315
...
283
316
```
284
317
285
- #### Message
318
+ ### Message
286
319
287
320
Each protokt message implements the ` KtMessage ` interface. ` KtMessage ` defines
288
321
the ` serialize() ` method and its overloads which can serialize to a byte array,
@@ -293,9 +326,9 @@ Each protokt message has a companion object `Deserializer` that implements the
293
326
overloads to construct an instance of the message from a byte array, a Java
294
327
InputStream, or others.
295
328
296
- #### Enums
329
+ ### Enums
297
330
298
- ##### Representation
331
+ #### Representation
299
332
300
333
Protokt represents enum fields as sealed classes with an integer value and name.
301
334
Protobuf enums cannot be represented as Kotlin enum classes since Kotlin enum
@@ -329,14 +362,14 @@ sealed class PhoneType(
329
362
}
330
363
```
331
364
332
- ##### Naming
365
+ #### Naming
333
366
334
367
To keep enums ergonomic while promoting protobuf best practices, enums that have
335
368
all values
336
369
[ prefixed with the enum type name] ( https://developers.google.com/protocol-buffers/docs/style#enums )
337
370
will have that prefix stripped in their Kotlin representations.
338
371
339
- #### Other Notes
372
+ ### Other Notes
340
373
341
374
- ` optimize_for ` is ignored.
342
375
- ` repeated ` fields are represented as Lists.
@@ -347,13 +380,13 @@ single property.
347
380
akin to ` protobuf-java ` 's ` ByteString ` .
348
381
- Protokt implements proto3's ` optional ` .
349
382
350
- ### Extensions
383
+ ## Extensions
351
384
352
385
See examples of each option in the [ options] ( testing/options/src/main/proto )
353
386
project. All protokt-specific options require importing ` protokt/protokt.proto `
354
387
in the protocol file.
355
388
356
- #### Wrapper Types
389
+ ### Wrapper Types
357
390
358
391
Sometimes a field on a protobuf message corresponds to a concrete nonprimitive
359
392
type. In standard protobuf the user would be responsible for this extra
@@ -363,7 +396,7 @@ types. Some standard types are implemented in [extensions](extensions).
363
396
364
397
Wrap a field by invoking the ` (protokt.property).wrap ` option:
365
398
366
- ``` proto
399
+ ``` protobuf
367
400
message WrapperMessage {
368
401
google.protobuf.Timestamp instant = 1 [
369
402
(protokt.property).wrap = "java.time.Instant"
@@ -510,7 +543,7 @@ determine what behavior should be in these cases. To represent nullable
510
543
primitive wrappers use well-known types or Proto3's ` optional ` . For example for
511
544
a nullable UUID:
512
545
513
- ``` proto
546
+ ``` protobuf
514
547
google.protobuf.BytesValue uuid = 1 [
515
548
(protokt.property).wrap = "java.util.UUID"
516
549
];
@@ -523,14 +556,14 @@ optional bytes optional_uuid = 2 [
523
556
524
557
Wrapper types can be repeated:
525
558
526
- ``` proto
559
+ ``` protobuf
527
560
repeated bytes uuid = 1 [
528
561
(protokt.property).wrap = "java.util.UUID"
529
562
];
530
563
```
531
564
532
565
And they can also be used for map keys and values:
533
- ``` proto
566
+ ``` protobuf
534
567
map<string, protokt.ext.InetSocketAddress> map_string_socket_address = 1 [
535
568
(protokt.property).key_wrap = "StringBox",
536
569
(protokt.property).value_wrap = "java.net.InetSocketAddress"
@@ -547,12 +580,12 @@ _N.b. Well-known type nullability is implemented with
547
580
for each message defined in
548
581
[ wrappers.proto] ( https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto ) ._
549
582
550
- #### Nonnull fields
551
- If a message has no meaning whatsoever when a particular nonscalar field is
583
+ ### Non-null fields
584
+ If a message has no meaning whatsoever when a particular non-scalar field is
552
585
missing, you can emulate proto2's ` required ` key word by using the
553
586
` (protokt.property).non_null ` option:
554
587
555
- ``` proto
588
+ ``` protobuf
556
589
message Sample {}
557
590
558
591
message NonNullSampleMessage {
@@ -567,7 +600,7 @@ without using Kotlin's `!!`.
567
600
568
601
Oneof fields can also be declared non-null:
569
602
570
- ``` proto
603
+ ``` protobuf
571
604
message NonNullSampleMessage {
572
605
oneof non_null_oneof {
573
606
option (protokt.oneof).non_null = true;
@@ -580,9 +613,9 @@ message NonNullSampleMessage {
580
613
Note that deserialization of a message with a non-nullable field will fail if the
581
614
message being decoded does not contain an instance of the required field.
582
615
583
- #### Interface implementation
616
+ ### Interface implementation
584
617
585
- ##### Messages
618
+ #### Messages
586
619
587
620
To avoid the need to create domain-specific objects from protobuf messages you
588
621
can declare that a protobuf message implements a custom interface with
@@ -596,7 +629,7 @@ interface Model {
596
629
}
597
630
```
598
631
599
- ``` proto
632
+ ``` protobuf
600
633
package com.protokt.sample;
601
634
602
635
message ImplementsSampleMessage {
@@ -622,7 +655,7 @@ Messages can also implement interfaces by delegation to one of their fields;
622
655
in this case the delegated interface need not live in a separate project, as
623
656
protokt requires no inspection of it:
624
657
625
- ``` proto
658
+ ``` protobuf
626
659
message ImplementsWithDelegate {
627
660
option (protokt.class).implements = "Model2 by modelTwo";
628
661
@@ -634,7 +667,7 @@ message ImplementsWithDelegate {
634
667
635
668
Note that the ` by ` clause references the field by its lower camel case name.
636
669
637
- ##### Oneof Fields
670
+ #### Oneof Fields
638
671
639
672
Oneof fields can declare that they implement an interface with the
640
673
` (protokt.oneof).implements ` option. Each possible field type of the oneof must
@@ -761,15 +794,15 @@ fun printVersion(config: MyObjectWithConfig.Config) {
761
794
}
762
795
```
763
796
764
- #### BytesSlice
797
+ ### BytesSlice
765
798
766
799
When reading messages that contain other serialized messages as ` bytes ` fields,
767
800
protokt can keep a reference to the originating byte array to prevent a large
768
801
copy operation on deserialization. This can be desirable when the wrapping
769
802
message is short-lived or a thin metadata shim and doesn't include much memory
770
803
overhead:
771
804
772
- ``` proto
805
+ ``` protobuf
773
806
message SliceModel {
774
807
int64 version = 1;
775
808
@@ -779,7 +812,7 @@ message SliceModel {
779
812
}
780
813
```
781
814
782
- ### gRPC code generation
815
+ ## gRPC code generation
783
816
784
817
Protokt will generate code for gRPC method and service descriptors when the
785
818
` generateGrpc ` option is enabled:
@@ -801,11 +834,11 @@ protokt {
801
834
Protokt does not yet generate full client and server stubs. It does generate the
802
835
components necessary to integrate with gRPC's Java and Kotlin APIs.
803
836
804
- #### Generated gRPC code
837
+ ### Generated gRPC code
805
838
806
839
Consider gRPC's canonical Health service:
807
840
808
- ``` proto
841
+ ``` protobuf
809
842
syntax = "proto3";
810
843
811
844
package grpc.health.v1;
@@ -859,7 +892,7 @@ constructor-injected method implementations is included in the examples below.
859
892
These two implementation styles can emulate each other, so the choice of which
860
893
to use is perhaps a matter of taste.
861
894
862
- #### Integrating with gRPC's Java API
895
+ ### Integrating with gRPC's Java API
863
896
864
897
A gRPC service using grpc-java (and therefore using StreamObservers for
865
898
asynchronous communication):
@@ -904,7 +937,7 @@ fun checkHealth(): HealthCheckResponse =
904
937
)
905
938
```
906
939
907
- #### Integrating with gRPC's Kotlin API
940
+ ### Integrating with gRPC's Kotlin API
908
941
909
942
To use the coroutine-based implementations provided by grpc-kotlin:
910
943
@@ -950,7 +983,7 @@ suspend fun checkHealth(): HealthCheckResponse =
950
983
)
951
984
```
952
985
953
- ### IntelliJ integration
986
+ ## IntelliJ integration
954
987
955
988
If IntelliJ doesn't automatically detect the generated files as source files,
956
989
you may be missing the ` idea ` plugin. Apply the ` idea ` plugin to your Gradle
@@ -962,7 +995,7 @@ plugins {
962
995
}
963
996
```
964
997
965
- ### Command line code generation
998
+ ## Command line code generation
966
999
967
1000
``` sh
968
1001
protokt % ./gradlew assemble
@@ -987,7 +1020,11 @@ protokt % protoc \
987
1020
test.proto
988
1021
```
989
1022
990
- ### Contribution
1023
+ ## Contribution
1024
+
1025
+ Community contributions are welcome. See the
1026
+ [ contribution guidelines] ( CONTRIBUTING.md ) and the project
1027
+ [ code of conduct] ( CODE-OF-CONDUCT.md ) .
991
1028
992
1029
To enable rapid development of the code generator, the protobuf conformance
993
1030
tests have been compiled and included in the ` testing ` project. They run on Mac
@@ -997,9 +1034,9 @@ When integration testing the Gradle plugin, note that after changing the plugin
997
1034
and republishing it to the integration repository, ` ./gradlew clean ` is needed
998
1035
to trigger regeneration of the protobuf files with the fresh plugin.
999
1036
1000
- ### Acknowledgements
1037
+ ## Acknowledgements
1001
1038
1002
- #### Authors
1039
+ ### Authors
1003
1040
1004
1041
[ Ben Gordon
] ( mailto:[email protected] ) ,
1005
1042
[ Andrew Parmet
] ( mailto:[email protected] ) ,
0 commit comments