1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +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` `keyboard`
* `makeLinkToMessage` now is not `PreviewFeature` * `makeLinkToMessage` now is not `PreviewFeature`
* All deprecations was removed * 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 ## 0.24.0

View File

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

View File

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