Skip to content

Commit

Permalink
Fix generator for open class (use polymorphic serializers)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliya-usov committed Sep 15, 2023
1 parent 20a8393 commit 8ef56aa
Show file tree
Hide file tree
Showing 14 changed files with 1,354 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ open class CSharp50Generator(
protected fun Member.Reactive.customSerializers(containing: Declaration, leadingComma: Boolean, ignorePerClientId: Boolean = false): String {
if(context != null && !ignorePerClientId)
return leadingComma.condstr { ", " } + perClientIdMapValueFactory(containing)
val res = genericParams.joinToString { it.readerDelegateRef(containing, true) + ", " + it.writerDelegateRef(containing, false) }
val res = genericParams.joinToString {
val allowSpecificOpenTypeReference = false
it.readerDelegateRef(containing, allowSpecificOpenTypeReference) + ", " + it.writerDelegateRef(containing, allowSpecificOpenTypeReference)
}
return (genericParams.isNotEmpty() && leadingComma).condstr { ", " } + res
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,30 +263,30 @@ open class Kotlin11Generator(
protected fun Member.Reactive.customSerializers(scope: Declaration, ignorePerClientId: Boolean = false) : List<String> {
if (context != null && !ignorePerClientId)
return listOf(context!!.longRef(scope), perClientIdMapValueFactory(scope))
return genericParams.asList().map { it.serializerRef(scope) }
return genericParams.asList().map { it.serializerRef(scope, false) }
}

protected fun IDeclaration.sanitizedName(scope: IDeclaration) : String {
val needQualification = namespace != scope.namespace
return needQualification.condstr { namespace + "." } + name
}

protected fun IType.leafSerializerRef(scope: Declaration): String? {
protected fun IType.leafSerializerRef(scope: Declaration, allowSpecificOpenTypeReference: Boolean): String? {
return when (this) {
is Enum -> "${sanitizedName(scope)}.marshaller"
is PredefinedType -> "FrameworkMarshallers.$name"
is Declaration ->
this.getSetting(Intrinsic)?.marshallerObjectFqn ?: run {
val name = sanitizedName(scope)
if (isAbstract) "AbstractPolymorphic($name)" else name
if (isAbstract || isOpen && !allowSpecificOpenTypeReference) "AbstractPolymorphic($name)" else name
}

is IArray -> if (this.isPrimitivesArray) "FrameworkMarshallers.${substitutedName(scope)}" else null
else -> null
}
}

protected fun IType.serializerRef(scope: Declaration) : String = leafSerializerRef(scope) ?: when(this) {
protected fun IType.serializerRef(scope: Declaration, allowSpecificOpenTypeReference: Boolean) : String = leafSerializerRef(scope, allowSpecificOpenTypeReference) ?: when(this) {
is InternedScalar -> "__${name}At${internKey.keyName}Serializer"
else -> "__${name}Serializer"
}
Expand Down Expand Up @@ -450,7 +450,7 @@ open class Kotlin11Generator(
if(decl is Toplevel) {
decl.declaredTypes.forEach {
if(it is Context.Generated)
+"object ${it.keyName}: ${it.contextImplementationFqn}<${it.type.substitutedName(decl)}>(\"${it.keyName}\", ${it.isHeavyKey}, ${it.type.serializerRef(decl)})"
+"object ${it.keyName}: ${it.contextImplementationFqn}<${it.type.substitutedName(decl)}>(\"${it.keyName}\", ${it.isHeavyKey}, ${it.type.serializerRef(decl, false)})"
}
}
}
Expand Down Expand Up @@ -580,7 +580,7 @@ open class Kotlin11Generator(
}

protected fun PrettyPrinter.customSerializersTrait(decl: Declaration) {
fun IType.serializerBuilder() : String = leafSerializerRef(decl)?: when (this) {
fun IType.serializerBuilder() : String = leafSerializerRef(decl, false)?: when (this) {
is IArray -> itemType.serializerBuilder() + ".array()"
is IImmutableList -> itemType.serializerBuilder() + ".list()"
is INullable -> itemType.serializerBuilder() + ".nullable()"
Expand All @@ -593,10 +593,10 @@ open class Kotlin11Generator(
.filterIsInstance<Member.Reactive>()
.flatMap { it.genericParams.toList() }
.distinct()
.filter { it.leafSerializerRef(decl) == null }
.filter { it.leafSerializerRef(decl, false) == null }

allTypesForDelegation
.map { "private val ${it.serializerRef(decl)} = ${it.serializerBuilder()}" }
.map { "private val ${it.serializerRef(decl, false)} = ${it.serializerBuilder()}" }
.distinct()
.forEach { println(it) }
}
Expand All @@ -610,7 +610,7 @@ open class Kotlin11Generator(
protected fun PrettyPrinter.registerSerializersTrait(decl: Toplevel, types: List<Declaration>) {
block("override fun registerSerializersCore(serializers: ISerializers) ") {
types.filter { !it.isAbstract }.filterIsInstance<IType>().println {
"serializers.register(${it.serializerRef(decl)})"
"serializers.register(${it.serializerRef(decl, true)})"
}

if (decl is Root) {
Expand Down Expand Up @@ -727,7 +727,8 @@ open class Kotlin11Generator(
else -> "${ctorSimpleName(decl)}(${delegatedBy.reader()})"
}
is Member.Reactive -> {
val params = (listOf("ctx", "buffer") + customSerializers(decl)).joinToString (", ")
val customSerializers = customSerializers(decl)
val params = (listOf("ctx", "buffer") + customSerializers).joinToString (", ")
if(context != null) {
"RdPerContextMap.read(${context!!.longRef(decl)}, buffer) ${this.perClientIdMapValueFactory(decl)}"
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jetbrains.rd.generator.test.cases.generator.csharp

import com.jetbrains.rd.generator.test.cases.generator.testModels.RecursivePolymorphicModel
import com.jetbrains.rd.generator.test.cases.generator.testModels.RecursivePolymorphicModelRoot
import com.jetbrains.rd.generator.testframework.CSharpRdGenOutputTest
import com.jetbrains.rd.generator.testframework.KotlinRdGenOutputTest
import org.junit.jupiter.api.Test

class RecursivePolymorphicCSharpTest : CSharpRdGenOutputTest() {
companion object {
const val testName = "recursivePolymorphicModelTest"
}

override val testName = Companion.testName

@Test
fun test1() = doTest<RecursivePolymorphicModelRoot>(RecursivePolymorphicModelRoot::class.java, RecursivePolymorphicModel::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jetbrains.rd.generator.test.cases.generator.kotlin

import com.jetbrains.rd.generator.test.cases.generator.testModels.RecursivePolymorphicModel
import com.jetbrains.rd.generator.test.cases.generator.testModels.RecursivePolymorphicModelRoot
import com.jetbrains.rd.generator.testframework.KotlinRdGenOutputTest
import org.junit.jupiter.api.Test

class RecursivePolymorphicKotlinTest : KotlinRdGenOutputTest() {
companion object {
const val testName = "recursivePolymorphicModelTest"
}

override val testName = Companion.testName

@Test
fun test1() = doTest<RecursivePolymorphicModelRoot>(RecursivePolymorphicModelRoot::class.java, RecursivePolymorphicModel::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jetbrains.rd.generator.test.cases.generator.testModels

import com.jetbrains.rd.generator.nova.*
import com.jetbrains.rd.generator.test.cases.generator.csharp.RecursivePolymorphicCSharpTest
import com.jetbrains.rd.generator.test.cases.generator.kotlin.RecursivePolymorphicKotlinTest
import com.jetbrains.rd.generator.testframework.CSharpRdGenOutputTest
import com.jetbrains.rd.generator.testframework.KotlinRdGenOutputTest

object RecursivePolymorphicModelRoot : Root(
*KotlinRdGenOutputTest.generators(RecursivePolymorphicKotlinTest.testName, "org.example"),
*CSharpRdGenOutputTest.generators(RecursivePolymorphicCSharpTest.testName, "org.example")
)

object RecursivePolymorphicModel : Ext(RecursivePolymorphicModelRoot) {
internal val BeTreeGridLine = openclass BeTreeGridLine@ {
list("children", this@BeTreeGridLine)
}

init {
property("line", BeTreeGridLine)
property("list", immutableList(BeTreeGridLine))
}
}
Loading

0 comments on commit 8ef56aa

Please sign in to comment.