org.http4k.connect.storage.AutoCodec.kt Maven / Gradle / Ivy
The newest version!
package org.http4k.connect.storage
import io.lettuce.core.codec.RedisCodec
import io.lettuce.core.codec.StringCodec
import org.http4k.format.AutoMarshalling
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets.UTF_8
inline fun AutoRedisCodec(autoMarshalling: AutoMarshalling) = object : RedisCodec {
override fun decodeKey(bytes: ByteBuffer) = UTF_8.decode(bytes).toString()
override fun decodeValue(bytes: ByteBuffer) = autoMarshalling.asA(UTF_8.decode(bytes).toString())
override fun encodeKey(key: String) = StringCodec().encodeKey(key)
override fun encodeValue(value: T) = UTF_8.encode(autoMarshalling.asFormatString(value))
}