Skip to content

Instantly share code, notes, and snippets.

@dcshock
Last active January 27, 2018 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcshock/7c085badd8969cf0568d3b9fc4de23bc to your computer and use it in GitHub Desktop.
Save dcshock/7c085badd8969cf0568d3b9fc4de23bc to your computer and use it in GitHub Desktop.
Protocol Buffer to Postgres JSONB - Kotlin Exposed
// Protocol Buffer (protobuf) V3 - Kotlin Exposed columnType
// Converts to and from jsonb fields in postgres.
class ProtoColumnType(val message: Message) : ColumnType() {
override fun sqlType() = "JSONB"
override fun valueFromDB(value: Any): Any {
if (value is PGobject) {
return JsonFormat.parser().ignoringUnknownFields().merge(value.value, message.toBuilder())
}
throw RuntimeException("Can't parse object: ${value}")
}
override fun setParameter(stmt: PreparedStatement, index: Int, value: Any?) {
val json = JsonFormat.printer().print(value as GeneratedMessageV3)
val obj = PGobject()
obj.type = "jsonb"
obj.value = json
stmt.setObject(index, obj)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment