Skip to content

Instantly share code, notes, and snippets.

@nachinius
Created October 11, 2019 20:19
Show Gist options
  • Save nachinius/aacefa719bf7097f0644563952e5962a to your computer and use it in GitHub Desktop.
Save nachinius/aacefa719bf7097f0644563952e5962a to your computer and use it in GitHub Desktop.
play binder for BigDecimal
// in buid.sbt """routesImport ++= Seq("com.nachinius.controllers.Binders._" """
package com.nachinius.controllers
object Binders {
implicit def BigDecimalQueryStringBindable = new QueryStringBindable[BigDecimal] {
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, BigDecimal]] = {
params.get(key).flatMap(_.headOption).map { value =>
Try { BigDecimal(value) } match {
case Success(value) => Right(value)
case Failure(exception) => Left(exception.getMessage)
}
}
}
override def unbind(key: String, value: BigDecimal): String = {
s"%key=${value.bigDecimal.stripTrailingZeros.toPlainString}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment