Skip to content

Commit

Permalink
Add Reader & Writer for UUID (#114)
Browse files Browse the repository at this point in the history
* Add UUIDWriter & UUIDReader

* Bump version
  • Loading branch information
saeltz authored Mar 25, 2021
1 parent 0cd4724 commit ec510f5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ lazy val `teleproto` =
.settings(Project.inConfig(Test)(sbtprotoc.ProtocPlugin.protobufConfigSettings): _*)
.settings(
name := "teleproto",
version := "1.11.0",
version := "1.12.0",
versionScheme := Some("early-semver"),
libraryDependencies ++= Seq(
library.scalaPB % "protobuf;compile",
library.scalaPBJson % Compile,
Expand Down
9 changes: 8 additions & 1 deletion src/main/scala/io/moia/protos/teleproto/Reader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package io.moia.protos.teleproto

import java.time.{Instant, LocalTime}

import com.google.protobuf.duration.{Duration => PBDuration}
import com.google.protobuf.timestamp.Timestamp
import scalapb.GeneratedMessage

import java.util.UUID
import scala.annotation.implicitNotFound
import scala.collection.compat._
import scala.collection.immutable.TreeMap
Expand Down Expand Up @@ -160,6 +160,13 @@ object Reader extends LowPriorityReads {
PbSuccess((Duration(protobuf.seconds, SECONDS) + Duration(protobuf.nanos.toLong, NANOSECONDS)).toCoarsest)
}

/**
* Transforms a string into a UUID.
*/
implicit object UUIDReader extends Reader[String, UUID] {
def read(uuid: String): PbResult[UUID] = Try(PbSuccess(UUID.fromString(uuid))).getOrElse(PbFailure("Value must be a UUID."))
}

/**
* Transforms a PB timestamp as fixed point in time (with milliseconds precision) into a Scala concurrent deadline.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/scala/io/moia/protos/teleproto/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.google.protobuf.duration.{Duration => PBDuration}
import com.google.protobuf.timestamp.Timestamp

import java.time.{Instant, LocalTime}
import java.util.UUID
import scala.annotation.implicitNotFound
import scala.collection.compat._
import scala.collection.immutable.TreeMap
Expand Down Expand Up @@ -129,6 +130,13 @@ object Writer extends LowPriorityWrites {
PBDuration(duration.toSeconds, (duration.toNanos % 1000000000).toInt)
}

/**
* Writes a UUID as string.
*/
implicit object UUIDWriter extends Writer[UUID, String] {
def write(uuid: UUID): String = uuid.toString
}

/**
* Writes a Scala deadline into a ScalaPB Timestamp as fixed point in time.
*
Expand Down

0 comments on commit ec510f5

Please sign in to comment.