Propertyをジェネリックを使用して型安全に

This commit is contained in:
usbharu 2024-03-05 15:26:55 +09:00
parent 8fb5fa416d
commit 753c1c9426
11 changed files with 48 additions and 25 deletions

View File

@ -18,8 +18,6 @@ package dev.usbharu.owl.broker
import dev.usbharu.owl.broker.service.DefaultRetryPolicyFactory
import dev.usbharu.owl.broker.service.RetryPolicyFactory
import dev.usbharu.owl.common.property.PropertySerializerFactory
import dev.usbharu.owl.common.property.PropertySerializerFactoryImpl
import kotlinx.coroutines.runBlocking
import org.koin.core.context.startKoin
import org.koin.dsl.module
@ -33,9 +31,6 @@ fun main() {
printLogger()
val module = module {
single<PropertySerializerFactory> {
PropertySerializerFactoryImpl()
}
single<RetryPolicyFactory> {
DefaultRetryPolicyFactory(emptyMap())
}

View File

@ -31,5 +31,5 @@ data class Task(
val nextRetry:Instant,
val completedAt: Instant? = null,
val attempt: Int,
val properties: Map<String, PropertyValue>
val properties: Map<String, PropertyValue<*>>
)

View File

@ -0,0 +1,9 @@
package dev.usbharu.owl.broker.service
import dev.usbharu.owl.common.property.CustomPropertySerializerFactory
import dev.usbharu.owl.common.property.IntegerPropertySerializer
import dev.usbharu.owl.common.property.PropertySerializerFactory
import org.koin.core.annotation.Singleton
@Singleton(binds = [PropertySerializerFactory::class])
class DefaultPropertySerializerFactory : CustomPropertySerializerFactory(setOf(IntegerPropertySerializer()))

View File

@ -32,7 +32,7 @@ interface TaskPublishService {
data class PublishTask(
val name: String,
val producerId: UUID,
val properties: Map<String, PropertyValue>
val properties: Map<String, PropertyValue<*>>
)
data class PublishedTask(

View File

@ -17,12 +17,13 @@
package dev.usbharu.owl.common.property
class PropertySerializerFactoryImpl : PropertySerializerFactory {
override fun factory(propertyValue: PropertyValue): PropertySerializer {
TODO("Not yet implemented")
open class CustomPropertySerializerFactory(private val propertySerializers: Set<PropertySerializer<*>>) :
PropertySerializerFactory {
override fun <T> factory(propertyValue: PropertyValue<T>): PropertySerializer<T> {
return propertySerializers.first { it.isSupported(propertyValue) } as PropertySerializer<T>
}
override fun factory(string: String): PropertySerializer {
TODO("Not yet implemented")
override fun factory(string: String): PropertySerializer<*> {
return propertySerializers.first { it.isSupported(string) }
}
}

View File

@ -16,7 +16,25 @@
package dev.usbharu.owl.common.property
class IntegerPropertyValue(override val value: Int) : PropertyValue() {
class IntegerPropertyValue(override val value: Int) : PropertyValue<Int>() {
override val type: PropertyType
get() = PropertyType.integer
}
class IntegerPropertySerializer : PropertySerializer<Int> {
override fun isSupported(propertyValue: PropertyValue<*>): Boolean {
return propertyValue.value is Int
}
override fun isSupported(string: String): Boolean {
return string.startsWith("int32:")
}
override fun serialize(propertyValue: PropertyValue<*>): String {
return "int32:" + propertyValue.value.toString()
}
override fun deserialize(string: String): PropertyValue<Int> {
return IntegerPropertyValue(string.replace("int32:", "").toInt())
}
}

View File

@ -19,13 +19,13 @@ package dev.usbharu.owl.common.property
object PropertySerializeUtils {
fun serialize(
serializerFactory: PropertySerializerFactory,
properties: Map<String, PropertyValue>
properties: Map<String, PropertyValue<*>>
): Map<String, String> =
properties.map { it.key to serializerFactory.factory(it.value).serialize(it.value) }.toMap()
fun deserialize(
serializerFactory: PropertySerializerFactory,
properties: Map<String, String>
): Map<String, PropertyValue> =
): Map<String, PropertyValue<*>> =
properties.map { it.key to serializerFactory.factory(it.value).deserialize(it.value) }.toMap()
}

View File

@ -16,10 +16,10 @@
package dev.usbharu.owl.common.property
interface PropertySerializer {
fun isSupported(propertyValue: PropertyValue): Boolean
interface PropertySerializer<T> {
fun isSupported(propertyValue: PropertyValue<*>): Boolean
fun isSupported(string: String): Boolean
fun serialize(propertyValue: PropertyValue): String
fun serialize(propertyValue: PropertyValue<*>): String
fun deserialize(string: String): PropertyValue
fun deserialize(string: String): PropertyValue<T>
}

View File

@ -17,6 +17,6 @@
package dev.usbharu.owl.common.property
interface PropertySerializerFactory {
fun factory(propertyValue: PropertyValue): PropertySerializer
fun factory(string: String): PropertySerializer
fun <T> factory(propertyValue: PropertyValue<T>): PropertySerializer<T>
fun factory(string: String): PropertySerializer<*>
}

View File

@ -16,7 +16,7 @@
package dev.usbharu.owl.common.property
sealed class PropertyValue {
abstract val value:Any
sealed class PropertyValue<T> {
abstract val value: T
abstract val type: PropertyType
}

View File

@ -27,6 +27,6 @@ interface TaskDefinition<T : Task> {
val timeoutMilli: Long
val propertyDefinition: PropertyDefinition
fun serialize(task: T): Map<String, PropertyValue>
fun deserialize(value: Map<String, PropertyValue>): T
fun serialize(task: T): Map<String, PropertyValue<*>>
fun deserialize(value: Map<String, PropertyValue<*>>): T
}