1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

fixes in ChatIdentifierSerializer

This commit is contained in:
InsanusMokrassar 2020-03-22 15:53:37 +06:00
parent 89881a7349
commit c5c8a743e6
3 changed files with 17 additions and 14 deletions

View File

@ -17,6 +17,8 @@
`keyboard`
* `makeLinkToMessage` now is not `PreviewFeature`
* All deprecations was removed
* `RequestException` now extends `io.ktor.utils.io.errors.IOException` instead of `kotlinx.io.IOException`
* `Any#toJson` now is NOT `inline`
## 0.24.0

View File

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import kotlinx.serialization.*
import kotlinx.serialization.json.JsonPrimitiveSerializer
@Serializable(ChatIdentifierSerializer::class)
sealed class ChatIdentifier
@ -39,13 +40,15 @@ fun String.toUsername(): Username = Username(this)
@Serializer(ChatIdentifier::class)
internal object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
override fun deserialize(decoder: Decoder): ChatIdentifier {
val id = decoder.decodeString()
return id.toLongOrNull() ?.let {
val id = JsonPrimitiveSerializer.deserialize(decoder)
return id.longOrNull ?.let {
ChatId(it)
} ?: if (!id.startsWith("@")) {
Username("@$id")
} ?: id.content.let {
if (!it.startsWith("@")) {
Username("@$it")
} else {
Username(id)
Username(it)
}
}
}

View File

@ -1,10 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.github.insanusmokrassar.TelegramBotAPI.NonstrictTestsJsonFormat
import com.github.insanusmokrassar.TelegramBotAPI.TestsJsonFormat
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.*
@ImplicitReflectionSerializer
@ -56,25 +54,25 @@ class ChatIdentifierTests {
)
Example(chatIdentifierChatId.toChatId()).let { withChatId ->
val stringified = NonstrictTestsJsonFormat.stringify(Example.serializer(), withChatId)
val stringified = TestsJsonFormat.stringify(Example.serializer(), withChatId)
assertEquals(stringified, "{\"identifier\":$chatIdentifierChatId}")
val deserialized = NonstrictTestsJsonFormat.parse(Example.serializer(), stringified)
val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified)
assertEquals(withChatId, deserialized)
}
Example(testUsername.toUsername()).let { withUsername ->
val stringified = NonstrictTestsJsonFormat.stringify(Example.serializer(), withUsername)
val stringified = TestsJsonFormat.stringify(Example.serializer(), withUsername)
assertEquals(stringified, "{\"identifier\":\"$testUsername\"}")
val deserialized = NonstrictTestsJsonFormat.parse(Example.serializer(), stringified)
val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified)
assertEquals(withUsername, deserialized)
}
// Replace @ by empty string, because from time to time we can retrieve from Telegram system
// username without starting @ symbol
Example(testUsername.toUsername()).let { withUsername ->
val stringified = NonstrictTestsJsonFormat.stringify(Example.serializer(), withUsername).replace("@", "")
val stringified = TestsJsonFormat.stringify(Example.serializer(), withUsername).replace("@", "")
assertEquals("{\"identifier\":\"${testUsername.replace("@", "")}\"}", stringified)
val deserialized = NonstrictTestsJsonFormat.parse(Example.serializer(), stringified)
val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified)
assertEquals(withUsername, deserialized)
}
}