-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Port from snakeyaml-engine] Allow to avoid dumping anchors #272
Draft
krzema12
wants to merge
1
commit into
main
Choose a base branch
from
port-dereference-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−3
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests fail for Wasm target with a mysterious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://youtrack.jetbrains.com/issue/KT-68185
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Asked for help: https://kotlinlang.slack.com/archives/CDFP59223/p1731593368886219
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for a failing test:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turned out that there's a bug here:
snakeyaml-engine-kmp/src/wasmJsMain/kotlin/it/krzeminski/snakeyaml/engine/kmp/internal/system.wasm.kt
Line 17 in bd8ffe0
WeakMap
has ahas
method instead ofcontains
. But even if I fix it, I'm getting further issues withset
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CC @aSemy in case you have any useful insight there. I see you contributed this part, and it looks like it's being exercised in the tests only now (which is weird since I thought BaseRepresenter is covered fairly well). Do you remember how you tackled implementing such internals in Wasm? Any good reference impls you know? I see korlibs still doesn't have it implemented: https://github.com/korlibs/korlibs-datastructure/blob/b3f589fe2ae7e90b878fce3f586e1f8caba07baf/korlibs-datastructure/src%40wasm/korlibs/datastructure/internal/InternalJs.kt#L25
Ideally we reproduce any issues related to identityHashCode with a new test on the main branch, and fix it there, then this PR should just work. I'll try to handle it, but I'm wondering of you have some useful thoughts/hints here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://kotlinlang.slack.com/archives/CDFP59223/p1716543360983169
maybe rename
contains
tohas
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this helps with the first problem, but it uncovers another one. I'll describe this next problem better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracting this problem and fixing it to a separate PR: #273