Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created April 12, 2021 16:49
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/acdd21b379a6a4fa70ffe73fd76232f5 to your computer and use it in GitHub Desktop.
Save kubukoz/acdd21b379a6a4fa70ffe73fd76232f5 to your computer and use it in GitHub Desktop.
Play JSON format syntax for HLists
import shapeless.::
object Extensions extends LowPriorityExtensions {
implicit class NotHListOWritesConcat[A](right: OWrites[A])(implicit notHList: shapeless.Refute[A <:< HList]) {
//noinspection ScalaStyle
def ~::[B](head: OWrites[B]): OWrites[B :: A :: HNil] = (head ~ right) (hlist => (hlist.head, hlist.tail.head))
}
implicit class NotHListReadsConcat[A](right: Reads[A])(implicit notHList: shapeless.Refute[A <:< HList]) {
//noinspection ScalaStyle
def ~::[B](head: Reads[B]): Reads[B :: A :: HNil] = (head ~ right) ((b, a) => b :: a :: HNil)
}
}
trait LowPriorityExtensions {
implicit class HListOWritesConcat[A <: HList](right: OWrites[A]) {
//noinspection ScalaStyle
def ~::[B](head: OWrites[B]): OWrites[B :: A] = (head ~ right) (hlist => (hlist.head, hlist.tail))
}
implicit class HListReadsConcat[A <: HList](right: Reads[A]) {
//noinspection ScalaStyle
def ~::[B](head: Reads[B]): Reads[B :: A] = (head ~ right) ((b, a) => b :: a)
}
}
import Extensions._
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment