feat: 型安全に

This commit is contained in:
usbharu 2023-10-12 16:20:18 +09:00
parent 1fef97c8ce
commit 5b65630556
2 changed files with 22 additions and 15 deletions

View File

@ -4,6 +4,14 @@ import dev.usbharu.hideout.domain.model.ap.Object
import dev.usbharu.hideout.domain.model.hideout.entity.User
interface APResourceResolveService {
suspend fun resolve(url: String, singerId: Long?): Object
suspend fun resolve(url: String, singer: User?): Object
suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singer: User?): T
suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singerId: Long?): T
}
suspend inline fun <reified T : Object> APResourceResolveService.resolve(url: String, singer: User?): T {
return resolve(url, T::class.java, singer)
}
suspend inline fun <reified T : Object> APResourceResolveService.resolve(url: String, singerId: Long?): T {
return resolve(url, T::class.java, singerId)
}

View File

@ -1,7 +1,6 @@
package dev.usbharu.hideout.service.ap.resource
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.domain.model.ap.Object
import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.repository.UserRepository
@ -20,35 +19,35 @@ class APResourceResolveServiceImpl(
) :
APResourceResolveService {
override suspend fun resolve(url: String, singerId: Long?): Object {
return internalResolve(url, singerId)
override suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singerId: Long?): T {
return internalResolve(url, singerId, clazz)
}
override suspend fun resolve(url: String, singer: User?): Object {
return internalResolve(url, singer)
override suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singer: User?): T {
return internalResolve(url, singer, clazz)
}
private suspend fun internalResolve(url: String, singerId: Long?): Object {
private suspend fun <T : Object> internalResolve(url: String, singerId: Long?, clazz: Class<T>): T {
val key = genCacheKey(url, singerId)
cacheManager.putCache(key) {
runResolve(url, singerId?.let { userRepository.findById(it) })
runResolve(url, singerId?.let { userRepository.findById(it) }, clazz)
}
return cacheManager.getOrWait(key)
return cacheManager.getOrWait(key) as T
}
private suspend fun internalResolve(url: String, singer: User?): Object {
private suspend fun <T : Object> internalResolve(url: String, singer: User?, clazz: Class<T>): T {
val key = genCacheKey(url, singer?.id)
cacheManager.putCache(key) {
runResolve(url, singer)
runResolve(url, singer, clazz)
}
return cacheManager.getOrWait(key)
return cacheManager.getOrWait(key) as T
}
private suspend fun runResolve(url: String, singer: User?): Object {
private suspend fun <T : Object> runResolve(url: String, singer: User?, clazz: Class<T>): Object {
val bodyAsText = httpClient.get(url).bodyAsText()
return objectMapper.readValue<Object>(bodyAsText)
return objectMapper.readValue(bodyAsText, clazz)
}
private fun genCacheKey(url: String, singerId: Long?): String {