From 6a36c7e555735e1fc19b24f42d067dc11f25ada5 Mon Sep 17 00:00:00 2001 From: mentegy Date: Fri, 6 Apr 2018 19:12:36 +0300 Subject: [PATCH] `quill-cassandra`: Add encoding for `Byte` and `Short` --- .../getquill/context/cassandra/CassandraContext.scala | 4 ++++ .../context/cassandra/encoding/CassandraTypes.scala | 11 ++++++++++- .../context/cassandra/encoding/Decoders.scala | 2 ++ .../context/cassandra/encoding/Encoders.scala | 2 ++ quill-cassandra/src/test/cql/cassandra-schema.cql | 4 ++++ .../io/getquill/context/cassandra/EncodingSpec.scala | 6 ++++++ .../context/cassandra/ListsEncodingSpec.scala | 6 ++++-- 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/CassandraContext.scala b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/CassandraContext.scala index d24bdb51cd..c34972edcd 100644 --- a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/CassandraContext.scala +++ b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/CassandraContext.scala @@ -21,6 +21,8 @@ trait CassandraContext[N <: NamingStrategy] implicit val stringDecoder: Decoder[String] implicit val bigDecimalDecoder: Decoder[BigDecimal] implicit val booleanDecoder: Decoder[Boolean] + implicit val byteDecoder: Decoder[Byte] + implicit val shortDecoder: Decoder[Short] implicit val intDecoder: Decoder[Int] implicit val longDecoder: Decoder[Long] implicit val floatDecoder: Decoder[Float] @@ -33,6 +35,8 @@ trait CassandraContext[N <: NamingStrategy] implicit val stringEncoder: Encoder[String] implicit val bigDecimalEncoder: Encoder[BigDecimal] implicit val booleanEncoder: Encoder[Boolean] + implicit val byteEncoder: Encoder[Byte] + implicit val shortEncoder: Encoder[Short] implicit val intEncoder: Encoder[Int] implicit val longEncoder: Encoder[Long] implicit val floatEncoder: Encoder[Float] diff --git a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/CassandraTypes.scala b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/CassandraTypes.scala index dd42b467e6..8443f4be6b 100644 --- a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/CassandraTypes.scala +++ b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/CassandraTypes.scala @@ -1,6 +1,6 @@ package io.getquill.context.cassandra.encoding -import java.lang.{ Boolean => JBoolean, Double => JDouble, Float => JFloat, Integer => JInt, Long => JLong } +import java.lang.{ Boolean => JBoolean, Double => JDouble, Float => JFloat, Integer => JInt, Long => JLong, Short => JShort, Byte => JByte } import java.math.{ BigDecimal => JBigDecimal } import java.nio.ByteBuffer import java.util.{ Date, UUID } @@ -13,6 +13,8 @@ import com.datastax.driver.core.LocalDate * For custom types please use `MappedEncoding` as in `MappedTypes` trait for example. */ trait CassandraTypes extends CassandraMappedTypes { + implicit val byteCassandraType: CassandraType[JByte] = CassandraType.of[JByte] + implicit val shortCassandraType: CassandraType[JShort] = CassandraType.of[JShort] implicit val integerCassandraType: CassandraType[JInt] = CassandraType.of[JInt] implicit val longCassandraType: CassandraType[JLong] = CassandraType.of[JLong] implicit val floatCassandraType: CassandraType[JFloat] = CassandraType.of[JFloat] @@ -31,6 +33,13 @@ trait CassandraTypes extends CassandraMappedTypes { * which are not in relation with CassandraTypes but can be represented as ones. */ trait CassandraMappedTypes { + + implicit val encodeByte: CassandraMapper[Byte, JByte] = CassandraMapper(byte2Byte) + implicit val decodeByte: CassandraMapper[JByte, Byte] = CassandraMapper(Byte2byte) + + implicit val encodeShort: CassandraMapper[Short, JShort] = CassandraMapper(short2Short) + implicit val decodeShort: CassandraMapper[JShort, Short] = CassandraMapper(Short2short) + implicit val encodeInt: CassandraMapper[Int, JInt] = CassandraMapper(int2Integer) implicit val decodeInt: CassandraMapper[JInt, Int] = CassandraMapper(Integer2int) diff --git a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Decoders.scala b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Decoders.scala index d2111c6792..4b6848c0f8 100644 --- a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Decoders.scala +++ b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Decoders.scala @@ -42,6 +42,8 @@ trait Decoders extends CollectionDecoders { implicit val bigDecimalDecoder: Decoder[BigDecimal] = decoder((index, row) => row.getDecimal(index)) implicit val booleanDecoder: Decoder[Boolean] = decoder(_.getBool) + implicit val byteDecoder: Decoder[Byte] = decoder(_.getByte) + implicit val shortDecoder: Decoder[Short] = decoder(_.getShort) implicit val intDecoder: Decoder[Int] = decoder(_.getInt) implicit val longDecoder: Decoder[Long] = decoder(_.getLong) implicit val floatDecoder: Decoder[Float] = decoder(_.getFloat) diff --git a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Encoders.scala b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Encoders.scala index d9c856538e..a784333a9e 100644 --- a/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Encoders.scala +++ b/quill-cassandra/src/main/scala/io/getquill/context/cassandra/encoding/Encoders.scala @@ -39,6 +39,8 @@ trait Encoders extends CollectionEncoders { implicit val bigDecimalEncoder: Encoder[BigDecimal] = encoder((index, value, row) => row.setDecimal(index, value.bigDecimal)) implicit val booleanEncoder: Encoder[Boolean] = encoder(_.setBool) + implicit val byteEncoder: Encoder[Byte] = encoder(_.setByte) + implicit val shortEncoder: Encoder[Short] = encoder(_.setShort) implicit val intEncoder: Encoder[Int] = encoder(_.setInt) implicit val longEncoder: Encoder[Long] = encoder(_.setLong) implicit val floatEncoder: Encoder[Float] = encoder(_.setFloat) diff --git a/quill-cassandra/src/test/cql/cassandra-schema.cql b/quill-cassandra/src/test/cql/cassandra-schema.cql index 8bcd58128a..2cd8c5f1fc 100644 --- a/quill-cassandra/src/test/cql/cassandra-schema.cql +++ b/quill-cassandra/src/test/cql/cassandra-schema.cql @@ -46,6 +46,8 @@ CREATE TABLE EncodingTestEntity( v9 DATE, v10 UUID, v11 TIMESTAMP, + v12 TINYINT, + v13 SMALLINT, o1 VARCHAR, o2 DECIMAL, o3 BOOLEAN, @@ -92,6 +94,8 @@ CREATE TABLE ListsEntity( texts LIST, decimals LIST, bools LIST, + bytes LIST, + shorts LIST, ints LIST, longs LIST, floats LIST, diff --git a/quill-cassandra/src/test/scala/io/getquill/context/cassandra/EncodingSpec.scala b/quill-cassandra/src/test/scala/io/getquill/context/cassandra/EncodingSpec.scala index ab2af478a1..593cecaed7 100644 --- a/quill-cassandra/src/test/scala/io/getquill/context/cassandra/EncodingSpec.scala +++ b/quill-cassandra/src/test/scala/io/getquill/context/cassandra/EncodingSpec.scala @@ -191,6 +191,8 @@ class EncodingSpec extends Spec { v9: LocalDate, v10: UUID, v11: Date, + v12: Byte, + v13: Short, o1: Option[String], o2: Option[BigDecimal], o3: Option[Boolean], @@ -220,6 +222,8 @@ class EncodingSpec extends Spec { v9 = LocalDate.fromYearMonthDay(2014, 11, 11), v10 = fixUUID, v11 = new Date(31202000), + v12 = (Byte.MaxValue - 10).toByte, + v13 = (Short.MaxValue - 10).toShort, o1 = Some("s"), o2 = Some(BigDecimal(1.1)), o3 = Some(true), @@ -244,6 +248,8 @@ class EncodingSpec extends Spec { v9 = LocalDate.fromMillisSinceEpoch(0), v10 = fixUUID, v11 = new Date(0), + v12 = 0, + v13 = 0, o1 = None, o2 = None, o3 = None, diff --git a/quill-cassandra/src/test/scala/io/getquill/context/cassandra/ListsEncodingSpec.scala b/quill-cassandra/src/test/scala/io/getquill/context/cassandra/ListsEncodingSpec.scala index 691e774927..3af2d2a37e 100644 --- a/quill-cassandra/src/test/scala/io/getquill/context/cassandra/ListsEncodingSpec.scala +++ b/quill-cassandra/src/test/scala/io/getquill/context/cassandra/ListsEncodingSpec.scala @@ -13,6 +13,8 @@ class ListsEncodingSpec extends CollectionsSpec { texts: List[String], decimals: List[BigDecimal], bools: List[Boolean], + bytes: List[Byte], + shorts: List[Short], ints: List[Int], longs: List[Long], floats: List[Float], @@ -21,8 +23,8 @@ class ListsEncodingSpec extends CollectionsSpec { timestamps: List[Date], uuids: List[UUID] ) - val e = ListsEntity(1, List("c"), List(BigDecimal(1.33)), List(true), List(1, 2), List(2, 3), List(1f, 3f), - List(5d), List(LocalDate.fromMillisSinceEpoch(System.currentTimeMillis())), + val e = ListsEntity(1, List("c"), List(BigDecimal(1.33)), List(true), List(0, 1), List(3, 2), List(1, 2), List(2, 3), + List(1f, 3f), List(5d), List(LocalDate.fromMillisSinceEpoch(System.currentTimeMillis())), List(new Date), List(UUID.randomUUID())) val q = quote(query[ListsEntity])