Skip to content

Commit

Permalink
Added enum support to ReflectionSchemaBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 14, 2024
1 parent 67f9893 commit ff4fef4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class ReflectionSchemaBuilder {
Float::class -> typeBuilder.floatType()
Set::class -> typeBuilder.array().items(schemaFor(type.arguments.first().type!!))
List::class -> typeBuilder.array().items(schemaFor(type.arguments.first().type!!))
is KClass<*> -> if (classifier.java.isEnum)
typeBuilder
.enumeration(classifier.java.name)
.namespace(classifier.java.packageName)
.symbols(*classifier.java.enumConstants.map { (it as Enum<*>).name }
.toTypedArray())
else
error("Unsupported type $classifier")

else -> error("Unsupported type $classifier")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.sksamuel.centurion.avro.generation

import com.sksamuel.centurion.avro.encoders.Wine
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.apache.avro.SchemaBuilder


data class Foo1(val a: String, val b: Boolean, val c: Long)
data class Foo2(val a: String?, val b: Boolean, val c: Long)
data class Foo3(val set1: Set<Int>, val set2: Set<Int?>)
data class Foo4(val list1: List<Int>, val list2: List<Int?>)
data class Foo5(val wine: Wine)

class ReflectionSchemaBuilderTest : FunSpec({

Expand Down Expand Up @@ -49,4 +52,12 @@ class ReflectionSchemaBuilderTest : FunSpec({
ReflectionSchemaBuilder().schema(Foo4::class) shouldBe expected
}

test("enums") {
val expected = SchemaBuilder.record("Foo5").namespace(Foo5::class.java.packageName)
.fields()
.name("wine").type().enumeration("Wine").namespace(Wine::class.java.packageName).symbols("Shiraz", "Malbec")
.noDefault()
.endRecord()
ReflectionSchemaBuilder().schema(Foo5::class) shouldBe expected
}
})

0 comments on commit ff4fef4

Please sign in to comment.