From 260f7706409dd442f698c370c2ed613e3d9353f6 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 14 Nov 2023 09:36:09 -0800 Subject: [PATCH] fix: code gen actual dataref types --- .../block/ftl/generator/ModuleGenerator.kt | 32 +++++----- .../ftl/generator/ModuleGeneratorTest.kt | 58 +++++++++---------- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt b/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt index 160cd12129..48e2b5a82f 100644 --- a/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt +++ b/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt @@ -1,16 +1,7 @@ package xyz.block.ftl.generator -import com.squareup.kotlinpoet.AnnotationSpec -import com.squareup.kotlinpoet.ClassName -import com.squareup.kotlinpoet.FileSpec -import com.squareup.kotlinpoet.FunSpec -import com.squareup.kotlinpoet.KModifier -import com.squareup.kotlinpoet.ParameterSpec +import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy -import com.squareup.kotlinpoet.PropertySpec -import com.squareup.kotlinpoet.TypeName -import com.squareup.kotlinpoet.TypeSpec -import com.squareup.kotlinpoet.TypeVariableName import xyz.block.ftl.Context import xyz.block.ftl.Ignore import xyz.block.ftl.Ingress @@ -59,7 +50,7 @@ class ModuleGenerator() { ) val types = module.decls.mapNotNull { it.data_ } - types.forEach { file.addType(buildDataClass(it)) } + types.forEach { file.addType(buildDataClass(it, namespace)) } val verbs = module.decls.mapNotNull { it.verb } verbs.forEach { moduleClass.addFunction(buildVerbFunction(className, it)) } @@ -68,7 +59,7 @@ class ModuleGenerator() { return file.build() } - private fun buildDataClass(type: Data): TypeSpec { + private fun buildDataClass(type: Data, namespace: String): TypeSpec { val dataClassBuilder = TypeSpec.classBuilder(type.name) .addModifiers(KModifier.DATA) .addKdoc(type.comments.joinToString("\n")) @@ -77,9 +68,9 @@ class ModuleGenerator() { type.fields.forEach { field -> dataClassBuilder.addKdoc(field.comments.joinToString("\n")) field.type?.let { - dataConstructorBuilder.addParameter(field.name, getTypeClass(it)) + dataConstructorBuilder.addParameter(field.name, getTypeClass(it, namespace)) dataClassBuilder.addProperty( - PropertySpec.builder(field.name, getTypeClass(it)).initializer(field.name).build() + PropertySpec.builder(field.name, getTypeClass(it, namespace)).initializer(field.name).build() ) } } @@ -135,7 +126,7 @@ class ModuleGenerator() { return verbFunBuilder.build() } - private fun getTypeClass(type: Type): TypeName { + private fun getTypeClass(type: Type, namespace: String): TypeName { return when { type.int != null -> ClassName("kotlin", "Long") type.float != null -> ClassName("kotlin", "Float") @@ -146,7 +137,7 @@ class ModuleGenerator() { val element = type.array?.element ?: throw IllegalArgumentException( "Missing element type in kotlin array generator" ) - val elementType = getTypeClass(element) + val elementType = getTypeClass(element, namespace) val arrayList = ClassName("kotlin.collections", "ArrayList") arrayList.parameterizedBy(elementType) } @@ -158,11 +149,14 @@ class ModuleGenerator() { val value = type.map?.value_ ?: throw IllegalArgumentException( "Missing map value in kotlin map generator" ) - map.parameterizedBy(getTypeClass(key), getTypeClass(value)) + map.parameterizedBy(getTypeClass(key, namespace), getTypeClass(value, namespace)) } - type.verbRef != null -> ClassName("xyz.block.ftl.v1.schema", "VerbRef") - type.dataRef != null -> ClassName("xyz.block.ftl.v1.schema", "DataRef") + type.dataRef != null -> { + var module = type.dataRef.module + type.dataRef.module.ifEmpty { module = namespace } + ClassName(module, type.dataRef.name) + } else -> throw IllegalArgumentException("Unknown type in kotlin generator") } diff --git a/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt b/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt index 27e25586d8..ab2166553f 100644 --- a/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt +++ b/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt @@ -51,32 +51,34 @@ public class TestModule() Decl( data_ = Data( comments = listOf("Response comments"), name = "TestResponse", fields = listOf( - Field(name = "int", type = Type(int = Int())), - Field(name = "float", type = Type(float = Float())), - Field(name = "string", type = Type(string = String())), - Field(name = "bool", type = Type(bool = Bool())), - Field(name = "time", type = Type(time = Time())), - Field(name = "array", type = Type(array = Array(element = Type(string = String())))), - Field( - name = "nestedArray", type = Type( - array = Array(element = Type(array = Array(element = Type(string = String())))) + Field(name = "int", type = Type(int = Int())), + Field(name = "float", type = Type(float = Float())), + Field(name = "string", type = Type(string = String())), + Field(name = "bool", type = Type(bool = Bool())), + Field(name = "time", type = Type(time = Time())), + Field(name = "array", type = Type(array = Array(element = Type(string = String())))), + Field( + name = "nestedArray", type = Type( + array = Array(element = Type(array = Array(element = Type(string = String())))) + ) + ), + Field(name = "dataRefArray", type = Type( + array = Array(element = Type(dataRef = DataRef(name = "TestRequest", module = "ftl.test"))) + )), + Field( + name = "map", + type = Type(map = Map(key = Type(string = String()), value_ = Type(int = Int()))) + ), + Field( + name = "nestedMap", type = Type( + map = Map( + key = Type(string = String()), + value_ = Type(map = Map(key = Type(string = String()), value_ = Type(int = Int()))) + ) + ) + ), + Field(name = "dataRef", type = Type(dataRef = DataRef(name = "TestRequest"))), ) - ), - Field( - name = "map", - type = Type(map = Map(key = Type(string = String()), value_ = Type(int = Int()))) - ), - Field( - name = "nestedMap", type = Type( - map = Map( - key = Type(string = String()), - value_ = Type(map = Map(key = Type(string = String()), value_ = Type(int = Int()))) - ) - ) - ), - Field(name = "verbRef", type = Type(verbRef = VerbRef())), - Field(name = "dataRef", type = Type(dataRef = DataRef())), - ) ) ), ) @@ -96,8 +98,6 @@ import kotlin.Unit import kotlin.collections.ArrayList import kotlin.collections.Map import xyz.block.ftl.Ignore -import xyz.block.ftl.v1.schema.DataRef -import xyz.block.ftl.v1.schema.VerbRef /** * Request comments @@ -117,10 +117,10 @@ public data class TestResponse( public val time: OffsetDateTime, public val array: ArrayList, public val nestedArray: ArrayList>, + public val dataRefArray: ArrayList, public val map: Map, public val nestedMap: Map>, - public val verbRef: VerbRef, - public val dataRef: DataRef, + public val dataRef: TestRequest, ) @Ignore