Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions modules/core/shared/src/main/scala/codec/UuidCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ package codec
import cats.syntax.all._
import java.util.UUID
import skunk.data.Type
import skunk.data.Arr

trait UuidCodec {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is now slightly wrong because we have multiple codecs, but it doesn't seem worth fixing while keeping binary compatibility.


val uuid: Codec[UUID] =
Codec.simple[UUID](
u => u.toString,
s => Either.catchOnly[IllegalArgumentException](UUID.fromString(s)).leftMap(_.getMessage),
Type.uuid
)
Codec.simple[UUID](_.toString, codec.uuid.parse, Type.uuid)

def _uuid: Codec[Arr[UUID]] = codec.uuid._uuidImpl

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, we can't add values directly to trait lest MiMa complains. Does anyone have a better way to do this?

}

object uuid extends UuidCodec
object uuid extends UuidCodec {
private[codec] def parse(s: String): Either[String, UUID] =
Either.catchOnly[IllegalArgumentException](UUID.fromString(s)).leftMap(_.getMessage)

private[codec] val _uuidImpl: Codec[Arr[UUID]] =
Codec.array[UUID](_.toString, parse, Type._uuid)
}
3 changes: 3 additions & 0 deletions modules/tests/shared/src/test/scala/codec/UuidCodecTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package tests
package codec
import skunk.codec.all._
import skunk.data.Arr
import java.util.UUID

/** Test that we can round=trip values via codecs. */
Expand All @@ -17,6 +18,8 @@ class UuidCodecTest extends CodecTest {
roundtripTest(uuid.opt)(Some(u1), Some(u2), None)
decodeFailureTest(time, List("x"))

val Some(arr) = Arr(u1, u2).reshape(2,1): @unchecked
roundtripTest(_uuid)(Arr.empty, arr)
}


Loading