Skip to content

Commit

Permalink
Derive enum readers explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
urmaul committed May 17, 2024
1 parent 780e2d8 commit 49a38a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main/scala/io/moia/protos/teleproto/WriterImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ class WriterImpl(val c: blackbox.Context) extends FormatImpl {
// val result = compileInner(implicitValue)
// (result, compatibility)
// } else
if (checkEnumerationTypes(protobufType, modelType)) {
val (implicitValue, compatibility) = compileEnumerationMapping(protobufType, modelType)
val result = compileInner(implicitValue)
(result, compatibility)
} else if (checkHierarchyTypes(protobufType, modelType)) {
val (implicitValue, compatibility) = compileTraitMapping(protobufType, modelType)
val result = compileInner(implicitValue)
(result, compatibility)
} else {
// if (checkEnumerationTypes(protobufType, modelType)) {
// val (implicitValue, compatibility) = compileEnumerationMapping(protobufType, modelType)
// val result = compileInner(implicitValue)
// (result, compatibility)
// } else if (checkHierarchyTypes(protobufType, modelType)) {
// val (implicitValue, compatibility) = compileTraitMapping(protobufType, modelType)
// val result = compileInner(implicitValue)
// (result, compatibility)
// } else
{
// look for an implicit transformer
val transformerType = appliedType(c.weakTypeTag[chimney.Transformer[_, _]].tpe, modelType, protobufType)
val existingTransformer =
Expand All @@ -113,15 +114,15 @@ class WriterImpl(val c: blackbox.Context) extends FormatImpl {
def askTransformer = if (existingTransformer != EmptyTree) {
q"implicitly[$transformerType]"
} else {
q"import io.moia.protos.teleproto.Writer._; $transformerObj.derive[$modelType, $protobufType]"
// q"import io.moia.protos.teleproto.Writer._; $transformerObj.derive[$modelType, $protobufType]"
q"import io.moia.protos.teleproto.Writer._; $transformerObj.define[$modelType, $protobufType].enableDefaultValues.buildTransformer"
}

def writerFromTransformer: Compiled =
(compileInner(q"$writerObj.fromTransformer[$modelType, $protobufType]($askTransformer)"), Compatibility.full)

writerFromTransformer // use the available transformer implicit
}
else
} else
ask // use the available implicit
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import scala.util.Success
class ProtocolBuffersRoundTripTest extends UnitTest with ScalaCheckPropertyChecks {
import ProtocolBuffersRoundTripTest._

// TODO: remove?
implicit val colorReader: Reader[food.Meal.Color, Color] = ProtocolBuffers.reader[food.Meal.Color, Color]
implicit val colorWriter: Writer[Color, food.Meal.Color] = ProtocolBuffers.writer[Color, food.Meal.Color]

implicit val reader: Reader[food.Meal, Meal] = ProtocolBuffers.reader[food.Meal, Meal]
implicit val writer: Writer[Meal, food.Meal] = ProtocolBuffers.writer[Meal, food.Meal]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ object Protobuf {

implicit val subReader: Reader[SubProtobuf, SubModel] = ProtocolBuffers.reader[SubProtobuf, SubModel]

// TODO: remove?
implicit val enumReader: Reader[ProtobufEnum, ModelEnum] = ProtocolBuffers.reader[ProtobufEnum, ModelEnum]
implicit val enumWriter: Writer[ModelEnum, ProtobufEnum] = ProtocolBuffers.writer[ModelEnum, ProtobufEnum]

val reader: Reader[Protobuf, Model] = ProtocolBuffers.reader[Protobuf, Model]

@backward("2e0e9b")
Expand Down

0 comments on commit 49a38a8

Please sign in to comment.