1111Generate kotlin data classes from ` protobuf ` files that supports _ Kotlin Native_ that can be serialized and
1212deserialized to protobuf using [ kotlinx.serialization] ( https://github.com/Kotlin/kotlinx.serialization ) .
1313
14- > [ !NOTE]
15- > Setup instructions and detailed documentation can be found
16- > at [ Setup and Documentation] ( https://dogacel.github.io/kotlinx-protobuf-gen ) .
17-
18- ![ Demonstrate Code] ( ./docs/src/doc/docs/assets/demonstrate_code.png )
14+ ![ Demonstrate Code] ( ./assets/demonstrate_code.png )
1915
2016## Features
2117
22- - [x] Supports ` proto2 ` and ` proto3 ` .
18+ - [x] Supports ` proto2 ` , ` proto3 ` and ` editions ` up to ` 2023 ` .
2319- [x] Generates ` kotlinx.serialization ` annotations for proto field numbers and serialization format.
2420- [x] Generates Kotlin code for primitive fields such as ` int32 ` , ` string ` , ` bytes ` .
2521- [x] Generates Kotlin code for ` message ` , ` enum ` , ` repeated ` , ` map ` , ` oneof ` types.
2622- [x] Generates Kotlin code that includes imports and uses nested types.
2723- [x] Supports common well-known types such as ` Timestamp ` , ` StringValue ` and serializes them to kotlin
2824 primitives or standards.
25+ - [x] Support Well-Known Types deserialization to Well-Known Kotlin types such as ` google.protobuf.Duration `
26+ to ` kotlin.time.Duration ` and ` google.protobuf.Timestamp ` to ` kotlinx.datetime.Instant ` .
27+ - An option is added to code generator to enable this feature.
28+ - More WKT additions will be added.
29+
30+ ## Setup
31+
32+ ### 1. Dependencies,
33+
34+ ``` kotlin
35+ plugins {
36+ kotlin(" jvm" ) version " 1.9.0"
37+ id(" org.jetbrains.kotlin.plugin.serialization" ) version " 1.9.0"
38+ id(" com.google.protobuf" ) version " 0.9.4"
39+ }
40+
41+ var protobufVersion = " 3.23.4"
42+
43+ dependencies {
44+ // Runtime libraries include WKT conversion utilities and other libraries that are required such
45+ // as kotlinx.datetime, kotlinx.coroutines, kotlinx.serialization, etc.
46+ implementation(" io.github.dogacel:kotlinx-protobuf-gen:0.0.1" )
47+ }
48+ ```
49+
50+ #### 2. Code generation,
51+
52+ ``` kotlin
53+ protobuf {
54+ protoc {
55+ artifact = " com.google.protobuf:protoc:$protobufVersion "
56+ }
57+
58+ plugins {
59+ id(" kotlinx-protobuf-gen" ) {
60+ artifact = " io.github.dogacel:kotlinx-protobuf-gen:0.0.1:jvm8@jar"
61+ }
62+ }
63+
64+ // Enable Kotlin generation
65+ generateProtoTasks {
66+ all().forEach {
67+ it.builtins {
68+ remove(" java" ) // Optionally you can keep the java generated files.
69+ }
70+ it.plugins {
71+ id(" kotlinx-protobuf-gen" ) {
72+ option(" package_prefix=custom.pkg" ) // Set a custom package prefix
73+ }
74+ }
75+ }
76+ }
77+ }
78+ ```
79+
80+ #### 3. Writing proto files,
81+
82+ Add your proto files to a known proto file path such as ` src/main/proto ` .
83+
84+ ``` protobuf
85+ syntax = "proto3";
86+
87+ package demo;
88+
89+ message Task {
90+ int32 id = 1;
91+ optional string description = 2;
92+ Status status = 3;
93+
94+ enum Status {
95+ WIP = 0;
96+ DONE = 1;
97+ }
98+ }
99+ ```
100+
101+ The following class will be generated and added to your classpath.
102+
103+ ``` kotlin
104+ @Serializable
105+ public data class Task (
106+ @ProtoNumber(number = 1 )
107+ public val id : Int = 0 ,
108+ @ProtoNumber(number = 2 )
109+ public val description : String? = null ,
110+ @ProtoNumber(number = 3 )
111+ public val status : Status = testgen.demo.Task .Status .WIP ,
112+ ) {
113+ @Serializable
114+ public enum class Status {
115+ @ProtoNumber(number = 0 )
116+ WIP ,
117+
118+ @ProtoNumber(number = 1 )
119+ DONE ,
120+ }
121+ }
122+ ```
123+
124+ ## Customizations
125+
126+ To customize the code generated, you can pass command line arguments or gradle options. For example,
127+
128+ ``` kotlin
129+ protobuf {
130+ protoc {
131+ artifact = " com.google.protobuf:protoc:$protobufVersion "
132+ }
133+
134+ plugins {
135+ id(" kotlinx-protobuf-gen" ) {
136+ artifact = " io.github.dogacel:kotlinx-protobuf-gen:0.0.1:jvm8@jar"
137+ }
138+ }
139+
140+ // Enable Kotlin generation
141+ generateProtoTasks {
142+ all().forEach {
143+ it.builtins {
144+ remove(" java" ) // Optionally you can keep the java generated files.
145+ }
146+ it.plugins {
147+ id(" kotlinx-protobuf-gen" ) {
148+ option(" package_prefix=custom.pkg" ) // Set a custom package prefix
149+ }
150+ }
151+ }
152+ }
153+ }
154+ ```
155+
156+ ### Available Options
157+
158+ | Option | Description | Default |
159+ | --------------------| ------------------------------------------------------------------------------------------------------------| ---------|
160+ | ` package_prefix ` | Prefix for the generated package names. Appended to the start of each class | ` "" ` |
161+ | ` useCamelCase ` | Whether to use the original ` snake_case ` for proto fields or ` camelCase ` . Can be either ` true ` or ` false ` . | ` true ` |
162+ | ` generateServices ` | Whether to generate abstract gRPC stubs or not. Can be either ` true ` or ` false ` . | ` true ` |
163+
29164
30165## Roadmap
31166
@@ -34,15 +169,8 @@ is a list of features we are working on that are required to release first stabl
34169
35170- [ ] Proper serialization / deserialization of all types. Check "Known Issues" section below to see all major
36171 issues.
37- - [ ] Run full conformance tests on the generated code.]
172+ - [ ] Run full conformance tests on the generated code.
38173- [ ] Support Protobuf JSON format.
39-
40- And here is a list of features that we are planning to work on after the first stable release.
41-
42- - [x] Support Well-Known Types deserialization to Well-Known Kotlin types such as ` google.protobuf.Duration `
43- to ` kotlin.time.Duration ` and ` google.protobuf.Timestamp ` to ` kotlinx.datetime.Instant ` .
44- - An option is added to code generator to enable this feature.
45- - More WKT additions will be added.
46174- [ ] Support various options such as ` deprecated ` , ` default ` , ` json_name ` .
47175- [ ] Auto-generated comments from ` .proto ` files in the generated code.
48176- [x] gRPC support.
@@ -51,6 +179,8 @@ And here is a list of features that we are planning to work on after the first s
51179- [ ] Plugin and more option support for customizing the generated code. (Such as non-enforced nullability to
52180 gimmick proto2 required fields based on certain rules)
53181
182+ For the full list, check [ issues] ( ./issues )
183+
54184## Known Issues
55185
56186An issue to track ` kotlinx.serialization ` : https://github.com/Kotlin/kotlinx.serialization/issues/2401
0 commit comments