Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created September 28, 2020 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubukoz/91ff72ee5764b6b42d4b38df2562d47b to your computer and use it in GitHub Desktop.
Save kubukoz/91ff72ee5764b6b42d4b38df2562d47b to your computer and use it in GitHub Desktop.
ciris ConfigValue derivation for case classes
object magic {
trait Derived[A] {
type Out
def out: Out
}
object Derived {
type Aux[A, B] = Derived[A] { type Out = B }
import shapeless._
object parallelFolder extends Poly2 {
implicit def parTuple[F[_]: NonEmptyParallel, X, Y <: HList]: Case.Aux[F[X], F[Y], F[X :: Y]] =
at[F[X], F[Y]]((_, _).parMapN(_ :: _))
}
implicit def autoMagic[Type, Repr <: HList, Lifted <: HList](
implicit gen: Generic.Aux[Type, Repr],
lift: LiftAll.Aux[ConfigValue, Repr, Lifted],
folder: RightFolder.Aux[Lifted, ConfigValue[HNil], parallelFolder.type, ConfigValue[Repr]]
): Derived.Aux[Type, ConfigValue[Type]] =
new Derived[Type] {
type Out = ConfigValue[Type]
val out: Out = lift.instances.foldRight(ConfigValue.default(HNil: HNil))(parallelFolder).map(gen.from)
}
}
def genericConfigValue[Type](implicit m: Derived[Type]): m.Out = m.out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment