Skip to content

Instantly share code, notes, and snippets.

Peter realpeterz

  • Mutual Mobile
  • Austin, TX
Block or report user

Report or block realpeterz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@realpeterz
realpeterz / SingleFieldFormat.scala
Last active Mar 12, 2018
Play Json Format helper for value classes
View SingleFieldFormat.scala
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, Json}
object SingleFieldFormat {
def format[A, B](f: A => B, g: B => A)(implicit fa: Format[A]) = fa.inmap(f, g)
// expanded:
// syntax.toInvariantFunctorOps(fa)(Format.invariantFunctorFormat).inmap(f, g)
// inmap's underlying:
// Format[B](fa.map(f), Writes[B](b => fa.writes(g(b))))
}
@realpeterz
realpeterz / AdtFormat.scala
Created Feb 14, 2018
Generic Play Json ADT Format (Writes & Reads)
View AdtFormat.scala
import play.api.libs.json._
import scala.reflect.ClassTag
trait AdtFormat[A <: Product with Serializable] {
type ReadByTypeFunc[C <: A] = PartialFunction[String, JsResult[C]]
type ReadsFunc[B <: A] = JsValue => JsResult[B]
type WriteFunc = PartialFunction[A, JsValue]
You can’t perform that action at this time.