Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit d85f965

Browse files
committed
feat: add ComponentColumnType for handling Component serialization in database
1 parent 2605d30 commit d85f965

File tree

1 file changed

+26
-0
lines changed
  • surf-cloud-api/surf-cloud-api-server/src/main/kotlin/dev/slne/surf/cloud/api/server/exposed/columns

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dev.slne.surf.cloud.api.server.exposed.columns
2+
3+
import net.kyori.adventure.text.Component
4+
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
5+
import org.jetbrains.exposed.sql.Column
6+
import org.jetbrains.exposed.sql.ColumnType
7+
import org.jetbrains.exposed.sql.Table
8+
import org.jetbrains.exposed.sql.vendors.currentDialect
9+
import java.nio.ByteBuffer
10+
11+
class ComponentColumnType : ColumnType<Component>() {
12+
override fun sqlType() = currentDialect.dataTypeProvider.largeTextType()
13+
14+
override fun valueFromDB(value: Any): Component = when (value) {
15+
is Component -> value
16+
is String -> GsonComponentSerializer.gson().deserialize(value)
17+
is ByteArray -> GsonComponentSerializer.gson().deserialize(value.decodeToString())
18+
is ByteBuffer -> GsonComponentSerializer.gson().deserialize(value.array().decodeToString())
19+
else -> error("Unexpected value of type Component: $value of ${value::class.qualifiedName}")
20+
}
21+
22+
override fun nonNullValueToString(value: Component): String =
23+
GsonComponentSerializer.gson().serialize(value)
24+
}
25+
26+
fun Table.component(name: String): Column<Component> = registerColumn(name, ComponentColumnType())

0 commit comments

Comments
 (0)