Skip to content

Commit 12682cc

Browse files
committed
feat(model-api-gen): generate modelql extension functions for writing
for adding/setting children and setting references
1 parent 81cf37a commit 12682cc

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

model-api-gen/src/main/kotlin/org/modelix/metamodel/generator/GeneratorInput.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ internal sealed class ProcessedLink(name: String, uid: String?, optional: Boolea
302302
}
303303

304304
internal class ProcessedChildLink(name: String, uid: String?, optional: Boolean, var multiple: Boolean, type: ProcessedConceptReference, deprecationMessage: String?) :
305-
ProcessedLink(name, uid, optional, type, deprecationMessage)
305+
ProcessedLink(name, uid, optional, type, deprecationMessage) {
306+
307+
fun adderMethodName() = "addTo" + generatedName.take(1).uppercase() + generatedName.drop(1)
308+
}
306309

307310
internal class ProcessedReferenceLink(name: String, uid: String?, optional: Boolean, type: ProcessedConceptReference, deprecationMessage: String?) :
308311
ProcessedLink(name, uid, optional, type, deprecationMessage)

model-api-gen/src/main/kotlin/org/modelix/metamodel/generator/MetaModelGenerator.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.squareup.kotlinpoet.CodeBlock
66
import com.squareup.kotlinpoet.FileSpec
77
import com.squareup.kotlinpoet.FunSpec
88
import com.squareup.kotlinpoet.KModifier
9+
import com.squareup.kotlinpoet.ParameterSpec
910
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
1011
import com.squareup.kotlinpoet.PropertySpec
1112
import com.squareup.kotlinpoet.TypeAliasSpec
@@ -463,6 +464,42 @@ class MetaModelGenerator(
463464
)
464465
.build(),
465466
)
467+
val returnType = IMonoStep::class.asTypeName().parameterizedBy(targetType)
468+
val receiverType = IMonoStep::class.asTypeName().parameterizedBy(concept.nodeWrapperInterfaceType())
469+
if (feature.multiple) {
470+
addFunction(
471+
FunSpec.builder(feature.adderMethodName())
472+
.returns(returnType)
473+
.receiver(receiverType)
474+
.addParameter("concept", ITypedConcept::class.asTypeName())
475+
.addParameter(
476+
ParameterSpec.builder("index", Int::class.asTypeName())
477+
.defaultValue("-1")
478+
.build(),
479+
)
480+
.addStatement(
481+
"return %T.addNewChild(this, %T.%N, index, concept)",
482+
TypedModelQL::class.asTypeName(),
483+
concept.conceptObjectType(),
484+
feature.generatedName,
485+
)
486+
.build(),
487+
)
488+
} else {
489+
addFunction(
490+
FunSpec.builder(feature.setterName())
491+
.returns(returnType)
492+
.receiver(receiverType)
493+
.addParameter("concept", ITypedConcept::class.asTypeName())
494+
.addStatement(
495+
"return %T.setChild(this, %T.%N, concept)",
496+
TypedModelQL::class.asTypeName(),
497+
concept.conceptObjectType(),
498+
feature.generatedName,
499+
)
500+
.build(),
501+
)
502+
}
466503
}
467504

468505
is ProcessedReferenceLink -> {
@@ -504,6 +541,26 @@ class MetaModelGenerator(
504541
.build(),
505542
)
506543
}
544+
545+
val inputStepType = IMonoStep::class.asTypeName()
546+
.parameterizedBy(concept.nodeWrapperInterfaceType())
547+
addFunction(
548+
FunSpec.builder(feature.setterName())
549+
.returns(inputStepType)
550+
.receiver(inputStepType)
551+
.addParameter(
552+
"target",
553+
IMonoStep::class.asTypeName().parameterizedBy(targetType)
554+
.let { if (feature.optional) it.copy(nullable = true) else it },
555+
)
556+
.addStatement(
557+
"return %T.setReference(this, %T.%N, target)",
558+
TypedModelQL::class.asTypeName(),
559+
concept.conceptWrapperInterfaceClass(),
560+
feature.generatedName,
561+
)
562+
.build(),
563+
)
507564
}
508565
}
509566
}

0 commit comments

Comments
 (0)