Skip to content

Commit

Permalink
Added java string option to ReflectionSchemaBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 14, 2024
1 parent 9f61760 commit aff0edd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object StringEncoder : Encoder<String> {
return when (schema.type) {
Schema.Type.BYTES -> ByteStringEncoder.encode(schema, value)
Schema.Type.FIXED -> FixedStringEncoder.encode(schema, value)
Schema.Type.STRING -> when (schema.getObjectProp(GenericData.STRING_PROP)) {
Schema.Type.STRING -> when (schema.getProp(GenericData.STRING_PROP)) {
"String" -> value
else -> UTF8StringEncoder.encode(schema, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package com.sksamuel.centurion.avro.generation

import org.apache.avro.Schema
import org.apache.avro.SchemaBuilder
import org.apache.avro.generic.GenericData
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.memberProperties

class ReflectionSchemaBuilder {
class ReflectionSchemaBuilder(
private val useJavaString: Boolean = false,
) {

fun schema(kclass: KClass<*>): Schema {

Expand All @@ -27,6 +30,8 @@ class ReflectionSchemaBuilder {

return when (val classifier = type.classifier) {
String::class -> typeBuilder.stringType()
.also { if (useJavaString) GenericData.setStringType(it, GenericData.StringType.String) }

Boolean::class -> typeBuilder.booleanType()
Int::class -> typeBuilder.intType()
Long::class -> typeBuilder.longType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ enum class UserType { User, Admin }
fun main() {

val user = User(Random.nextLong(), "sammy mcsamface", "[email protected]", Random.nextLong(), UserType.Admin)
val schema = ReflectionSchemaBuilder().schema(User::class)
val schema = ReflectionSchemaBuilder(true).schema(User::class)

GenericData.setStringType(schema, GenericData.StringType.String)

repeat(5) {
val time = measureTime {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.sksamuel.centurion.avro.encoders

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.types.shouldBeTypeOf
import org.apache.avro.SchemaBuilder
import org.apache.avro.generic.GenericData
import org.apache.avro.util.Utf8

class StringEncoderTest : FunSpec({

test("should use java string when prop is set") {
val schema = SchemaBuilder.builder().stringType()
GenericData.setStringType(schema, GenericData.StringType.String)
StringEncoder.encode(schema, "hello").shouldBeTypeOf<String>()
}

test("should use Utf8 when prop is not set") {
val schema = SchemaBuilder.builder().stringType()
StringEncoder.encode(schema, "hello").shouldBeTypeOf<Utf8>()
}

})

0 comments on commit aff0edd

Please sign in to comment.