From a2ecb800f00e7aab2689aea181feb75d58239717 Mon Sep 17 00:00:00 2001 From: Gregg Hernandez Date: Thu, 23 Feb 2017 09:18:17 -0700 Subject: [PATCH] Fixes UUID for postgres in ColReader --- .../com/lucidchart/relate/ColReader.scala | 8 ++------ relate/src/test/scala/ColReaderTest.scala | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/relate/src/main/scala/com/lucidchart/relate/ColReader.scala b/relate/src/main/scala/com/lucidchart/relate/ColReader.scala index f48f755..af4d48d 100644 --- a/relate/src/main/scala/com/lucidchart/relate/ColReader.scala +++ b/relate/src/main/scala/com/lucidchart/relate/ColReader.scala @@ -60,12 +60,8 @@ object ColReader { implicit val longReader: ColReader[Long] = optReader((col, rs) => rs.getLong(col)) implicit val shortReader: ColReader[Short] = optReader((col, rs) => rs.getShort(col)) implicit val stringReader: ColReader[String] = optReader((col, rs) => rs.getString(col)) - implicit val uuidReader: ColReader[UUID] = byteArrayReader.map { bytes => - require(bytes.length == 16) - val bb = ByteBuffer.wrap(bytes) - val high = bb.getLong - val low = bb.getLong - new UUID(high, low) + implicit val uuidReader: ColReader[UUID] = ColReader[UUID] { (col, row) => + row.uuidOption(col) } def enumReader[A <: Enumeration](e: A): ColReader[e.Value] = { diff --git a/relate/src/test/scala/ColReaderTest.scala b/relate/src/test/scala/ColReaderTest.scala index f0a10e0..e53a422 100644 --- a/relate/src/test/scala/ColReaderTest.scala +++ b/relate/src/test/scala/ColReaderTest.scala @@ -51,7 +51,7 @@ object RecordA extends Mockito { rs.getLong("long") returns 100L rs.getShort("short") returns (5: Short) rs.getString("str") returns "hello" - rs.getBytes("uuid") returns Array[Byte](1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) + rs.getObject("uuid") returns Array[Byte](1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) rs.getInt("thing") returns 1 SqlRow(rs) } @@ -185,4 +185,22 @@ class ColReaderTest extends Specification with Mockito { ) } } + + "uuidReader" should { + "parse a byte array" in { + val rs = mock[java.sql.ResultSet] + val row = SqlRow(rs) + rs.getObject("col") returns Array[Byte]('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f') + + ColReader.uuidReader.read("col", row) mustEqual Some(new UUID(3472611983179986487L, 4051376414998685030L)) + } + + "parse a uuid" in { + val rs = mock[java.sql.ResultSet] + val row = SqlRow(rs) + rs.getObject("col") returns new UUID(3472611983179986487L, 4051376414998685030L) + + ColReader.uuidReader.read("col", row) mustEqual Some(new UUID(3472611983179986487L, 4051376414998685030L)) + } + } }