From 0d9b4e9a46df5ba285a3ac73aab9c46c1ee44de1 Mon Sep 17 00:00:00 2001 From: woowabrie Date: Fri, 14 Jun 2024 09:15:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20AttributeConverter=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/domain/StringToListConverter.kt | 18 ++++++++++++------ .../domain/StringToLongListConverter.kt | 19 ------------------- 2 files changed, 12 insertions(+), 25 deletions(-) delete mode 100644 src/main/kotlin/support/domain/StringToLongListConverter.kt diff --git a/src/main/kotlin/support/domain/StringToListConverter.kt b/src/main/kotlin/support/domain/StringToListConverter.kt index f375d0a1f..3e26f1b44 100644 --- a/src/main/kotlin/support/domain/StringToListConverter.kt +++ b/src/main/kotlin/support/domain/StringToListConverter.kt @@ -2,18 +2,24 @@ package support.domain import javax.persistence.AttributeConverter import javax.persistence.Converter +import kotlin.reflect.KClass -@Converter -class StringToListConverter : AttributeConverter, String> { - override fun convertToDatabaseColumn(recipients: List): String { - return recipients.joinToString(COMMA) +abstract class StringToListConverter( + private val type: KClass, + private val transform: (String) -> T, +) : AttributeConverter, String> { + override fun convertToDatabaseColumn(attribute: List): String { + return attribute.joinToString(COMMA) } - override fun convertToEntityAttribute(dbData: String): List { - return dbData.split(COMMA) + override fun convertToEntityAttribute(dbData: String): List { + return dbData.split(COMMA).map { transform(it) } } companion object { private const val COMMA: String = "," } } + +@Converter +class StringToLongListConverter : StringToListConverter(Long::class, String::toLong) diff --git a/src/main/kotlin/support/domain/StringToLongListConverter.kt b/src/main/kotlin/support/domain/StringToLongListConverter.kt deleted file mode 100644 index 944c76366..000000000 --- a/src/main/kotlin/support/domain/StringToLongListConverter.kt +++ /dev/null @@ -1,19 +0,0 @@ -package support.domain - -import javax.persistence.AttributeConverter -import javax.persistence.Converter - -@Converter -class StringToLongListConverter : AttributeConverter, String> { - override fun convertToDatabaseColumn(recipients: List): String { - return recipients.joinToString(COMMA) - } - - override fun convertToEntityAttribute(dbData: String): List { - return dbData.split(COMMA).map { it.toLong() } - } - - companion object { - private const val COMMA: String = "," - } -}