Skip to content

Commit

Permalink
Added maps and nested maps to ReflectionSchemaBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 14, 2024
1 parent ff4fef4 commit 653b8e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ReflectionSchemaBuilder {
Float::class -> typeBuilder.floatType()
Set::class -> typeBuilder.array().items(schemaFor(type.arguments.first().type!!))
List::class -> typeBuilder.array().items(schemaFor(type.arguments.first().type!!))
Map::class -> typeBuilder.map().values(schemaFor(type.arguments[1].type!!))
is KClass<*> -> if (classifier.java.isEnum)
typeBuilder
.enumeration(classifier.java.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ data class Foo2(val a: String?, val b: Boolean, val c: Long)
data class Foo3(val set1: Set<Int>, val set2: Set<Int?>)
data class Foo4(val list1: List<Int>, val list2: List<Int?>)
data class Foo5(val wine: Wine)
data class Foo6(val map: Map<String, Boolean>)
data class Foo7(val map: Map<String, Map<String, String>>)

class ReflectionSchemaBuilderTest : FunSpec({

Expand Down Expand Up @@ -60,4 +62,20 @@ class ReflectionSchemaBuilderTest : FunSpec({
.endRecord()
ReflectionSchemaBuilder().schema(Foo5::class) shouldBe expected
}

test("maps") {
val expected = SchemaBuilder.record("Foo6").namespace(Foo6::class.java.packageName)
.fields()
.name("map").type().map().values().booleanType().noDefault()
.endRecord()
ReflectionSchemaBuilder().schema(Foo6::class) shouldBe expected
}

test("maps of maps") {
val expected = SchemaBuilder.record("Foo7").namespace(Foo7::class.java.packageName)
.fields()
.name("map").type().map().values(SchemaBuilder.builder().map().values().stringType()).noDefault()
.endRecord()
ReflectionSchemaBuilder().schema(Foo7::class) shouldBe expected
}
})

0 comments on commit 653b8e1

Please sign in to comment.