Created
May 31, 2023 20:31
-
-
Save megafarad/5f67096e0cc7e0119aa03a06964a46ef to your computer and use it in GitHub Desktop.
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
package com.megafarad.omoiomoi.binders | |
import play.api.mvc.QueryStringBindable | |
import java.time.format.DateTimeParseException | |
import java.time.zone.ZoneRulesException | |
import java.time.{LocalDate, ZoneId} | |
object CustomBinders { | |
implicit def localDateBinder(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[LocalDate] = new QueryStringBindable[LocalDate] { | |
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, LocalDate]] = { | |
params.get(key).map { | |
dateString => | |
try { | |
Right(LocalDate.parse(dateString.head)) | |
} catch { | |
case _: DateTimeParseException => Left("Not a valid date") | |
} | |
} | |
} | |
override def unbind(key: String, value: LocalDate): String = stringBinder.unbind(key, value.toString) | |
} | |
implicit def zoneIdBinder(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[ZoneId] = new QueryStringBindable[ZoneId] { | |
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, ZoneId]] = { | |
params.get(key).map { | |
dateString => try { | |
Right(ZoneId.of(dateString.head)) | |
} catch { | |
case _: DateTimeParseException => Left("Not a valid ZoneId") | |
case _: ZoneRulesException => Left("ZoneId not found") | |
} | |
} | |
} | |
override def unbind(key: String, value: ZoneId): String = stringBinder.unbind(key, value.toString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment