Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
smyrgeorge committed May 15, 2024
1 parent 476dc8d commit 7057355
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 61 deletions.
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

<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;
}
}
```
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"
)
}

0 comments on commit 7057355

Please sign in to comment.