-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from cit-consulting/value_class
Value (former inline) class serialization support
- Loading branch information
Showing
5 changed files
with
80 additions
and
1 deletion.
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
21 changes: 21 additions & 0 deletions
21
src/test/kotlin/com/github/avrokotlin/avro4k/decoder/ValueClassDecoderTest.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,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) | ||
} | ||
}) |
19 changes: 19 additions & 0 deletions
19
src/test/kotlin/com/github/avrokotlin/avro4k/encoder/ValueClassEncoderTest.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,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)) | ||
} | ||
}) |
23 changes: 23 additions & 0 deletions
23
src/test/kotlin/com/github/avrokotlin/avro4k/schema/ValueClassSchemaTest.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,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) | ||
} |
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,11 @@ | ||
{ | ||
"type": "record", | ||
"name": "ContainsInlineTest", | ||
"namespace": "com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest", | ||
"fields": [ | ||
{ | ||
"name": "id", | ||
"type": "string" | ||
} | ||
] | ||
} |