From c864a6a49122513561d1a128699ee7c87ac1ff2f Mon Sep 17 00:00:00 2001 From: grimsi <9295182+grimsi@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:00:56 +0200 Subject: [PATCH] Replaced hardcoded mapping with reflection (yay reflection) --- .../de/grimsi/gameyfin/shared/token/TokenService.kt | 2 +- .../de/grimsi/gameyfin/shared/token/TokenType.kt | 9 +-------- .../gameyfin/shared/token/TokenTypeUserType.kt | 13 +++++++------ 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenService.kt b/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenService.kt index 95e128f..ffbc95d 100644 --- a/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenService.kt +++ b/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenService.kt @@ -17,7 +17,7 @@ abstract class TokenService( ) tokenRepository.findByUserAndType(user, type)?.let { - log.warn { "Deleting existing '${it.type}' token for user '${user.username}'" } + log.warn { "Deleting existing ${it.type.key} token for user '${user.username}'" } delete(it) } diff --git a/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenType.kt b/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenType.kt index fe3ada9..bbc916a 100644 --- a/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenType.kt +++ b/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenType.kt @@ -1,20 +1,13 @@ package de.grimsi.gameyfin.shared.token -import java.io.Serializable import kotlin.time.Duration import kotlin.time.Duration.Companion.minutes sealed class TokenType( val key: String, val expiration: Duration -) : Serializable { +) { data object PasswordReset : TokenType("password-reset", 15.minutes) data object EmailVerification : TokenType("email-verification", Duration.INFINITE) data object Invitation : TokenType("invitation", Duration.INFINITE) - - fun readResolve(): Any = when (this) { - PasswordReset -> PasswordReset - EmailVerification -> EmailVerification - Invitation -> Invitation - } } diff --git a/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenTypeUserType.kt b/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenTypeUserType.kt index 100e52f..50140d5 100644 --- a/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenTypeUserType.kt +++ b/src/main/kotlin/de/grimsi/gameyfin/shared/token/TokenTypeUserType.kt @@ -6,6 +6,7 @@ import java.io.Serializable import java.sql.PreparedStatement import java.sql.ResultSet import java.sql.Types +import kotlin.reflect.full.createInstance class TokenTypeUserType : UserType { @@ -24,12 +25,12 @@ class TokenTypeUserType : UserType { owner: Any? ): TokenType? { val key = rs.getString(position) ?: return null - return when (key) { - TokenType.PasswordReset.key -> TokenType.PasswordReset - TokenType.EmailVerification.key -> TokenType.EmailVerification - TokenType.Invitation.key -> TokenType.Invitation - else -> throw IllegalArgumentException("Unknown TokenType key: $key") - } + val tokenTypeClass = TokenType::class + + return tokenTypeClass.sealedSubclasses + .map { it.objectInstance ?: it.createInstance() } + .firstOrNull { it.key == key } + ?: throw IllegalArgumentException("Unknown TokenType: $key") } override fun nullSafeSet(