Skip to content

Commit 4a95d33

Browse files
authored
Merge pull request #178 from modelix/issue/MODELIX-473
MODELIX-473 Add javascript target for model-sync-lib
2 parents 0d2f84e + b1f1f47 commit 4a95d33

File tree

9 files changed

+79
-28
lines changed

9 files changed

+79
-28
lines changed

model-sync-lib/build.gradle.kts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ repositories {
77
}
88

99
kotlin {
10+
js(IR) {
11+
browser()
12+
}
1013
jvm {
1114
jvmToolchain(11)
1215
testRuns["test"].executionTask.configure {
@@ -21,7 +24,7 @@ kotlin {
2124
}
2225
}
2326

24-
val commonTest by getting {
27+
val jvmTest by getting {
2528
dependencies {
2629
implementation(project(":model-api"))
2730
implementation(libs.kotlin.serialization.json)
@@ -31,11 +34,3 @@ kotlin {
3134
}
3235
}
3336
}
34-
35-
publishing {
36-
publications {
37-
create<MavenPublication>("maven") {
38-
from(components["kotlin"])
39-
}
40-
}
41-
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ package org.modelix.model.sync
22

33
import org.modelix.model.api.INode
44
import org.modelix.model.api.serialize
5-
import org.modelix.model.data.ModelData
65
import org.modelix.model.data.NodeData
76
import org.modelix.model.data.associateWithNotNull
8-
import java.io.File
97

10-
class ModelExporter(private val root: INode) {
11-
12-
fun export(outputFile: File) {
13-
val modelData = ModelData(root = root.asExported())
14-
outputFile.parentFile.mkdirs()
15-
outputFile.writeText(modelData.toJson())
16-
}
17-
}
8+
/**
9+
* A ModelExporter exports a node and its subtree in bulk.
10+
*/
11+
expect class ModelExporter(root: INode)
1812

13+
/**
14+
* Returns a [NodeData] representation of the receiver node as it would be exported by a [ModelExporter].
15+
* This function is recursively called on the node's children.
16+
*/
1917
fun INode.asExported() : NodeData {
2018
val idKey = NodeData.idPropertyKey
2119
return NodeData(

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@ 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
76

7+
/**
8+
* A ModelImporter updates an existing [INode] and its subtree based on a [ModelData] specification.
9+
*
10+
* The import is incremental.
11+
* Instead of simply overwriting the existing model, only a minimal amount of operations is used.
12+
*
13+
* Properties, references, and child links are synchronized for this node and all of its (in-)direct children.
14+
*
15+
* @param root the root node to be updated
16+
*/
817
class ModelImporter(private val root: INode) {
918

1019
private val originalIdToExisting: MutableMap<String, INode> = mutableMapOf()
1120
private val postponedReferences = ArrayList<() -> Unit>()
1221
private val nodesToRemove = HashSet<INode>()
1322

14-
fun import(jsonFile: File) {
15-
require(jsonFile.exists())
16-
require(jsonFile.extension == "json")
17-
18-
val data = ModelData.fromJson(jsonFile.readText())
19-
import(data)
20-
}
21-
23+
/**
24+
* Incrementally updates this importers root based on the provided [ModelData] specification.
25+
*
26+
* @param data the model specification
27+
*/
2228
fun import(data: ModelData) {
2329
originalIdToExisting.clear()
2430
postponedReferences.clear()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.modelix.model.sync
2+
3+
import org.modelix.model.api.INode
4+
5+
actual class ModelExporter actual constructor(private val root: INode) {
6+
7+
/**
8+
* Triggers a bulk export of this ModelExporter's root node and its (in-)direct children.
9+
*
10+
* @return exported node
11+
*/
12+
fun export() = root.asExported()
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.modelix.model.sync
2+
3+
import org.modelix.model.api.INode
4+
import org.modelix.model.data.ModelData
5+
import java.io.File
6+
7+
actual class ModelExporter actual constructor(private val root: INode) {
8+
9+
/**
10+
* Triggers a bulk export of this ModelExporter's root node and its (in-)direct children into the specified file.
11+
*
12+
* @param outputFile target file of the export
13+
*/
14+
fun export(outputFile: File) {
15+
val modelData = ModelData(root = root.asExported())
16+
outputFile.parentFile.mkdirs()
17+
outputFile.writeText(modelData.toJson())
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.modelix.model.sync
2+
3+
import org.modelix.model.data.ModelData
4+
import java.io.File
5+
6+
/**
7+
* Incrementally updates the root of the receiver [ModelImporter]
8+
* based on the [ModelData] specification contained in the given file.
9+
*
10+
* @param jsonFile json file containing the model specification
11+
*
12+
* @throws IllegalArgumentException if the file is not a json file or the file does not exist.
13+
*/
14+
fun ModelImporter.import(jsonFile: File) {
15+
require(jsonFile.exists())
16+
require(jsonFile.extension == "json")
17+
18+
val data = ModelData.fromJson(jsonFile.readText())
19+
import(data)
20+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)