diff --git a/README.md b/README.md index af10c3a..c1e34d9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,95 @@ # protogen4k -A small proto file generator from kotlin data classes. + +![Build](https://github.com/smyrgeorge/protogen4k/actions/workflows/ci.yml/badge.svg) +![Maven Central](https://img.shields.io/maven-central/v/io.github.smyrgeorge/protogen4k) +![GitHub License](https://img.shields.io/github/license/smyrgeorge/protogen4k) +![GitHub commit activity](https://img.shields.io/github/commit-activity/w/smyrgeorge/protogen4k) +![GitHub issues](https://img.shields.io/github/issues/smyrgeorge/protogen4k) + +A small and simple `.proto` file generator. Generates a protobuf schema from the given kotlin classes. + +### Usage + +```xml + + + io.github.smyrgeorge + protogen4k + x.y.z + +``` + +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 +) { + enum class AnEnum { + A, B + } +} + +fun main(args: Array) { + 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; + } +} +``` diff --git a/examples/src/main/kotlin/io/github/smyrgeorge/datasuite/converter/protobuf/examples/Main.kt b/examples/src/main/kotlin/io/github/smyrgeorge/datasuite/converter/protobuf/examples/Main.kt index b81b844..fd57f7a 100644 --- a/examples/src/main/kotlin/io/github/smyrgeorge/datasuite/converter/protobuf/examples/Main.kt +++ b/examples/src/main/kotlin/io/github/smyrgeorge/datasuite/converter/protobuf/examples/Main.kt @@ -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, - @ProtoSkip - val bb: Map>, - val kind: Kind, - val sealed: TestSealed + val b: List ) { - 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 -) - -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) { 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" ) }