Skip to content

Commit

Permalink
Fix type coercion in cache queries
Browse files Browse the repository at this point in the history
  • Loading branch information
BartArys committed Nov 23, 2020
1 parent 70cb4d9 commit 4933bf8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -100,7 +101,7 @@ interface GuildBehavior : Entity, Strategizable {
* This property is not resolvable through REST and will always use [KordCache] instead.
*/
val presences: Flow<Presence>
get() = kord.cache.query<PresenceData> { PresenceData::guildId eq id.value }
get() = kord.cache.query<PresenceData> { idEq(PresenceData::guildId, id) }
.asFlow()
.map { Presence(it, kord) }

Expand Down Expand Up @@ -149,7 +150,7 @@ interface GuildBehavior : Entity, Strategizable {
*/
val voiceStates: Flow<VoiceState>
get() = kord.cache
.query<VoiceStateData> { VoiceStateData::guildId eq id.value }
.query<VoiceStateData> { idEq( VoiceStateData::guildId, id) }
.asFlow()
.map { VoiceState(it, kord) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -139,8 +140,8 @@ interface MemberBehavior : Entity, UserBehavior {
*/
suspend fun getPresenceOrNull(): Presence? {
val data = kord.cache.query<PresenceData> {
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)
Expand Down Expand Up @@ -170,8 +171,8 @@ interface MemberBehavior : Entity, UserBehavior {
*/
suspend fun getVoiceStateOrNull(): VoiceState? {
val data = kord.cache.query<VoiceStateData> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,7 +38,7 @@ interface VoiceChannelBehavior : GuildChannelBehavior {
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*/
val voiceStates: Flow<VoiceState>
get() = kord.cache.query<VoiceStateData> { VoiceStateData::channelId eq id.value }
get() = kord.cache.query<VoiceStateData> { idEq(VoiceStateData::channelId, id) }
.asFlow()
.map { VoiceState(it, kord) }

Expand Down
16 changes: 15 additions & 1 deletion core/src/main/kotlin/com/gitlab/kordlib/core/cache/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Any> QueryBuilder<T>.idEq(property: KProperty1<T, Snowflake>, value: Snowflake?) {
fun <T : Any> QueryBuilder<T>.idEq(property: KProperty1<T, Snowflake?>, value: Snowflake?) {
property.eq(value)
}

Expand All @@ -19,3 +19,17 @@ fun <T : Any> QueryBuilder<T>.idEq(property: KProperty1<T, OptionalSnowflake>, v
fun <T : Any> QueryBuilder<T>.idEq(property: KProperty1<T, Optional<String>>, value: String?) {
property.eq(Optional(value))
}

fun <T : Any> QueryBuilder<T>.idGt(property: KProperty1<T, Snowflake>, value: Snowflake) {
property.gt(value)
}

@JvmName("stringEq")
fun <T : Any> QueryBuilder<T>.idEq(property: KProperty1<T, String?>, value: String?) {
property.eq(value)
}

@JvmName("booleanEq")
fun <T : Any> QueryBuilder<T>.idEq(property: KProperty1<T, Boolean?>, value: Boolean?) {
property.eq(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal class GuildEventHandler(
}

private suspend fun handle(event: GuildDelete, shard: Int) = with(event.guild) {
val query = cache.query<GuildData> { GuildData::id eq id }
val query = cache.query<GuildData> { idEq(GuildData::id, id) }

val old = query.asFlow().map { Guild(it, kord) }.singleOrNull()
query.remove()
Expand Down Expand Up @@ -124,7 +124,7 @@ internal class GuildEventHandler(

val emojis = data.map { GuildEmoji(it, kord) }

cache.query<GuildData> { GuildData::id eq guildId.value }.update {
cache.query<GuildData> { idEq(GuildData::id, guildId) }.update {
it.copy(emojis = emojis.map { emoji -> emoji.id })
}

Expand All @@ -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> { UserData::id eq userData.id }.remove()
cache.query<UserData> { idEq(UserData::id, userData.id) }.remove()
val user = User(userData, kord)

coreFlow.emit(MemberLeaveEvent(user, guildId, shard))
Expand Down Expand Up @@ -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> { PresenceData::id eq data.id }
val old = cache.query<PresenceData> { idEq(PresenceData::id, data.id) }
.asFlow().map { Presence(it, kord) }.singleOrNull()

cache.put(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> { UserData::id eq data.id }
val old = cache.query<UserData> { idEq(UserData::id, data.id) }
.asFlow().map { User(it, kord) }.singleOrNull()

cache.put(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> { VoiceStateData::id eq data.id }
val old = cache.query<VoiceStateData> { idEq(VoiceStateData::id, data.id) }
.asFlow().map { VoiceState(it, kord) }.singleOrNull()

cache.put(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,7 +115,7 @@ class CacheEntitySupplier(private val kord: Kord) : EntitySupplier {

override fun getChannelPins(channelId: Snowflake): Flow<Message> = cache.query<MessageData> {
idEq(MessageData::channelId, channelId)
MessageData::pinned eq true
idEq(MessageData::pinned, true)
}.asFlow().map { Message(it, kord) }

override suspend fun getGuildOrNull(id: Snowflake): Guild? {
Expand Down Expand Up @@ -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<MessageData> {
idEq(MessageData::channelId, channelId)
MessageData::id lt messageId
idGt(MessageData::id, messageId)
}.asFlow().map { Message(it, kord) }.take(limit)
}

Expand Down

0 comments on commit 4933bf8

Please sign in to comment.