Skip to content

Commit

Permalink
Manual implementation for enum writers
Browse files Browse the repository at this point in the history
  • Loading branch information
urmaul committed Aug 23, 2024
1 parent 19af3c3 commit c2f647f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.moia.protos.teleproto
import io.moia.food.food
import org.scalacheck.Gen
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import io.moia.protos.teleproto.BaseTransformers._ // TODO: remove
//import io.moia.protos.teleproto.BaseTransformers._ // TODO: remove

import scala.util.Success

Expand All @@ -12,9 +12,24 @@ class ProtocolBuffersRoundTripTest extends UnitTest with ScalaCheckPropertyCheck

// 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 colorWriter: Writer[Color, food.Meal.Color] = ProtocolBuffers.writer[Color, food.Meal.Color]
// TODO: derive automatically
implicit val colorWriter: Writer[Color, food.Meal.Color] = new Writer[Color, food.Meal.Color] {

/** Returns the written Protocol Buffer object.
*/
override def write(model: Color): food.Meal.Color = model match {
case Color.Red => food.Meal.Color.COLOR_RED
case Color.orange => food.Meal.Color.COLOR_ORANGE
case Color.Yellow => food.Meal.Color.COLOR_YELLOW
case Color.pink => food.Meal.Color.COLOR_PINK
case Color.Blue => food.Meal.Color.COLOR_BLUE
}
}

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

val version = 1
Expand Down Expand Up @@ -48,6 +63,7 @@ class ProtocolBuffersRoundTripTest extends UnitTest with ScalaCheckPropertyCheck
"ProtocolBuffers" should {
"generate writer and reader that round trip successfully" in {
forAll(mealGen) { meal =>
// println(writer.write(meal))
reader.read(writer.write(meal)) shouldBe PbSuccess(meal)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,21 @@ 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 enumReader: Reader[ProtobufEnum, ModelEnum] = ProtocolBuffers.reader[ProtobufEnum, ModelEnum]
// implicit val enumWriter: Writer[ModelEnum, ProtobufEnum] = ProtocolBuffers.writer[ModelEnum, ProtobufEnum]

// TODO: implement derivation
implicit val enumWriter: Writer[ModelEnum, ProtobufEnum] = new Writer[ModelEnum, ProtobufEnum] {

/** Returns the written Protocol Buffer object.
*/
override def write(model: ModelEnum): ProtobufEnum = model match {
case ModelEnum.First_Case => ProtobufEnum.FirstCase
case ModelEnum.SecondCase => ProtobufEnum.SECOND_CASE
case ModelEnum.THIRD_CASE => ProtobufEnum.Third_Case
}
}

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

@backward("2e0e9b")
Expand Down

0 comments on commit c2f647f

Please sign in to comment.