-
Notifications
You must be signed in to change notification settings - Fork 43
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
Showing
4 changed files
with
70 additions
and
3 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
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
27 changes: 27 additions & 0 deletions
27
centurion-avro/src/main/kotlin/com/sksamuel/centurion/avro/decoders/records.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sksamuel.centurion.avro.decoders | ||
|
||
import org.apache.avro.Schema | ||
import org.apache.avro.generic.GenericRecord | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.primaryConstructor | ||
|
||
class ReflectionRecordDecoder<T : Any>(private val kclass: KClass<T>) : Decoder<T> { | ||
|
||
companion object { | ||
inline operator fun <reified T : Any> invoke() = ReflectionRecordDecoder(T::class) | ||
} | ||
|
||
override fun decode(schema: Schema, value: Any?): T { | ||
require(schema.type == Schema.Type.RECORD) | ||
require(kclass.isData) { "Decoders only support data class: was $kclass" } | ||
require(value is GenericRecord) { "ReflectionRecordDecoder only supports GenericRecords: was $value" } | ||
val args = kclass.primaryConstructor!!.parameters.map { param -> | ||
val field = schema.getField(param.name) | ||
val arg = value.get(param.name) | ||
val decoder = Decoder.decoderFor(param.type) | ||
decoder.decode(field.schema(), arg) | ||
} | ||
require(args.size == kclass.primaryConstructor!!.parameters.size) | ||
return kclass.primaryConstructor!!.call(*args.toTypedArray()) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
centurion-avro/src/test/kotlin/com/sksamuel/centurion/avro/decoders/RecordDecoderTest.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.sksamuel.centurion.avro.decoders | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.apache.avro.SchemaBuilder | ||
import org.apache.avro.generic.GenericData | ||
import org.apache.avro.util.Utf8 | ||
|
||
class RecordDecoderTest : FunSpec({ | ||
|
||
test("basic record decoder") { | ||
data class Foo(val a: String, val b: Boolean) | ||
val schema = SchemaBuilder.record("Foo").fields().requiredString("a").requiredBoolean("b").endRecord() | ||
|
||
val record = GenericData.Record(schema) | ||
record.put("a", Utf8("hello")) | ||
record.put("b", true) | ||
|
||
ReflectionRecordDecoder<Foo>().decode(schema, record) shouldBe Foo("hello", true) | ||
} | ||
|
||
}) |