Skip to content

Commit 81cf37a

Browse files
committed
feat(modelql): typed addNewChild, setChild and setReference
1 parent 2621363 commit 81cf37a

File tree

1 file changed

+21
-0
lines changed
  • modelql-typed/src/commonMain/kotlin/org/modelix/modelql/typed

1 file changed

+21
-0
lines changed

modelql-typed/src/commonMain/kotlin/org/modelix/modelql/typed/TypedModelQL.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ import org.modelix.modelql.core.flatMap
5959
import org.modelix.modelql.core.inSet
6060
import org.modelix.modelql.core.map
6161
import org.modelix.modelql.core.mapIfNotNull
62+
import org.modelix.modelql.core.nullMono
6263
import org.modelix.modelql.core.orNull
6364
import org.modelix.modelql.core.stepOutputSerializer
6465
import org.modelix.modelql.core.toBoolean
6566
import org.modelix.modelql.core.toInt
6667
import org.modelix.modelql.core.upcast
6768
import org.modelix.modelql.untyped.UntypedModelQL
69+
import org.modelix.modelql.untyped.addNewChild
6870
import org.modelix.modelql.untyped.children
6971
import org.modelix.modelql.untyped.conceptReference
7072
import org.modelix.modelql.untyped.descendants
@@ -75,7 +77,9 @@ import org.modelix.modelql.untyped.ofConcept
7577
import org.modelix.modelql.untyped.property
7678
import org.modelix.modelql.untyped.query
7779
import org.modelix.modelql.untyped.reference
80+
import org.modelix.modelql.untyped.remove
7881
import org.modelix.modelql.untyped.setProperty
82+
import org.modelix.modelql.untyped.setReference
7983
import kotlin.jvm.JvmName
8084
import kotlin.reflect.KClass
8185

@@ -140,6 +144,17 @@ object TypedModelQL {
140144
return input.untyped().flatMap { it.children(link.untyped().key()) }.typedUnsafe(link.getTypedChildConcept().getInstanceInterface())
141145
}
142146

147+
fun <ParentT : ITypedNode, ChildT : ITypedNode> addNewChild(input: IMonoStep<ParentT>, link: ITypedChildListLink<ChildT>, index: Int = -1, concept: ITypedConcept): IMonoStep<ChildT> {
148+
val conceptRef = ConceptReference(concept.untyped().getUID())
149+
return input.untyped().addNewChild(link.untyped(), index, conceptRef).typedUnsafe(link.getTypedChildConcept().getInstanceInterface())
150+
}
151+
152+
fun <ParentT : ITypedNode, ChildT : ITypedNode> setChild(input: IMonoStep<ParentT>, link: ITypedSingleChildLink<ChildT>, concept: ITypedConcept): IMonoStep<ChildT> {
153+
val conceptRef = ConceptReference(concept.untyped().getUID())
154+
input.untyped().children(link.untyped().key()).firstOrNull().mapIfNotNull { it.remove() }
155+
return input.untyped().addNewChild(link.untyped(), conceptRef).typedUnsafe(link.getTypedChildConcept().getInstanceInterface())
156+
}
157+
143158
fun <SourceT : ITypedNode, TargetT : ITypedNode> reference(input: IMonoStep<SourceT>, link: ITypedReferenceLink<TargetT>): IMonoStep<TargetT> {
144159
return input.untyped().reference(link.untyped().key()).typedUnsafe<TargetT>(link.getTypedTargetConcept().getInstanceInterface())
145160
}
@@ -152,6 +167,12 @@ object TypedModelQL {
152167
fun <SourceT : ITypedNode, TargetT : ITypedNode> referenceOrNull(input: IFluxStep<SourceT>, link: ITypedReferenceLink<TargetT>): IFluxStep<TargetT?> {
153168
return input.map { referenceOrNull(it, link) }
154169
}
170+
171+
fun <SourceT : ITypedNode, TargetT : ITypedNode> setReference(input: IMonoStep<SourceT>, link: ITypedReferenceLink<TargetT>, target: IMonoStep<TargetT?>?): IMonoStep<SourceT> {
172+
val targetOrNull = target?.untyped() ?: nullMono<INode>()
173+
input.untyped().setReference(link.untyped(), targetOrNull)
174+
return input
175+
}
155176
}
156177

157178
/** Doesn't check the concept when executed remotely. Use .ofConcept() to convert a node in a type safe way. */

0 commit comments

Comments
 (0)