mirror of https://github.com/usbharu/Hideout.git
Propertyをジェネリックを使用して型安全に
This commit is contained in:
parent
7043462b58
commit
63fa08fd69
|
@ -18,8 +18,6 @@ package dev.usbharu.owl.broker
|
||||||
|
|
||||||
import dev.usbharu.owl.broker.service.DefaultRetryPolicyFactory
|
import dev.usbharu.owl.broker.service.DefaultRetryPolicyFactory
|
||||||
import dev.usbharu.owl.broker.service.RetryPolicyFactory
|
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 kotlinx.coroutines.runBlocking
|
||||||
import org.koin.core.context.startKoin
|
import org.koin.core.context.startKoin
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
|
@ -33,9 +31,6 @@ fun main() {
|
||||||
printLogger()
|
printLogger()
|
||||||
|
|
||||||
val module = module {
|
val module = module {
|
||||||
single<PropertySerializerFactory> {
|
|
||||||
PropertySerializerFactoryImpl()
|
|
||||||
}
|
|
||||||
single<RetryPolicyFactory> {
|
single<RetryPolicyFactory> {
|
||||||
DefaultRetryPolicyFactory(emptyMap())
|
DefaultRetryPolicyFactory(emptyMap())
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,5 +31,5 @@ data class Task(
|
||||||
val nextRetry:Instant,
|
val nextRetry:Instant,
|
||||||
val completedAt: Instant? = null,
|
val completedAt: Instant? = null,
|
||||||
val attempt: Int,
|
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(
|
data class PublishTask(
|
||||||
val name: String,
|
val name: String,
|
||||||
val producerId: UUID,
|
val producerId: UUID,
|
||||||
val properties: Map<String, PropertyValue>
|
val properties: Map<String, PropertyValue<*>>
|
||||||
)
|
)
|
||||||
|
|
||||||
data class PublishedTask(
|
data class PublishedTask(
|
||||||
|
|
|
@ -17,12 +17,13 @@
|
||||||
package dev.usbharu.owl.common.property
|
package dev.usbharu.owl.common.property
|
||||||
|
|
||||||
|
|
||||||
class PropertySerializerFactoryImpl : PropertySerializerFactory {
|
open class CustomPropertySerializerFactory(private val propertySerializers: Set<PropertySerializer<*>>) :
|
||||||
override fun factory(propertyValue: PropertyValue): PropertySerializer {
|
PropertySerializerFactory {
|
||||||
TODO("Not yet implemented")
|
override fun <T> factory(propertyValue: PropertyValue<T>): PropertySerializer<T> {
|
||||||
|
return propertySerializers.first { it.isSupported(propertyValue) } as PropertySerializer<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun factory(string: String): PropertySerializer {
|
override fun factory(string: String): PropertySerializer<*> {
|
||||||
TODO("Not yet implemented")
|
return propertySerializers.first { it.isSupported(string) }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,7 +16,25 @@
|
||||||
|
|
||||||
package dev.usbharu.owl.common.property
|
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
|
override val type: PropertyType
|
||||||
get() = PropertyType.integer
|
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 {
|
object PropertySerializeUtils {
|
||||||
fun serialize(
|
fun serialize(
|
||||||
serializerFactory: PropertySerializerFactory,
|
serializerFactory: PropertySerializerFactory,
|
||||||
properties: Map<String, PropertyValue>
|
properties: Map<String, PropertyValue<*>>
|
||||||
): Map<String, String> =
|
): Map<String, String> =
|
||||||
properties.map { it.key to serializerFactory.factory(it.value).serialize(it.value) }.toMap()
|
properties.map { it.key to serializerFactory.factory(it.value).serialize(it.value) }.toMap()
|
||||||
|
|
||||||
fun deserialize(
|
fun deserialize(
|
||||||
serializerFactory: PropertySerializerFactory,
|
serializerFactory: PropertySerializerFactory,
|
||||||
properties: Map<String, String>
|
properties: Map<String, String>
|
||||||
): Map<String, PropertyValue> =
|
): Map<String, PropertyValue<*>> =
|
||||||
properties.map { it.key to serializerFactory.factory(it.value).deserialize(it.value) }.toMap()
|
properties.map { it.key to serializerFactory.factory(it.value).deserialize(it.value) }.toMap()
|
||||||
}
|
}
|
|
@ -16,10 +16,10 @@
|
||||||
|
|
||||||
package dev.usbharu.owl.common.property
|
package dev.usbharu.owl.common.property
|
||||||
|
|
||||||
interface PropertySerializer {
|
interface PropertySerializer<T> {
|
||||||
fun isSupported(propertyValue: PropertyValue): Boolean
|
fun isSupported(propertyValue: PropertyValue<*>): Boolean
|
||||||
fun isSupported(string: String): 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
|
package dev.usbharu.owl.common.property
|
||||||
|
|
||||||
interface PropertySerializerFactory {
|
interface PropertySerializerFactory {
|
||||||
fun factory(propertyValue: PropertyValue): PropertySerializer
|
fun <T> factory(propertyValue: PropertyValue<T>): PropertySerializer<T>
|
||||||
fun factory(string: String): PropertySerializer
|
fun factory(string: String): PropertySerializer<*>
|
||||||
}
|
}
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
package dev.usbharu.owl.common.property
|
package dev.usbharu.owl.common.property
|
||||||
|
|
||||||
sealed class PropertyValue {
|
sealed class PropertyValue<T> {
|
||||||
abstract val value:Any
|
abstract val value: T
|
||||||
abstract val type: PropertyType
|
abstract val type: PropertyType
|
||||||
}
|
}
|
|
@ -27,6 +27,6 @@ interface TaskDefinition<T : Task> {
|
||||||
val timeoutMilli: Long
|
val timeoutMilli: Long
|
||||||
val propertyDefinition: PropertyDefinition
|
val propertyDefinition: PropertyDefinition
|
||||||
|
|
||||||
fun serialize(task: T): Map<String, PropertyValue>
|
fun serialize(task: T): Map<String, PropertyValue<*>>
|
||||||
fun deserialize(value: Map<String, PropertyValue>): T
|
fun deserialize(value: Map<String, PropertyValue<*>>): T
|
||||||
}
|
}
|
Loading…
Reference in New Issue