Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a dedicated typeclass to guide coproduct induction #924

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions core/src/main/scala/io/finch/ToResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,29 @@ object ToResponse extends HighPriorityToResponseInstances {
}
}

implicit def cnilToResponse[CT <: String]: Aux[CNil, CT] =
instance((_, _) => Response(Version.Http10, Status.NotFound))

implicit def coproductToResponse[H, T <: Coproduct, CT <: String](implicit
trH: ToResponse.Aux[H, CT],
trT: ToResponse.Aux[T, CT]
): Aux[H :+: T, CT] = instance {
case (Inl(h), cs) => trH(h, cs)
case (Inr(t), cs) => trT(t, cs)
trait FromCoproduct[C <: Coproduct] extends ToResponse[C]

object FromCoproduct {
type Aux[C <: Coproduct, CT] = FromCoproduct[C] { type ContentType = CT }

def instance[C <: Coproduct, CT <: String](fn: (C, Charset) => Response): Aux[C, CT] =
new FromCoproduct[C] {
type ContentType = CT
def apply(c: C, cs: Charset): Response = fn(c, cs)
}

implicit def cnilToResponse[CT <: String]: Aux[CNil, CT] =
instance((_, _) => Response(Version.Http10, Status.NotFound))

implicit def cconsToResponse[L, R <: Coproduct, CT <: String](
implicit trL: ToResponse.Aux[L, CT], fcR: Aux[R, CT]
): Aux[L :+: R, CT] = instance {
case (Inl(h), cs) => trL(h, cs)
case (Inr(t), cs) => fcR(t, cs)
}
}

implicit def coproductToResponse[C <: Coproduct, CT <: String](
implicit fc: FromCoproduct.Aux[C, CT]
): Aux[C, CT] = fc
}