Skip to content

Commit

Permalink
Merge pull request #103 from cit-consulting/value_class
Browse files Browse the repository at this point in the history
Value (former inline) class serialization support
  • Loading branch information
thake committed Jun 15, 2021
2 parents dbddf97 + ed81e91 commit d8e6467
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fun schemaFor(serializersModule: SerializersModule,

val schemaFor: SchemaFor = when (underlying) {
is AvroDescriptor -> SchemaFor.const(underlying.schema(annos, serializersModule, namingStrategy))
else -> when (descriptor.kind) {
else -> when (descriptor.carrierDescriptor.kind) {
PrimitiveKind.STRING -> SchemaFor.StringSchemaFor
PrimitiveKind.LONG -> SchemaFor.LongSchemaFor
PrimitiveKind.INT -> SchemaFor.IntSchemaFor
Expand Down Expand Up @@ -209,3 +209,8 @@ fun schemaFor(serializersModule: SerializersModule,

return if (descriptor.isNullable) NullableSchemaFor(schemaFor, annos) else schemaFor
}

// copy-paste from kotlinx serialization because it internal
@ExperimentalSerializationApi
internal val SerialDescriptor.carrierDescriptor: SerialDescriptor
get() = if (isInline) getElementDescriptor(0) else this
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.avrokotlin.avro4k.decoder

import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest.ContainsInlineTest
import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest.StringWrapper
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.StringSpec
import org.apache.avro.generic.GenericData

class ValueClassDecoderTest : StringSpec({

"decode value class" {

val id = StringWrapper("100500")
val schema = Avro.default.schema(ContainsInlineTest.serializer())
val record = GenericData.Record(schema)
record.put("id", id.a)

Avro.default.fromRecord(ContainsInlineTest.serializer(), record) shouldBe ContainsInlineTest(id)
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.avrokotlin.avro4k.encoder

import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.ListRecord
import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import org.apache.avro.util.Utf8

class ValueClassEncoderTest : StringSpec({

"encode value class" {

val id = ValueClassSchemaTest.StringWrapper("100500")
val schema = Avro.default.schema(ValueClassSchemaTest.ContainsInlineTest.serializer())
Avro.default.toRecord(ValueClassSchemaTest.ContainsInlineTest.serializer(),
ValueClassSchemaTest.ContainsInlineTest(id)) shouldBe ListRecord(schema, Utf8(id.a))
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.avrokotlin.avro4k.schema

import com.github.avrokotlin.avro4k.Avro
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Serializable

class ValueClassSchemaTest : StringSpec({

"value class should be primitive in schema" {
val expected =
org.apache.avro.Schema.Parser().parse(javaClass.getResourceAsStream("/value_class.json"))
val schema = Avro.default.schema(ContainsInlineTest.serializer())
schema.toString(true) shouldBe expected.toString(true)
}
}) {
@Serializable
@JvmInline
value class StringWrapper(val a: String)

@Serializable
data class ContainsInlineTest(val id: StringWrapper)
}
11 changes: 11 additions & 0 deletions src/test/resources/value_class.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "record",
"name": "ContainsInlineTest",
"namespace": "com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest",
"fields": [
{
"name": "id",
"type": "string"
}
]
}

0 comments on commit d8e6467

Please sign in to comment.