Skip to content

Commit

Permalink
Merge pull request #1049 from mentegy/fix-1009
Browse files Browse the repository at this point in the history
`quill-cassandra`: Add encoding for `Byte` and `Short`
  • Loading branch information
fwbrasil authored Apr 9, 2018
2 parents 024aae8 + 6a36c7e commit 9f179e4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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]
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions quill-cassandra/src/test/cql/cassandra-schema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ CREATE TABLE EncodingTestEntity(
v9 DATE,
v10 UUID,
v11 TIMESTAMP,
v12 TINYINT,
v13 SMALLINT,
o1 VARCHAR,
o2 DECIMAL,
o3 BOOLEAN,
Expand Down Expand Up @@ -92,6 +94,8 @@ CREATE TABLE ListsEntity(
texts LIST<VARCHAR>,
decimals LIST<DECIMAL>,
bools LIST<BOOLEAN>,
bytes LIST<TINYINT>,
shorts LIST<SMALLINT>,
ints LIST<INT>,
longs LIST<BIGINT>,
floats LIST<FLOAT>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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])

Expand Down

0 comments on commit 9f179e4

Please sign in to comment.