From 49a38a80dd7c9de301d81c4af5997a976f611159 Mon Sep 17 00:00:00 2001 From: Bogdan Kolbik Date: Fri, 17 May 2024 11:32:24 +0200 Subject: [PATCH] Derive enum readers explicitly --- .../io/moia/protos/teleproto/WriterImpl.scala | 25 ++++++++++--------- .../ProtocolBuffersRoundTripTest.scala | 4 +++ .../teleproto/ProtocolBuffersTest.scala | 4 +++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/scala/io/moia/protos/teleproto/WriterImpl.scala b/src/main/scala/io/moia/protos/teleproto/WriterImpl.scala index f98f517..fd76829 100644 --- a/src/main/scala/io/moia/protos/teleproto/WriterImpl.scala +++ b/src/main/scala/io/moia/protos/teleproto/WriterImpl.scala @@ -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 = @@ -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 } diff --git a/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersRoundTripTest.scala b/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersRoundTripTest.scala index dd0d91c..8cd0406 100644 --- a/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersRoundTripTest.scala +++ b/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersRoundTripTest.scala @@ -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] diff --git a/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersTest.scala b/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersTest.scala index a283665..8650129 100644 --- a/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersTest.scala +++ b/src/test/scala/io/moia/protos/teleproto/ProtocolBuffersTest.scala @@ -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")