Skip to content

Commit

Permalink
Add getSelectorFullPath to KspoonDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
burnoo committed Sep 4, 2024
1 parent 09e229c commit 3337d15
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions kspoon/src/commonMain/kotlin/decoder/HtmlTreeDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,6 @@ internal class HtmlTreeDecoder(

override fun decodeDocument() = elements.firstOrNull() as? Document
?: kspoonError("Current Element is not a Document. Document type works only on root")

override fun getSelectorFullPath(): String = getSelectorFullPath(tag = currentTagOrNull)
}
2 changes: 2 additions & 0 deletions kspoon/src/commonMain/kotlin/decoder/KspoonDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ interface KspoonDecoder {
fun decodeElements(): Elements

fun decodeDocument(): Document

fun getSelectorFullPath(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.burnoo.ksoup.exception

class KspoonParseException(
override val message: String?,
override val cause: Throwable?,
override val cause: Throwable? = null,
) : IllegalStateException()

internal fun kspoonError(message: String?, cause: Throwable? = null): Nothing {
Expand Down
22 changes: 20 additions & 2 deletions kspoon/src/commonTest/kotlin/KspoonSerializersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dev.burnoo.ksoup

import dev.burnoo.ksoup.annotation.Selector
import dev.burnoo.ksoup.decoder.KspoonDecoder
import dev.burnoo.ksoup.exception.KspoonParseException
import io.kotest.assertions.throwables.shouldThrowWithMessage
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Contextual
import kotlinx.serialization.KSerializer
Expand Down Expand Up @@ -36,8 +38,10 @@ object TagSerializer : KSerializer<String> {
PrimitiveSerialDescriptor("dev.burnoo.ksoup.type.CustomString", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): String {
val element = (decoder as KspoonDecoder).decodeElement()
val tag = element?.tag()?.name ?: "tag not found"
val kspoonDecoder = decoder as KspoonDecoder
val element = decoder.decodeElement()
val tag = element?.tag()?.name
?: throw KspoonParseException("Could not get tag name for selector: ${kspoonDecoder.getSelectorFullPath()}")
return tag
}

Expand Down Expand Up @@ -80,4 +84,18 @@ class SerializersModuleTest {

model shouldBe Model("p")
}

@Test
fun shouldThrowWithFullPathTagMessage() {
@Serializable
data class Model(
@Selector(".class1")
@Serializable(TagSerializer::class)
val tag: String,
)

shouldThrowWithMessage<KspoonParseException>(message = "Could not get tag name for selector: ['.class1']") {
Kspoon.parse<Model>("")
}
}
}

0 comments on commit 3337d15

Please sign in to comment.