From 4933bf80935bfaf293b428ff733627be5b644546 Mon Sep 17 00:00:00 2001 From: BartArys Date: Mon, 23 Nov 2020 15:07:52 +0100 Subject: [PATCH] Fix type coercion in cache queries --- .../kordlib/core/behavior/GuildBehavior.kt | 5 +++-- .../kordlib/core/behavior/MemberBehavior.kt | 9 +++++---- .../behavior/channel/VoiceChannelBehavior.kt | 3 ++- .../com/gitlab/kordlib/core/cache/Query.kt | 16 +++++++++++++++- .../core/gateway/handler/GuildEventHandler.kt | 8 ++++---- .../core/gateway/handler/UserEventHandler.kt | 3 ++- .../core/gateway/handler/VoiceEventHandler.kt | 3 ++- .../kordlib/core/supplier/CacheEntitySupplier.kt | 5 +++-- 8 files changed, 36 insertions(+), 16 deletions(-) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt index e5bf94d6267..734b1fb3f9a 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt @@ -7,6 +7,7 @@ import com.gitlab.kordlib.common.entity.optional.Optional import com.gitlab.kordlib.common.exception.RequestException import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.cache.data.* +import com.gitlab.kordlib.core.cache.idEq import com.gitlab.kordlib.core.catchDiscordError import com.gitlab.kordlib.core.entity.* import com.gitlab.kordlib.core.entity.channel.* @@ -100,7 +101,7 @@ interface GuildBehavior : Entity, Strategizable { * This property is not resolvable through REST and will always use [KordCache] instead. */ val presences: Flow - get() = kord.cache.query { PresenceData::guildId eq id.value } + get() = kord.cache.query { idEq(PresenceData::guildId, id) } .asFlow() .map { Presence(it, kord) } @@ -149,7 +150,7 @@ interface GuildBehavior : Entity, Strategizable { */ val voiceStates: Flow get() = kord.cache - .query { VoiceStateData::guildId eq id.value } + .query { idEq( VoiceStateData::guildId, id) } .asFlow() .map { VoiceState(it, kord) } diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MemberBehavior.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MemberBehavior.kt index 092e2492121..d8843578f9e 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MemberBehavior.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MemberBehavior.kt @@ -6,6 +6,7 @@ import com.gitlab.kordlib.common.exception.RequestException import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.cache.data.PresenceData import com.gitlab.kordlib.core.cache.data.VoiceStateData +import com.gitlab.kordlib.core.cache.idEq import com.gitlab.kordlib.core.entity.* import com.gitlab.kordlib.core.exception.EntityNotFoundException import com.gitlab.kordlib.core.supplier.EntitySupplier @@ -139,8 +140,8 @@ interface MemberBehavior : Entity, UserBehavior { */ suspend fun getPresenceOrNull(): Presence? { val data = kord.cache.query { - PresenceData::userId eq id.value - PresenceData::guildId eq guildId.value + idEq(PresenceData::userId, id) + idEq(PresenceData::guildId, guildId) }.singleOrNull() ?: return null return Presence(data, kord) @@ -170,8 +171,8 @@ interface MemberBehavior : Entity, UserBehavior { */ suspend fun getVoiceStateOrNull(): VoiceState? { val data = kord.cache.query { - VoiceStateData::userId eq id.value - VoiceStateData::guildId eq guildId.value + idEq(VoiceStateData::userId, id) + idEq(VoiceStateData::guildId, guildId) }.singleOrNull() ?: return null return VoiceState(data, kord) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/VoiceChannelBehavior.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/VoiceChannelBehavior.kt index 98933fb94bd..19f3f36be1d 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/VoiceChannelBehavior.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/VoiceChannelBehavior.kt @@ -7,6 +7,7 @@ import com.gitlab.kordlib.common.exception.RequestException import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.cache.data.ChannelData import com.gitlab.kordlib.core.cache.data.VoiceStateData +import com.gitlab.kordlib.core.cache.idEq import com.gitlab.kordlib.core.entity.VoiceState import com.gitlab.kordlib.core.entity.channel.Channel import com.gitlab.kordlib.core.entity.channel.VoiceChannel @@ -37,7 +38,7 @@ interface VoiceChannelBehavior : GuildChannelBehavior { * [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead. */ val voiceStates: Flow - get() = kord.cache.query { VoiceStateData::channelId eq id.value } + get() = kord.cache.query { idEq(VoiceStateData::channelId, id) } .asFlow() .map { VoiceState(it, kord) } diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/cache/Query.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/cache/Query.kt index 6b1b9f47c33..071a51b5e24 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/cache/Query.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/cache/Query.kt @@ -7,7 +7,7 @@ import com.gitlab.kordlib.common.entity.optional.OptionalSnowflake import com.gitlab.kordlib.common.entity.optional.optionalSnowflake import kotlin.reflect.KProperty1 -fun QueryBuilder.idEq(property: KProperty1, value: Snowflake?) { +fun QueryBuilder.idEq(property: KProperty1, value: Snowflake?) { property.eq(value) } @@ -19,3 +19,17 @@ fun QueryBuilder.idEq(property: KProperty1, v fun QueryBuilder.idEq(property: KProperty1>, value: String?) { property.eq(Optional(value)) } + +fun QueryBuilder.idGt(property: KProperty1, value: Snowflake) { + property.gt(value) +} + +@JvmName("stringEq") +fun QueryBuilder.idEq(property: KProperty1, value: String?) { + property.eq(value) +} + +@JvmName("booleanEq") +fun QueryBuilder.idEq(property: KProperty1, value: Boolean?) { + property.eq(value) +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/GuildEventHandler.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/GuildEventHandler.kt index 26d3de3f292..8707da4ff15 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/GuildEventHandler.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/GuildEventHandler.kt @@ -94,7 +94,7 @@ internal class GuildEventHandler( } private suspend fun handle(event: GuildDelete, shard: Int) = with(event.guild) { - val query = cache.query { GuildData::id eq id } + val query = cache.query { idEq(GuildData::id, id) } val old = query.asFlow().map { Guild(it, kord) }.singleOrNull() query.remove() @@ -124,7 +124,7 @@ internal class GuildEventHandler( val emojis = data.map { GuildEmoji(it, kord) } - cache.query { GuildData::id eq guildId.value }.update { + cache.query { idEq(GuildData::id, guildId) }.update { it.copy(emojis = emojis.map { emoji -> emoji.id }) } @@ -150,7 +150,7 @@ internal class GuildEventHandler( private suspend fun handle(event: GuildMemberRemove, shard: Int) = with(event.member) { val userData = UserData.from(user) - cache.query { UserData::id eq userData.id }.remove() + cache.query { idEq(UserData::id, userData.id) }.remove() val user = User(userData, kord) coreFlow.emit(MemberLeaveEvent(user, guildId, shard)) @@ -216,7 +216,7 @@ internal class GuildEventHandler( private suspend fun handle(event: PresenceUpdate, shard: Int) = with(event.presence) { val data = PresenceData.from(this.guildId.value!!, this) - val old = cache.query { PresenceData::id eq data.id } + val old = cache.query { idEq(PresenceData::id, data.id) } .asFlow().map { Presence(it, kord) }.singleOrNull() cache.put(data) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/UserEventHandler.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/UserEventHandler.kt index ae7b8cca8be..29d5153faa9 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/UserEventHandler.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/UserEventHandler.kt @@ -5,6 +5,7 @@ import com.gitlab.kordlib.cache.api.put import com.gitlab.kordlib.cache.api.query import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.cache.data.UserData +import com.gitlab.kordlib.core.cache.idEq import com.gitlab.kordlib.core.entity.User import com.gitlab.kordlib.core.event.user.UserUpdateEvent import com.gitlab.kordlib.core.gateway.MasterGateway @@ -31,7 +32,7 @@ internal class UserEventHandler( private suspend fun handle(event: UserUpdate, shard: Int) { val data = UserData.from(event.user) - val old = cache.query { UserData::id eq data.id } + val old = cache.query { idEq(UserData::id, data.id) } .asFlow().map { User(it, kord) }.singleOrNull() cache.put(data) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/VoiceEventHandler.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/VoiceEventHandler.kt index c5f4cc09889..46f94e0aa35 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/VoiceEventHandler.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/gateway/handler/VoiceEventHandler.kt @@ -6,6 +6,7 @@ import com.gitlab.kordlib.cache.api.query import com.gitlab.kordlib.common.entity.optional.optional import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.cache.data.* +import com.gitlab.kordlib.core.cache.idEq import com.gitlab.kordlib.core.entity.VoiceState import com.gitlab.kordlib.core.event.guild.VoiceServerUpdateEvent import com.gitlab.kordlib.core.event.user.VoiceStateUpdateEvent @@ -35,7 +36,7 @@ internal class VoiceEventHandler( private suspend fun handle(event: VoiceStateUpdate, shard: Int) { val data = VoiceStateData.from(event.voiceState.guildId.value!!, event.voiceState) - val old = cache.query { VoiceStateData::id eq data.id } + val old = cache.query { idEq(VoiceStateData::id, data.id) } .asFlow().map { VoiceState(it, kord) }.singleOrNull() cache.put(data) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/CacheEntitySupplier.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/CacheEntitySupplier.kt index 7d19a5bf336..613e4b62e26 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/CacheEntitySupplier.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/CacheEntitySupplier.kt @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.any import com.gitlab.kordlib.core.cache.data.* import com.gitlab.kordlib.core.cache.idEq +import com.gitlab.kordlib.core.cache.idGt import com.gitlab.kordlib.core.entity.* import com.gitlab.kordlib.core.entity.channel.Channel import com.gitlab.kordlib.core.entity.channel.GuildChannel @@ -114,7 +115,7 @@ class CacheEntitySupplier(private val kord: Kord) : EntitySupplier { override fun getChannelPins(channelId: Snowflake): Flow = cache.query { idEq(MessageData::channelId, channelId) - MessageData::pinned eq true + idEq(MessageData::pinned, true) }.asFlow().map { Message(it, kord) } override suspend fun getGuildOrNull(id: Snowflake): Guild? { @@ -152,7 +153,7 @@ class CacheEntitySupplier(private val kord: Kord) : EntitySupplier { require(limit > 0) { "At least 1 item should be requested, but got $limit." } return cache.query { idEq(MessageData::channelId, channelId) - MessageData::id lt messageId + idGt(MessageData::id, messageId) }.asFlow().map { Message(it, kord) }.take(limit) }