-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
476dc8d
commit 7057355
Showing
2 changed files
with
100 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,95 @@ | ||
# protogen4k | ||
A small proto file generator from kotlin data classes. | ||
|
||
 | ||
 | ||
 | ||
 | ||
 | ||
|
||
A small and simple `.proto` file generator. Generates a protobuf schema from the given kotlin classes. | ||
|
||
### Usage | ||
|
||
```xml | ||
|
||
<dependency> | ||
<groupId>io.github.smyrgeorge</groupId> | ||
<artifactId>protogen4k</artifactId> | ||
<version>x.y.z</version> | ||
</dependency> | ||
``` | ||
|
||
or using gradle: | ||
|
||
```kotlin | ||
implementation("io.github.smyrgeorge:protogen4k:x.y.z") | ||
``` | ||
|
||
### Examples | ||
|
||
Let's see an example. | ||
|
||
```kotlin | ||
package io.github.smyrgeorge.datasuite.converter.protobuf.examples | ||
|
||
import io.github.smyrgeorge.datasuite.converter.protobuf.KotlinClassToProtobuf | ||
import io.github.smyrgeorge.datasuite.converter.protobuf.annotation.ProtoFile | ||
|
||
class Main | ||
|
||
@ProtoFile(name = "a_class.proto", schema = "schema1") | ||
data class AClass( | ||
val a: String, | ||
val b: List<AnEnum> | ||
) { | ||
enum class AnEnum { | ||
A, B | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
KotlinClassToProtobuf.generate( | ||
topicPrefix = "sample", | ||
workDirectory = "examples/src/main/proto", | ||
classes = listOf(AClass::class), | ||
protoPackage = "io.github.smyrgeorge.datasuite.proto" | ||
) | ||
} | ||
``` | ||
|
||
The above `kotlin` code will generate 2 `.proto` files. | ||
|
||
```protobuf | ||
// file: a_class.proto | ||
syntax = "proto3"; | ||
package io.github.smyrgeorge.datasuite.proto; | ||
option java_package = "io.github.smyrgeorge.datasuite.proto"; | ||
option java_outer_classname = "AClassProto"; | ||
import "default.proto"; | ||
message AClass { | ||
string a = 1; | ||
repeated AClassAnEnum.Enum b = 2; | ||
} | ||
``` | ||
|
||
```protobuf | ||
// file: default.proto | ||
syntax = "proto3"; | ||
package io.github.smyrgeorge.datasuite.proto; | ||
option java_package = "io.github.smyrgeorge.datasuite.proto"; | ||
option java_outer_classname = "DefaultProto"; | ||
message AClassAnEnum { | ||
enum Enum { | ||
PROTO_EMPTY = 0; | ||
A = 1; | ||
B = 2; | ||
} | ||
} | ||
``` |
66 changes: 6 additions & 60 deletions
66
examples/src/main/kotlin/io/github/smyrgeorge/datasuite/converter/protobuf/examples/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,25 @@ | ||
package io.github.smyrgeorge.datasuite.converter.protobuf.examples | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo | ||
import io.github.smyrgeorge.datasuite.converter.protobuf.KotlinClassToProtobuf | ||
import io.github.smyrgeorge.datasuite.converter.protobuf.annotation.ProtoFile | ||
import io.github.smyrgeorge.datasuite.converter.protobuf.annotation.ProtoSkip | ||
|
||
class Main | ||
|
||
@ProtoFile( | ||
name = "test1.proto", | ||
schema = "schema1", | ||
polymorphism = ["sealed"] | ||
) | ||
data class Test1( | ||
@ProtoSkip | ||
@ProtoFile(name = "a_class.proto", schema = "schema1") | ||
data class AClass( | ||
val a: String, | ||
val test: TestEnum, | ||
val b: List<String>, | ||
@ProtoSkip | ||
val bb: Map<String, List<String>>, | ||
val kind: Kind, | ||
val sealed: TestSealed | ||
val b: List<AnEnum> | ||
) { | ||
enum class Kind { | ||
A, B, C, D, E | ||
enum class AnEnum { | ||
A, B | ||
} | ||
} | ||
|
||
@ProtoFile( | ||
name = "test2.proto", | ||
schema = "schema2", | ||
polymorphism = ["sealed", "testCamel"] | ||
) | ||
data class Test2( | ||
val a: String, | ||
val sealed: TestSealed, | ||
@ProtoSkip | ||
val testCamel: List<TestSealed> | ||
) | ||
|
||
enum class TestEnum { | ||
V1, | ||
V2, | ||
V3 | ||
} | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.EXISTING_PROPERTY, | ||
property = "kind" | ||
) | ||
@JsonSubTypes( | ||
JsonSubTypes.Type(value = TestSealed.S1::class, name = "S1"), | ||
JsonSubTypes.Type(value = TestSealed.S2::class, name = "S2") | ||
) | ||
sealed interface TestSealed { | ||
val kind: String | ||
|
||
data class S1( | ||
override val kind: String | ||
) : TestSealed | ||
|
||
data class S2( | ||
override val kind: String, | ||
@ProtoSkip val test: String, | ||
val b: String | ||
) : TestSealed | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
KotlinClassToProtobuf.generate( | ||
topicPrefix = "sample", | ||
workDirectory = "examples/src/main/proto", | ||
classes = listOf(Test1::class, Test2::class), | ||
classes = listOf(AClass::class), | ||
protoPackage = "io.github.smyrgeorge.datasuite.proto" | ||
) | ||
} |