Created
September 28, 2020 20:51
-
-
Save kubukoz/91ff72ee5764b6b42d4b38df2562d47b to your computer and use it in GitHub Desktop.
ciris ConfigValue derivation for case classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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