From 20d6cf3c320c383307bf3c01a896855a2a6ed80b Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 5 Mar 2024 15:51:09 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20PropertySerializer=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultPropertySerializerFactory.kt | 14 +++++++---- .../common/property/BooleanPropertyValue.kt | 24 +++++++++++++++++++ .../common/property/DoublePropertyValue.kt | 24 +++++++++++++++++++ .../common/property/IntegerPropertyValue.kt | 2 +- .../owl/common/property/PropertyType.kt | 5 ++-- .../common/property/StringPropertyValue.kt | 24 +++++++++++++++++++ 6 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 common/src/main/kotlin/dev/usbharu/owl/common/property/BooleanPropertyValue.kt create mode 100644 common/src/main/kotlin/dev/usbharu/owl/common/property/DoublePropertyValue.kt create mode 100644 common/src/main/kotlin/dev/usbharu/owl/common/property/StringPropertyValue.kt diff --git a/broker/src/main/kotlin/dev/usbharu/owl/broker/service/DefaultPropertySerializerFactory.kt b/broker/src/main/kotlin/dev/usbharu/owl/broker/service/DefaultPropertySerializerFactory.kt index b78fa47f..3e34c65b 100644 --- a/broker/src/main/kotlin/dev/usbharu/owl/broker/service/DefaultPropertySerializerFactory.kt +++ b/broker/src/main/kotlin/dev/usbharu/owl/broker/service/DefaultPropertySerializerFactory.kt @@ -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())) \ No newline at end of file +class DefaultPropertySerializerFactory : + CustomPropertySerializerFactory( + setOf( + IntegerPropertySerializer(), + StringPropertyValueSerializer(), + DoublePropertySerializer(), + BooleanPropertySerializer() + ) + ) \ No newline at end of file diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/property/BooleanPropertyValue.kt b/common/src/main/kotlin/dev/usbharu/owl/common/property/BooleanPropertyValue.kt new file mode 100644 index 00000000..0a04685a --- /dev/null +++ b/common/src/main/kotlin/dev/usbharu/owl/common/property/BooleanPropertyValue.kt @@ -0,0 +1,24 @@ +package dev.usbharu.owl.common.property + +class BooleanPropertyValue(override val value: Boolean) : PropertyValue() { + override val type: PropertyType + get() = PropertyType.binary +} + +class BooleanPropertySerializer : PropertySerializer { + 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 { + return BooleanPropertyValue(string.replace("bool:", "").toBoolean()) + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/property/DoublePropertyValue.kt b/common/src/main/kotlin/dev/usbharu/owl/common/property/DoublePropertyValue.kt new file mode 100644 index 00000000..13156f20 --- /dev/null +++ b/common/src/main/kotlin/dev/usbharu/owl/common/property/DoublePropertyValue.kt @@ -0,0 +1,24 @@ +package dev.usbharu.owl.common.property + +class DoublePropertyValue(override val value: Double) : PropertyValue() { + override val type: PropertyType + get() = PropertyType.number +} + +class DoublePropertySerializer : PropertySerializer { + 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 { + return DoublePropertyValue(string.replace("double:", "").toDouble()) + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/property/IntegerPropertyValue.kt b/common/src/main/kotlin/dev/usbharu/owl/common/property/IntegerPropertyValue.kt index 331f8f7d..42782069 100644 --- a/common/src/main/kotlin/dev/usbharu/owl/common/property/IntegerPropertyValue.kt +++ b/common/src/main/kotlin/dev/usbharu/owl/common/property/IntegerPropertyValue.kt @@ -18,7 +18,7 @@ package dev.usbharu.owl.common.property class IntegerPropertyValue(override val value: Int) : PropertyValue() { override val type: PropertyType - get() = PropertyType.integer + get() = PropertyType.number } class IntegerPropertySerializer : PropertySerializer { diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/property/PropertyType.kt b/common/src/main/kotlin/dev/usbharu/owl/common/property/PropertyType.kt index 66941146..c9dabae5 100644 --- a/common/src/main/kotlin/dev/usbharu/owl/common/property/PropertyType.kt +++ b/common/src/main/kotlin/dev/usbharu/owl/common/property/PropertyType.kt @@ -17,6 +17,7 @@ package dev.usbharu.owl.common.property enum class PropertyType { - integer, - string + number, + string, + binary } \ No newline at end of file diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/property/StringPropertyValue.kt b/common/src/main/kotlin/dev/usbharu/owl/common/property/StringPropertyValue.kt new file mode 100644 index 00000000..a7030d22 --- /dev/null +++ b/common/src/main/kotlin/dev/usbharu/owl/common/property/StringPropertyValue.kt @@ -0,0 +1,24 @@ +package dev.usbharu.owl.common.property + +class StringPropertyValue(override val value: String) : PropertyValue() { + override val type: PropertyType + get() = PropertyType.string +} + +class StringPropertyValueSerializer : PropertySerializer { + 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 { + return StringPropertyValue(string.replace("str:", "")) + } +} \ No newline at end of file