Skip to content

Commit

Permalink
fix(codegen): actual dataref types (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Nov 14, 2023
1 parent a996136 commit c72c615
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)) }
Expand All @@ -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"))
Expand All @@ -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()
)
}
}
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
)
)
),
)
Expand All @@ -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
Expand All @@ -117,10 +117,10 @@ public data class TestResponse(
public val time: OffsetDateTime,
public val array: ArrayList<String>,
public val nestedArray: ArrayList<ArrayList<String>>,
public val dataRefArray: ArrayList<TestRequest>,
public val map: Map<String, Long>,
public val nestedMap: Map<String, Map<String, Long>>,
public val verbRef: VerbRef,
public val dataRef: DataRef,
public val dataRef: TestRequest,
)
@Ignore
Expand Down

0 comments on commit c72c615

Please sign in to comment.