Skip to content

Commit

Permalink
Relete covariance from Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
urmaul committed May 8, 2024
1 parent 3c4938c commit 273562f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
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 @@ -31,7 +31,7 @@ import scala.util.Try
/** Provides reading of a generated Protocol Buffers model into a business model.
*/
@implicitNotFound("No Protocol Buffers mapper from type ${P} to ${M} was found. Try to implement an implicit Reader for this type.")
trait Reader[-P, +M] {
trait Reader[P, M] {

/** Returns the read business model or an error message.
*/
Expand Down Expand Up @@ -150,6 +150,13 @@ object Reader extends LowPriorityReads {
PbSuccess((Duration(protobuf.seconds, SECONDS) + Duration(protobuf.nanos.toLong, NANOSECONDS)).toCoarsest)
}

/** Transforms a ScalaPB duration into a Scala concurrent duration.
*/
implicit object DurationReader extends Reader[PBDuration, Duration] {
def read(protobuf: PBDuration): PbResult[FiniteDuration] =
PbSuccess((Duration(protobuf.seconds, SECONDS) + Duration(protobuf.nanos.toLong, NANOSECONDS)).toCoarsest)
}

/** Transforms a string into a UUID.
*/
implicit object UUIDReader extends Reader[String, UUID] {
Expand Down
13 changes: 7 additions & 6 deletions src/main/scala/io/moia/protos/teleproto/WriterImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ class WriterImpl(val c: blackbox.Context) extends FormatImpl {
// look for an implicit writer
val writerType = appliedType(c.weakTypeTag[Writer[_, _]].tpe, modelType, protobufType)

val existingWriter = try {
c.inferImplicitValue(writerType)
} catch {
// Return EmptyTree in case of errors
case _: Throwable => EmptyTree
}
val existingWriter =
try {
c.inferImplicitValue(writerType)
} catch {
// Return EmptyTree in case of errors
case _: Throwable => EmptyTree
}

// "ask" for the implicit writer or use the found one
def ask: Compiled = (compileInner(q"implicitly[$writerType]"), Compatibility.full)
Expand Down

0 comments on commit 273562f

Please sign in to comment.