Propertyをジェネリックを使用して型安全に
This commit is contained in:
parent
8fb5fa416d
commit
753c1c9426
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -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<*>>
|
||||
)
|
||||
|
|
|
@ -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()))
|
|
@ -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(
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
}
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
}
|
|
@ -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>
|
||||
}
|
|
@ -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<*>
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue