8000 fix: make YamlMap::get throws IncorrectTypeException with clear message by XYZboom · Pull Request #699 · charleskorn/kaml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: make YamlMap::get throws IncorrectTypeException with clear message #699

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
19 changes: 15 additions & 4 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,22 @@ public data class YamlMap(val entries: Map<YamlScalar, YamlNode>, override val p
override fun contentToString(): String =
"{" + entries.map { (key, value) -> "${key.contentToString()}: ${value.contentToString()}" }.joinToString(", ") + "}"

@Suppress("UNCHECKED_CAST")
public operator fun <T : YamlNode> get(key: String): T? =
entries.entries
/**
* Returns the value corresponding to the given key and the given type,
* or null if such a key is not present in the map,
* or throws [IncorrectTypeException] if the value is not the given type.
*/
public inline operator fun <reified T : YamlNode> get(key: String): T? {
val node = entries.entries
.firstOrNull { it.key.content == key }
?.value as T?
?.value ?: return null // no such key in the map
// if the value is not the given type,
// throws IncorrectTypeException with a clear message instead of ClassCastException.
return node as? T ?: throw IncorrectTypeException(
"Expected element to be ${T::class.simpleName} but is ${node::class.simpleName}",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the class names, could you please use the plain English names instead (eg. instead of "YamlList", use "list", instead of "YamlMap", use "map" etc.)

Copy link
Author
@XYZboom XYZboom May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that class name is used here(https://github.com/charleskorn/kaml/blob/main/src/commonMain/kotlin/com/charleskorn/kaml/YamlNode.kt#L304), so I just want to keep it consistent with here. Do we need to make some changes here at YamlNode.kt:Line 304?

node.path
)
}

public fun getScalar(key: String): YamlScalar? = when (val node = get<YamlNode>(key)) {
null -> null
Expand Down
12 changes: 12 additions & 0 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlMapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ class YamlMapTest : FlatFunSpec({
test("returns the value for that key") {
map.get<YamlScalar>("hello") shouldBe YamlScalar("world", helloValuePath)
}
test("throws IncorrectTypeException with clear message when the type mismatches") {
val exception = shouldThrow<IncorrectTypeException> {
map.get<YamlList>("hello")
}

exception.asClue {
it.message shouldBe "Expected element to be YamlList but is YamlScalar"
it.line shouldBe 2
it.column shouldBe 1
it.path shouldBe helloValuePath
}
}
}
}

Expand Down
Loading
0