feat: PropertySerializerを追加

This commit is contained in:
usbharu 2024-03-05 15:51:09 +09:00
parent 34c380a33b
commit 1124b69319
6 changed files with 86 additions and 7 deletions

View File

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

View File

@ -0,0 +1,24 @@
package dev.usbharu.owl.common.property
class BooleanPropertyValue(override val value: Boolean) : PropertyValue<Boolean>() {
override val type: PropertyType
get() = PropertyType.binary
}
class BooleanPropertySerializer : PropertySerializer<Boolean> {
override fun isSupported(propertyValue: PropertyValue<*>): Boolean {
return propertyValue.value is Boolean
}
override fun isSupported(string: String): Boolean {
return string.startsWith("bool:")
}
override fun serialize(propertyValue: PropertyValue<*>): String {
return "bool:" + propertyValue.value.toString()
}
override fun deserialize(string: String): PropertyValue<Boolean> {
return BooleanPropertyValue(string.replace("bool:", "").toBoolean())
}
}

View File

@ -0,0 +1,24 @@
package dev.usbharu.owl.common.property
class DoublePropertyValue(override val value: Double) : PropertyValue<Double>() {
override val type: PropertyType
get() = PropertyType.number
}
class DoublePropertySerializer : PropertySerializer<Double> {
override fun isSupported(propertyValue: PropertyValue<*>): Boolean {
return propertyValue.value is Double
}
override fun isSupported(string: String): Boolean {
return string.startsWith("double:")
}
override fun serialize(propertyValue: PropertyValue<*>): String {
return "double:" + propertyValue.value.toString()
}
override fun deserialize(string: String): PropertyValue<Double> {
return DoublePropertyValue(string.replace("double:", "").toDouble())
}
}

View File

@ -18,7 +18,7 @@ package dev.usbharu.owl.common.property
class IntegerPropertyValue(override val value: Int) : PropertyValue<Int>() { class IntegerPropertyValue(override val value: Int) : PropertyValue<Int>() {
override val type: PropertyType override val type: PropertyType
get() = PropertyType.integer get() = PropertyType.number
} }
class IntegerPropertySerializer : PropertySerializer<Int> { class IntegerPropertySerializer : PropertySerializer<Int> {

View File

@ -17,6 +17,7 @@
package dev.usbharu.owl.common.property package dev.usbharu.owl.common.property
enum class PropertyType { enum class PropertyType {
integer, number,
string string,
binary
} }

View File

@ -0,0 +1,24 @@
package dev.usbharu.owl.common.property
class StringPropertyValue(override val value: String) : PropertyValue<String>() {
override val type: PropertyType
get() = PropertyType.string
}
class StringPropertyValueSerializer : PropertySerializer<String> {
override fun isSupported(propertyValue: PropertyValue<*>): Boolean {
return propertyValue.value is String
}
override fun isSupported(string: String): Boolean {
return string.startsWith("str:")
}
override fun serialize(propertyValue: PropertyValue<*>): String {
return "str:" + propertyValue.value
}
override fun deserialize(string: String): PropertyValue<String> {
return StringPropertyValue(string.replace("str:", ""))
}
}