Skip to content

Commit

Permalink
Avoid redeclaring inlined classes
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Oct 21, 2024
1 parent 8aaafe5 commit 04fcad9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit 04fcad9

Please sign in to comment.