Skip to content

Commit

Permalink
Added decoding of sets and lists of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 14, 2024
1 parent 41fd0cb commit f568a73
Showing 1 changed file with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,58 @@ class ReflectionRecordDecoderTest : FunSpec({
}

test("sets") {
data class Foo(val set1: Set<Int>, val set2: Set<Long?>)
data class Foo(val set1: Set<Int>, val set2: Set<Long?>, val set3: Set<Wine>)

val enum = SchemaBuilder.enumeration("Wine").symbols("Malbec", "Shiraz")
val schema = SchemaBuilder.record("Foo").fields()
.name("set1").type().array().items().intType().noDefault()
.name("set2").type().array().items().type(SchemaBuilder.nullable().longType()).noDefault()
.name("set3").type().array().items().type(enum).noDefault()
.endRecord()

val record = GenericData.Record(schema)
record.put("set1", listOf(1, 2))
record.put("set2", listOf(1, null, 2))

ReflectionRecordDecoder<Foo>().decode(schema, record) shouldBe Foo(setOf(1, 2), setOf(1L, null, 2L))
record.put(
"set3",
listOf(
GenericData.get().createEnum("Shiraz", enum),
GenericData.get().createEnum("Malbec", enum),
)
)

ReflectionRecordDecoder<Foo>().decode(schema, record) shouldBe Foo(
setOf(1, 2),
setOf(1L, null, 2L),
setOf(Wine.Shiraz, Wine.Malbec),
)
}

test("list") {
data class Foo(val list1: List<Int>, val list2: List<Long?>)
data class Foo(val list1: List<Int>, val list2: List<Long?>, val list3: List<Wine>)

val enum = SchemaBuilder.enumeration("Wine").symbols("Malbec", "Shiraz")
val schema = SchemaBuilder.record("Foo").fields()
.name("list1").type().array().items().intType().noDefault()
.name("list2").type().array().items().type(SchemaBuilder.nullable().longType()).noDefault()
.name("list3").type().array().items().type(enum).noDefault()
.endRecord()

val record = GenericData.Record(schema)
record.put("list1", listOf(1, 2))
record.put("list2", listOf(1, null, 2))

ReflectionRecordDecoder<Foo>().decode(schema, record) shouldBe Foo(listOf(1, 2), listOf(1L, null, 2L))
record.put(
"list3",
listOf(
GenericData.get().createEnum("Shiraz", enum),
GenericData.get().createEnum("Malbec", enum),
)
)

ReflectionRecordDecoder<Foo>().decode(schema, record) shouldBe Foo(
listOf(1, 2),
listOf(1L, null, 2L),
listOf(Wine.Shiraz, Wine.Malbec),
)
}
})

0 comments on commit f568a73

Please sign in to comment.