-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Port from snakeyaml-engine] Allow to avoid dumping anchors
Co-authored-by: Andrey Somov <[email protected]>
- Loading branch information
Showing
11 changed files
with
242 additions
and
3 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
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
29 changes: 29 additions & 0 deletions
29
src/commonMain/kotlin/it/krzeminski/snakeyaml/engine/kmp/serializer/IdentitySet.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,29 @@ | ||
package it.krzeminski.snakeyaml.engine.kmp.serializer | ||
|
||
import it.krzeminski.snakeyaml.engine.kmp.internal.IdentityHashCode | ||
import it.krzeminski.snakeyaml.engine.kmp.internal.identityHashCode | ||
|
||
/** | ||
* A set that compares objects by their identities, not values. | ||
* It's an attempt to reimplement `Collections.newSetFromMap(new IdentityHashMap<Node, Boolean>())` | ||
* from the JVM. | ||
*/ | ||
internal class IdentitySet<T> { | ||
private val contents: MutableSet<IdentityHashCode> = mutableSetOf() | ||
|
||
fun add(obj: T) { | ||
contents.add(identityHashCode(obj)) | ||
} | ||
|
||
fun contains(obj: T): Boolean { | ||
return contents.contains(identityHashCode(obj)) | ||
} | ||
|
||
fun clear() { | ||
contents.clear() | ||
} | ||
|
||
fun remove(obj: T) { | ||
contents.remove(identityHashCode(obj)) | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/commonTest/kotlin/it/krzeminski/snakeyaml/engine/kmp/serializer/IdentitySetTest.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,54 @@ | ||
package it.krzeminski.snakeyaml.engine.kmp.serializer | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
data class TestClass(val value: String) | ||
|
||
class IdentitySetTest : FunSpec({ | ||
val objectFoo1 = TestClass(value = "foo") | ||
val objectFoo2 = TestClass(value = "foo") | ||
val objectBar = TestClass(value = "bar") | ||
|
||
test("expecting existence of the same object wrt. identity") { | ||
val identitySet = IdentitySet<TestClass>() | ||
identitySet.add(objectFoo1) | ||
identitySet.contains(objectFoo1) shouldBe true | ||
} | ||
|
||
test("comparing two objects that are equal according to 'equals'") { | ||
val identitySet = IdentitySet<TestClass>() | ||
identitySet.add(objectFoo1) | ||
identitySet.contains(objectFoo2) shouldBe false | ||
} | ||
|
||
test("comparing two objects that are not equal according to 'equals'") { | ||
val identitySet = IdentitySet<TestClass>() | ||
identitySet.add(objectFoo1) | ||
identitySet.contains(objectBar) shouldBe false | ||
} | ||
|
||
test("clearing the set") { | ||
val identitySet = IdentitySet<TestClass>() | ||
identitySet.add(objectFoo1) | ||
identitySet.contains(objectFoo1) shouldBe true | ||
identitySet.clear() | ||
identitySet.contains(objectFoo1) shouldBe false | ||
} | ||
|
||
test("removing the same object") { | ||
val identitySet = IdentitySet<TestClass>() | ||
identitySet.add(objectFoo1) | ||
identitySet.contains(objectFoo1) shouldBe true | ||
identitySet.remove(objectFoo1) | ||
identitySet.contains(objectFoo1) shouldBe false | ||
} | ||
|
||
test("removing object that is equal according to 'equals'") { | ||
val identitySet = IdentitySet<TestClass>() | ||
identitySet.add(objectFoo1) | ||
identitySet.contains(objectFoo1) shouldBe true | ||
identitySet.remove(objectFoo2) | ||
identitySet.contains(objectFoo1) shouldBe true | ||
} | ||
}) |
47 changes: 47 additions & 0 deletions
47
...est/java/it/krzeminski/snakeyaml/engine/kmp/usecases/references/DereferenceAliasesTest.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,47 @@ | ||
package it.krzeminski.snakeyaml.engine.kmp.usecases.references | ||
|
||
import it.krzeminski.snakeyaml.engine.kmp.api.Dump | ||
import it.krzeminski.snakeyaml.engine.kmp.api.DumpSettings | ||
import it.krzeminski.snakeyaml.engine.kmp.api.Load | ||
import it.krzeminski.snakeyaml.engine.kmp.api.LoadSettings | ||
import it.krzeminski.snakeyaml.engine.kmp.common.FlowStyle | ||
import it.krzeminski.snakeyaml.engine.kmp.exceptions.YamlEngineException | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.fail | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
import org.snakeyaml.engine.v2.utils.TestUtils | ||
import java.io.StringWriter | ||
|
||
@Tag("fast") | ||
class DereferenceAliasesTest { | ||
@Test | ||
fun testNoAliases() { | ||
val settings = LoadSettings.builder().build() | ||
val load = Load(settings) | ||
val map = load.loadFromString(TestUtils.getResource("/issues/issue1086-1-input.yaml")) as Map<*, *>? | ||
val setting = DumpSettings.builder().setDefaultFlowStyle(FlowStyle.BLOCK) | ||
.setDereferenceAliases(true).build() | ||
val dump = Dump(setting) | ||
val node = dump.dumpToString(map) | ||
val out = StringWriter() | ||
val expected = TestUtils.getResource("/issues/issue1086-1-expected.yaml") | ||
assertEquals(expected, node) | ||
} | ||
|
||
@Test | ||
fun testNoAliasesRecursive() { | ||
val settings = LoadSettings.builder().build() | ||
val load = Load(settings) | ||
val map = load.loadFromString(TestUtils.getResource("/issues/issue1086-2-input.yaml")) as Map<*, *>? | ||
val setting = DumpSettings.builder().setDefaultFlowStyle(FlowStyle.BLOCK) | ||
.setDereferenceAliases(true).build() | ||
val dump = Dump(setting) | ||
try { | ||
dump.dumpToString(map) | ||
fail() | ||
} catch (e: YamlEngineException) { | ||
assertEquals("Cannot dereference aliases for recursive structures.", e.message) | ||
} | ||
} | ||
} |
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,21 @@ | ||
defines: | ||
serverPattern1: | ||
type: t3 | ||
strage: 500 | ||
serverPattern2: | ||
type: t2 | ||
strage: 250 | ||
lbPattern1: | ||
name: lbForPublic | ||
vpc: vpc1 | ||
lbPattern2: | ||
name: lbForInternal | ||
vpc: vpc1 | ||
current: | ||
assenbled1: | ||
server: | ||
type: t3 | ||
strage: 500 | ||
lb: | ||
name: lbForPublic | ||
vpc: vpc1 |
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,17 @@ | ||
defines: | ||
serverPattern1: &server_HighPerformance | ||
type: t3 | ||
strage: 500 | ||
serverPattern2: &server_LowPerformance | ||
type: t2 | ||
strage: 250 | ||
lbPattern1: &lb_Public | ||
name: lbForPublic | ||
vpc: vpc1 | ||
lbPattern2: &lb_Internal | ||
name: lbForInternal | ||
vpc: vpc1 | ||
current: | ||
assenbled1: | ||
server: *server_HighPerformance | ||
lb: *lb_Public |
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,34 @@ | ||
&id002 | ||
bankAccountOwner: &id001 | ||
bankAccountOwner: *id001 | ||
birthPlace: Leningrad | ||
birthday: 1970-01-12T13:46:40Z | ||
children: &id003 | ||
- *id002 | ||
- bankAccountOwner: *id001 | ||
birthPlace: New York | ||
birthday: 1983-04-24T02:40:00Z | ||
children: [] | ||
father: *id001 | ||
mother: &id004 | ||
bankAccountOwner: *id001 | ||
birthPlace: Saint-Petersburg | ||
birthday: 1973-03-03T09:46:40Z | ||
children: *id003 | ||
father: null | ||
mother: null | ||
name: Mother | ||
partner: *id001 | ||
name: Daughter | ||
partner: null | ||
father: null | ||
mother: null | ||
name: Father | ||
partner: *id004 | ||
birthPlace: Munich | ||
birthday: 1979-10-28T23:06:40Z | ||
children: [] | ||
father: *id001 | ||
mother: *id004 | ||
name: Son | ||
partner: null |
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 |
---|---|---|
@@ -1 +1 @@ | ||
3d97ee77ff70ab1093a63e4d3dadda9e885fe857 | ||
1af7dcd2c9c64a9d7cd9de3cd5f64bb2fc2aab6a |