Skip to content

Commit eaa8cf7

Browse files
committed
refactor(model-sync-lib): unified id property key by moving it to node data
1 parent 86b088e commit eaa8cf7

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

model-api/src/commonMain/kotlin/org/modelix/model/data/ModelData.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ data class NodeData(
7979
val children: List<NodeData> = emptyList(),
8080
val properties: Map<String, String> = emptyMap(),
8181
val references: Map<String, String> = emptyMap()
82-
)
82+
) {
83+
companion object {
84+
const val idPropertyKey = "originalId"
85+
}
86+
}
8387

8488
fun NodeData.uid(model: ModelData): String {
8589
return (model.id ?: throw IllegalArgumentException("Model has no ID")) +

model-sync-lib/src/main/kotlin/org/modelix/model/sync/ModelExporter.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import org.modelix.model.data.associateWithNotNull
1010
import java.io.File
1111

1212
class ModelExporter(private val branch: IBranch) {
13-
companion object {
14-
const val idRole = "originalId"
15-
}
1613

1714
fun export(outputFile: File) {
1815
lateinit var modelData: ModelData
@@ -28,14 +25,15 @@ class ModelExporter(private val branch: IBranch) {
2825
}
2926

3027
fun INode.asExported() : NodeData {
28+
val idKey = NodeData.idPropertyKey
3129
return NodeData(
32-
id = getPropertyValue(ModelExporter.idRole) ?: reference.serialize(),
30+
id = getPropertyValue(idKey) ?: reference.serialize(),
3331
concept = concept?.getUID(),
3432
role = roleInParent,
35-
properties = getPropertyRoles().associateWithNotNull { getPropertyValue(it) }.filterKeys { it != ModelExporter.idRole },
33+
properties = getPropertyRoles().associateWithNotNull { getPropertyValue(it) }.filterKeys { it != idKey },
3634
references = getReferenceRoles().associateWithNotNull {
3735
getReferenceTarget(it)?.let { node ->
38-
node.getPropertyValue(ModelExporter.idRole) ?: node.reference.serialize()
36+
node.getPropertyValue(idKey) ?: node.reference.serialize()
3937
}
4038
},
4139
children = allChildren.map { it.asExported() }

model-sync-lib/src/main/kotlin/org/modelix/model/sync/ModelImporter.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import org.modelix.model.data.NodeData
66

77
class ModelImporter(private val branch: IBranch) {
88

9-
companion object {
10-
const val idRole = "originalId"
11-
}
12-
139
private lateinit var originalIdToRef: MutableMap<String, INodeReference>
1410

1511
fun import(data: ModelData) {
@@ -63,7 +59,7 @@ class ModelImporter(private val branch: IBranch) {
6359
toBeAdded.forEach {
6460
val index = nodeData.children.indexOf(it)
6561
val createdNode = node.addNewChild(it.role, index, it.concept?.let { s -> ConceptReference(s) })
66-
createdNode.setPropertyValue(idRole, it.properties[idRole] ?: it.id)
62+
createdNode.setPropertyValue(NodeData.idPropertyKey, it.originalId())
6763
}
6864
toBeRemoved.forEach { node.removeChild(it) }
6965
syncChildOrder(node, nodeData)
@@ -84,11 +80,11 @@ class ModelImporter(private val branch: IBranch) {
8480
}
8581

8682
private fun INode.originalId(): String? {
87-
return this.getPropertyValue(idRole)
83+
return this.getPropertyValue(NodeData.idPropertyKey)
8884
}
8985

9086
private fun NodeData.originalId(): String? {
91-
return properties[idRole] ?: id
87+
return properties[NodeData.idPropertyKey] ?: id
9288
}
9389

9490
}

model-sync-lib/src/test/kotlin/org/modelix/model/sync/ModelImporterTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.modelix.model.api.PBranch
99
import org.modelix.model.api.getRootNode
1010
import org.modelix.model.client.IdGenerator
1111
import org.modelix.model.data.ModelData
12+
import org.modelix.model.data.NodeData
1213
import org.modelix.model.data.asData
1314
import org.modelix.model.lazy.CLTree
1415
import org.modelix.model.lazy.ObjectStoreCache
@@ -48,8 +49,8 @@ class ModelImporterTest {
4849
val expectedNode = newModel.root.children[0]
4950
val expectedProperties = expectedNode.properties
5051
val actualProperties = branch.getRootNode().allChildren.first().asData().properties
51-
assertEquals(expectedProperties, actualProperties.filterKeys { it != ModelImporter.idRole })
52-
assertEquals(expectedNode.id, actualProperties[ModelImporter.idRole])
52+
assertEquals(expectedProperties, actualProperties.filterKeys { it != NodeData.idPropertyKey })
53+
assertEquals(expectedNode.id, actualProperties[NodeData.idPropertyKey])
5354
}
5455
}
5556

@@ -70,8 +71,8 @@ class ModelImporterTest {
7071
branch.runRead {
7172
val index = 2
7273
val node = branch.getRootNode().allChildren.toList()[index]
73-
val specifiedOrder = newModel.root.children[index].children.map { it.properties[ModelImporter.idRole] ?: it.id }
74-
val actualOrder = node.allChildren.map { it.getPropertyValue(ModelImporter.idRole) }
74+
val specifiedOrder = newModel.root.children[index].children.map { it.properties[NodeData.idPropertyKey] ?: it.id }
75+
val actualOrder = node.allChildren.map { it.getPropertyValue(NodeData.idPropertyKey) }
7576
assertEquals(specifiedOrder, actualOrder)
7677
}
7778
}

0 commit comments

Comments
 (0)