This repository has been archived by the owner on May 16, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
264 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/com/github/pgutkowski/kgraphql/schema/execution/Merge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.github.pgutkowski.kgraphql.schema.execution | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
|
||
fun MutableMap<String, JsonNode?>.merge(key: String, node: JsonNode?): MutableMap<String, JsonNode?> { | ||
merge(key, node, this::get, this::set) | ||
return this | ||
} | ||
|
||
fun ObjectNode.merge(other: ObjectNode) { | ||
other.fields().forEach { | ||
merge(it.key, it.value) | ||
} | ||
} | ||
|
||
fun ObjectNode.merge(key: String, node: JsonNode?) { | ||
merge(key, node, this::get, this::set) | ||
} | ||
|
||
fun merge(key: String, node: JsonNode?, get: (String) -> JsonNode?, set: (String, JsonNode?) -> Any?) { | ||
val existingNode = get(key) | ||
if (existingNode != null) { | ||
when { | ||
node == null -> throw IllegalStateException("trying to merge null with non-null for $key") | ||
node is ObjectNode -> { | ||
check(existingNode is ObjectNode) { "trying to merge object with simple node for $key" } | ||
existingNode.merge(node) | ||
} | ||
existingNode is ObjectNode -> throw IllegalStateException("trying to merge simple node with object node for $key") | ||
node != existingNode -> throw IllegalStateException("trying to merge different simple nodes for $key") | ||
} | ||
} else { | ||
set(key, node) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/kotlin/com/github/pgutkowski/kgraphql/merge/MapMergeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.pgutkowski.kgraphql.merge | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory | ||
import com.github.pgutkowski.kgraphql.expect | ||
import com.github.pgutkowski.kgraphql.schema.execution.merge | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
|
||
class MapMergeTest { | ||
private val jsonNodeFactory = JsonNodeFactory.instance | ||
|
||
@Test | ||
fun `merge should add property`() { | ||
val existing = createMap("param1" to jsonNodeFactory.textNode("value1")) | ||
val update: JsonNode? = jsonNodeFactory.textNode("value2") | ||
|
||
existing.merge("param2", update) | ||
|
||
assertThat(existing.get("param2"), equalTo(update)) | ||
} | ||
|
||
@Test | ||
fun `merge should add nested property`() { | ||
val existing = createMap("param1" to jsonNodeFactory.textNode("value1")) | ||
val update: JsonNode? = jsonNodeFactory.objectNode().put("param2", "value2") | ||
|
||
existing.merge("sub", update) | ||
|
||
assertThat(existing.get("sub"), equalTo(update)) | ||
} | ||
|
||
@Test | ||
fun `merge should not change simple node`() { | ||
val existingValue: JsonNode? = jsonNodeFactory.textNode("value1") | ||
val existing = createMap("param" to existingValue) | ||
val update = jsonNodeFactory.textNode("value2") | ||
|
||
expect<IllegalStateException>("different simple nodes") { existing.merge("param", update) } | ||
|
||
assertThat(existing.get("param"), equalTo(existingValue)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge simple node with object node`() { | ||
val existingValue: JsonNode? = jsonNodeFactory.textNode("value1") | ||
val existing = createMap("param" to existingValue) | ||
val update = jsonNodeFactory.objectNode() | ||
|
||
expect<IllegalStateException>("merge object with simple node") { existing.merge("param", update) } | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value1") | ||
assertThat(existing.get("param"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge object node with simple node`() { | ||
val existingObj: JsonNode? = jsonNodeFactory.objectNode().put("other", "value1") | ||
val existing = createMap("param" to existingObj) | ||
val update = jsonNodeFactory.textNode("value2") | ||
|
||
expect<IllegalStateException>("merge simple node with object node") { existing.merge("param", update) } | ||
|
||
assertThat(existing.get("param"), equalTo(existingObj)) | ||
} | ||
|
||
private fun createMap(vararg pairs: Pair<String, JsonNode?>) = mutableMapOf(*pairs) | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/kotlin/com/github/pgutkowski/kgraphql/merge/ObjectNodeMergeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.github.pgutkowski.kgraphql.merge | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory | ||
import com.github.pgutkowski.kgraphql.expect | ||
import com.github.pgutkowski.kgraphql.schema.execution.merge | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
|
||
class ObjectNodeMergeTest { | ||
private val jsonNodeFactory = JsonNodeFactory.instance | ||
|
||
@Test | ||
fun `merge should add property`() { | ||
val existing = jsonNodeFactory.objectNode().put("param1", "value1") | ||
val update = jsonNodeFactory.objectNode().put("param2", "value2") | ||
|
||
existing.merge(update) | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value2") | ||
assertThat(existing.get("param2"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should add nested property`() { | ||
val existing = jsonNodeFactory.objectNode().put("param1", "value1") | ||
val update = jsonNodeFactory.objectNode() | ||
update.putObject("sub").put("param2", "value2") | ||
|
||
existing.merge(update) | ||
|
||
val expected: JsonNode? = jsonNodeFactory.objectNode().put("param2", "value2") | ||
assertThat(existing.get("sub"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not change simple node`() { | ||
val existing = jsonNodeFactory.objectNode().put("param", "value1") | ||
val update = jsonNodeFactory.objectNode().put("param", "value2") | ||
|
||
expect<IllegalStateException>("different simple nodes") { existing.merge(update) } | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value1") | ||
assertThat(existing.get("param"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge simple node with object node`() { | ||
val existing = jsonNodeFactory.objectNode().put("param", "value1") | ||
val update = jsonNodeFactory.objectNode() | ||
update.putObject("param") | ||
|
||
expect<IllegalStateException>("merge object with simple node") { existing.merge(update) } | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value1") | ||
assertThat(existing.get("param"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge object node with simple node`() { | ||
val existing = jsonNodeFactory.objectNode() | ||
val existingObj: JsonNode? = existing.putObject("param").put("other", "value1") | ||
val update = jsonNodeFactory.objectNode().put("param", "value2") | ||
|
||
expect<IllegalStateException>("merge simple node with object node") { existing.merge(update) } | ||
|
||
assertThat(existing.get("param"), equalTo(existingObj)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters