Skip to content

Commit a2385d9

Browse files
committed
feat(model-sync-lib): added function to directly import a json file
1 parent eaa8cf7 commit a2385d9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ package org.modelix.model.sync
33
import org.modelix.model.api.*
44
import org.modelix.model.data.ModelData
55
import org.modelix.model.data.NodeData
6+
import java.io.File
67

78
class ModelImporter(private val branch: IBranch) {
89

910
private lateinit var originalIdToRef: MutableMap<String, INodeReference>
1011

12+
fun import(jsonFile: File) {
13+
require(jsonFile.exists())
14+
require(jsonFile.extension == "json")
15+
16+
val data = ModelData.fromJson(jsonFile.readText())
17+
import(data)
18+
}
19+
1120
fun import(data: ModelData) {
1221
originalIdToRef = mutableMapOf()
1322
branch.runWrite {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ModelImporterTest {
2727
@BeforeAll
2828
fun `load and import model`() {
2929
model = ModelData.fromJson(File("src/test/resources/model.json").readText())
30-
newModel = ModelData.fromJson(File("src/test/resources/newmodel.json").readText())
30+
val newModelFile = File("src/test/resources/newmodel.json")
31+
newModel = ModelData.fromJson(newModelFile.readText())
3132

3233
val tree = CLTree(ObjectStoreCache(MapBaseStore()))
3334
branch = PBranch(tree, IdGenerator.getInstance(1))
@@ -36,7 +37,7 @@ class ModelImporterTest {
3637
model.load(branch)
3738
// println("PRE-SPEC ${model.toJson()}")
3839
// println("PRE-LOADED ${branch.getRootNode().toJson()}")
39-
ModelImporter(branch).import(newModel)
40+
ModelImporter(branch).import(newModelFile)
4041
// println("POST-SPEC ${newModel.root.toJson()}")
4142
// println("POST-LOADED ${branch.getRootNode().toJson()}")
4243
}

0 commit comments

Comments
 (0)