1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

fix warnings

This commit is contained in:
InsanusMokrassar 2019-02-21 14:21:33 +08:00
parent 2974a33e51
commit 313068543f
10 changed files with 55 additions and 53 deletions

View File

@ -16,7 +16,7 @@ import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.util.cio.toByteArray import io.ktor.util.cio.toByteArray
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.io.charsets.Charset import kotlinx.io.charsets.Charset
import kotlinx.serialization.json.JSON import kotlinx.serialization.json.Json
class KtorRequestsExecutor( class KtorRequestsExecutor(
token: String, token: String,
@ -25,7 +25,7 @@ class KtorRequestsExecutor(
callsFactories: List<KtorCallFactory> = emptyList(), callsFactories: List<KtorCallFactory> = emptyList(),
excludeDefaultFactories: Boolean = false, excludeDefaultFactories: Boolean = false,
private val requestsLimiter: RequestLimiter = EmptyLimiter, private val requestsLimiter: RequestLimiter = EmptyLimiter,
private val jsonFormatter: JSON = JSON.nonstrict private val jsonFormatter: Json = Json.nonstrict
) : BaseRequestsExecutor(token, hostUrl) { ) : BaseRequestsExecutor(token, hostUrl) {
constructor( constructor(
token: String, token: String,

View File

@ -23,8 +23,8 @@ fun String.toInputFile(): InputFile = FileId(this)
@Serializer(FileId::class) @Serializer(FileId::class)
object FileIdSerializer : KSerializer<FileId> { object FileIdSerializer : KSerializer<FileId> {
override fun serialize(output: Encoder, obj: FileId) = output.encodeString(obj.fileId) override fun serialize(encoder: Encoder, obj: FileId) = encoder.encodeString(obj.fileId)
override fun deserialize(input: Decoder): FileId = FileId(input.decodeString()) override fun deserialize(decoder: Decoder): FileId = FileId(decoder.decodeString())
} }
// TODO:: add checks for files size // TODO:: add checks for files size

View File

@ -4,10 +4,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ChatRequ
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJson
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.internal.BooleanSerializer import kotlinx.serialization.internal.BooleanSerializer
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonTreeMapper
@Serializable @Serializable
data class SetChatPhoto ( data class SetChatPhoto (
@ -19,5 +19,5 @@ data class SetChatPhoto (
override fun method(): String = "setChatPhoto" override fun method(): String = "setChatPhoto"
override fun resultSerializer(): KSerializer<Boolean> = BooleanSerializer override fun resultSerializer(): KSerializer<Boolean> = BooleanSerializer
override val mediaMap: Map<String, MultipartFile> = mapOf(photoField to photo) override val mediaMap: Map<String, MultipartFile> = mapOf(photoField to photo)
override val paramsJson: JsonObject = JsonTreeMapper().writeTree(this, serializer()).jsonObject override val paramsJson: JsonObject = this.toJson(serializer())
} }

View File

@ -32,8 +32,8 @@ fun String.asUsername(): Username = Username(this)
@Serializer(ChatIdentifier::class) @Serializer(ChatIdentifier::class)
internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> { internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {
override fun deserialize(input: Decoder): ChatIdentifier { override fun deserialize(decoder: Decoder): ChatIdentifier {
val id = input.decodeString() val id = decoder.decodeString()
return id.toLongOrNull() ?.let { return id.toLongOrNull() ?.let {
ChatId(it) ChatId(it)
} ?: if (!id.startsWith("@")) { } ?: if (!id.startsWith("@")) {
@ -43,10 +43,10 @@ internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {
} }
} }
override fun serialize(output: Encoder, obj: ChatIdentifier) { override fun serialize(encoder: Encoder, obj: ChatIdentifier) {
when (obj) { when (obj) {
is ChatId -> output.encodeString(obj.chatId.toString()) is ChatId -> encoder.encodeString(obj.chatId.toString())
is Username -> output.encodeString(obj.username) is Username -> encoder.encodeString(obj.username)
} }
} }
} }

View File

@ -8,32 +8,32 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException
@Serializer(InlineQueryResult::class) @Serializer(InlineQueryResult::class)
object InlineQueryResultSerializer : object InlineQueryResultSerializer :
KSerializer<InlineQueryResult> { KSerializer<InlineQueryResult> {
override fun serialize(output: Encoder, obj: InlineQueryResult) { override fun serialize(encoder: Encoder, obj: InlineQueryResult) {
when(obj) { when(obj) {
is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(output, obj) is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(encoder, obj)
is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(output, obj) is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(output, obj) is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(encoder, obj)
is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(output, obj) is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(encoder, obj)
is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(output, obj) is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(output, obj) is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(encoder, obj)
is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(output, obj) is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(encoder, obj)
is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(output, obj) is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(output, obj) is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(encoder, obj)
is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(output, obj) is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(encoder, obj)
is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(output, obj) is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(output, obj) is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(encoder, obj)
is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(output, obj) is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(output, obj) is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(encoder, obj)
is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(output, obj) is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(encoder, obj)
is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(output, obj) is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(encoder, obj)
is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(output, obj) is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(output, obj) is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(encoder, obj)
is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(output, obj) is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(output, obj) is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(encoder, obj)
} }
} }
override fun deserialize(input: Decoder): InlineQueryResult { override fun deserialize(decoder: Decoder): InlineQueryResult {
throw NotImplementedException() throw NotImplementedException()
} }
} }

View File

@ -25,8 +25,8 @@ object HTMLParseMode : ParseMode() {
@Serializer(ParseMode::class) @Serializer(ParseMode::class)
internal class ParseModeSerializerObject: KSerializer<ParseMode> { internal class ParseModeSerializerObject: KSerializer<ParseMode> {
override fun deserialize(input: Decoder): ParseMode { override fun deserialize(decoder: Decoder): ParseMode {
val mode = input.decodeString() val mode = decoder.decodeString()
return when (mode) { return when (mode) {
MarkdownParseMode.parseModeName -> MarkdownParseMode MarkdownParseMode.parseModeName -> MarkdownParseMode
HTMLParseMode.parseModeName -> HTMLParseMode HTMLParseMode.parseModeName -> HTMLParseMode
@ -34,7 +34,7 @@ internal class ParseModeSerializerObject: KSerializer<ParseMode> {
} }
} }
override fun serialize(output: Encoder, obj: ParseMode) { override fun serialize(encoder: Encoder, obj: ParseMode) {
output.encodeString(obj.parseModeName) encoder.encodeString(obj.parseModeName)
} }
} }

View File

@ -22,15 +22,15 @@ fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this)
@Serializer(TelegramDate::class) @Serializer(TelegramDate::class)
internal class TelegramDateSerializer: KSerializer<TelegramDate> { internal class TelegramDateSerializer: KSerializer<TelegramDate> {
override fun serialize(output: Encoder, obj: TelegramDate) { override fun serialize(encoder: Encoder, obj: TelegramDate) {
output.encodeLong( encoder.encodeLong(
TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis) TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis)
) )
} }
override fun deserialize(input: Decoder): TelegramDate { override fun deserialize(decoder: Decoder): TelegramDate {
return TelegramDate( return TelegramDate(
input.decodeLong() decoder.decodeLong()
) )
} }
} }

View File

@ -20,12 +20,12 @@ sealed class BotAction {
@Serializer(BotAction::class) @Serializer(BotAction::class)
class BotActionSerializer: KSerializer<BotAction> { class BotActionSerializer: KSerializer<BotAction> {
override fun serialize(output: Encoder, obj: BotAction) { override fun serialize(encoder: Encoder, obj: BotAction) {
output.encodeString(obj.actionName) encoder.encodeString(obj.actionName)
} }
override fun deserialize(input: Decoder): BotAction { override fun deserialize(decoder: Decoder): BotAction {
val actionName = input.decodeString() val actionName = decoder.decodeString()
return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName") return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName")
} }
} }

View File

@ -9,12 +9,12 @@ interface Currencied {
@Serializer(Currency::class) @Serializer(Currency::class)
object CurrencySerializer : KSerializer<Currency> { object CurrencySerializer : KSerializer<Currency> {
override fun serialize(output: Encoder, obj: Currency) { override fun serialize(encoder: Encoder, obj: Currency) {
output.encodeString(obj.currencyCode) encoder.encodeString(obj.currencyCode)
} }
override fun deserialize(input: Decoder): Currency { override fun deserialize(decoder: Decoder): Currency {
return input.decodeString().let { return decoder.decodeString().let {
Currency.getInstance(it) Currency.getInstance(it)
} }
} }

View File

@ -4,13 +4,15 @@ import kotlinx.serialization.*
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
inline fun <reified T: Any> T.toJsonWithoutNulls(): JsonObject = toJson(serializerByValue(this)).withoutNulls() inline fun <reified T: Any> T.toJsonWithoutNulls(): JsonObject = Json.nonstrict.toJson(
this
).jsonObject.withoutNulls()
inline fun <reified T: Any> T.toJsonWithoutNulls(serializer: KSerializer<T>): JsonObject = toJson(serializer).withoutNulls() inline fun <reified T: Any> T.toJsonWithoutNulls(serializer: KSerializer<T>): JsonObject = toJson(serializer).withoutNulls()
inline fun <reified T: Any> T.toJson(serializer: KSerializer<T>): JsonObject = JsonTreeMapper().writeTree( inline fun <reified T: Any> T.toJson(serializer: KSerializer<T>): JsonObject = Json.nonstrict.toJson(
this, serializer,
serializer this
).jsonObject ).jsonObject
fun JsonArray.withoutNulls(): JsonArray { fun JsonArray.withoutNulls(): JsonArray {