feat: PropertySerializerを追加
This commit is contained in:
parent
34c380a33b
commit
1124b69319
|
@ -1,9 +1,15 @@
|
|||
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 dev.usbharu.owl.common.property.*
|
||||
import org.koin.core.annotation.Singleton
|
||||
|
||||
@Singleton(binds = [PropertySerializerFactory::class])
|
||||
class DefaultPropertySerializerFactory : CustomPropertySerializerFactory(setOf(IntegerPropertySerializer()))
|
||||
class DefaultPropertySerializerFactory :
|
||||
CustomPropertySerializerFactory(
|
||||
setOf(
|
||||
IntegerPropertySerializer(),
|
||||
StringPropertyValueSerializer(),
|
||||
DoublePropertySerializer(),
|
||||
BooleanPropertySerializer()
|
||||
)
|
||||
)
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ package dev.usbharu.owl.common.property
|
|||
|
||||
class IntegerPropertyValue(override val value: Int) : PropertyValue<Int>() {
|
||||
override val type: PropertyType
|
||||
get() = PropertyType.integer
|
||||
get() = PropertyType.number
|
||||
}
|
||||
|
||||
class IntegerPropertySerializer : PropertySerializer<Int> {
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package dev.usbharu.owl.common.property
|
||||
|
||||
enum class PropertyType {
|
||||
integer,
|
||||
string
|
||||
number,
|
||||
string,
|
||||
binary
|
||||
}
|
|
@ -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:", ""))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue