diff --git a/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/CellValue.scala b/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/CellValue.scala index f2485a45..f4fee6a9 100644 --- a/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/CellValue.scala +++ b/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/CellValue.scala @@ -26,10 +26,16 @@ trait CellValue[T] { def value: String } +private class AnnotationCellValue[T](a: Annotation[CsvValue, T]) extends CellValue[T] { + def value: String = a().value +} + +private class ConstantCellValue[T](val value: String) extends CellValue[T] + object CellValue { inline given deriveSingleton[T](using m: Mirror.ProductOf[T] { type MirroredElemTypes = EmptyTuple }): CellValue[T] = summonFrom { - case a: Annotation[CsvValue, T] => new CellValue[T] { def value: String = a().value } - case _ => new CellValue[T] { def value: String = constValue[m.MirroredLabel] } + case a: Annotation[CsvValue, T] => new AnnotationCellValue[T](a) + case _ => new ConstantCellValue[T](constValue[m.MirroredLabel]) } } diff --git a/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellDecoder.scala b/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellDecoder.scala index 038e5de2..37ba6a8b 100644 --- a/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellDecoder.scala +++ b/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellDecoder.scala @@ -25,6 +25,11 @@ import scala.deriving.Mirror trait DerivedCellDecoder[T] extends CellDecoder[T] +private class CoproductDerivedCellDecoder[T](decoders: List[DerivedCellDecoder[T]]) extends DerivedCellDecoder[T] { + def apply(in: String) = + decoders.foldRight(new DecoderError("Didn't match any value").asLeft)(_.apply(in).orElse(_)) +} + object DerivedCellDecoder { def expect[T](e: String, r: T): DerivedCellDecoder[T] = (in: String) => Either.cond(in == e, r, new DecoderError(s"Expected $e, got $in")) @@ -38,10 +43,7 @@ object DerivedCellDecoder { inline given deriveCoproduct[T](using m: Mirror.SumOf[T]): DerivedCellDecoder[T] = { val decoders: List[DerivedCellDecoder[T]] = summonAsArray[K0.LiftP[DerivedCellDecoder, m.MirroredElemTypes]].toList.asInstanceOf - new DerivedCellDecoder[T] { - def apply(in: String) = - decoders.foldRight(new DecoderError("Didn't match any value").asLeft)(_.apply(in).orElse(_)) - } + new CoproductDerivedCellDecoder[T](decoders) } inline given deriveSingleton[T](using cv: CellValue[T], m: Mirror.ProductOf[T]): DerivedCellDecoder[T] = diff --git a/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellEncoder.scala b/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellEncoder.scala index 5ee4dfa0..44b90d37 100644 --- a/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellEncoder.scala +++ b/csv/generic/shared/src/main/scala-3/fs2/data/csv/generic/internal/DerivedCellEncoder.scala @@ -31,14 +31,12 @@ object DerivedCellEncoder { ce(t.productElement(0).asInstanceOf[Tuple.Head[m.MirroredElemTypes]]) } - inline given deriveCoproduct[T](using g: K0.CoproductInstances[DerivedCellEncoder, T]): DerivedCellEncoder[T] = - new DerivedCellEncoder[T] { - def apply(elem: T) = g.fold(elem)([t <: T] => (dce: DerivedCellEncoder[t], te: t) => dce(te)) - } - - inline given deriveSingleton[T](using cv: CellValue[T]): DerivedCellEncoder[T] = - new DerivedCellEncoder[T] { - def apply(t: T) = cv.value - } + inline given deriveCoproduct[T](using g: K0.CoproductInstances[DerivedCellEncoder, T]): DerivedCellEncoder[T] with { + def apply(elem: T) = g.fold(elem)([t <: T] => (dce: DerivedCellEncoder[t], te: t) => dce(te)) + } + + inline given deriveSingleton[T](using cv: CellValue[T]): DerivedCellEncoder[T] with { + def apply(t: T) = cv.value + } }