Created
October 11, 2019 20:19
-
-
Save nachinius/aacefa719bf7097f0644563952e5962a to your computer and use it in GitHub Desktop.
play binder for BigDecimal
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
// 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