Skip to content

Instantly share code, notes, and snippets.

@derekmorr
Created May 23, 2017 23:51
Show Gist options
  • Save derekmorr/89d5a12d700bb55c29bf79a76a4394d7 to your computer and use it in GitHub Desktop.
Save derekmorr/89d5a12d700bb55c29bf79a76a4394d7 to your computer and use it in GitHub Desktop.
Play 2.x PathBinable for Refined Types
package controllers
import scala.language.higherKinds
import eu.timepit.refined.api.{RefType, Validate}
import play.api.mvc.PathBindable
object RefinedPathBindable {
implicit def refinedPathBindable[R[_, _], T, P](implicit
baseTypeBinder: PathBindable[T],
refType: RefType[R],
validate: Validate[T, P]
): PathBindable[R[T, P]] = new PathBindable[R[T, P]] {
override def bind(key: String, value: String): Either[String, R[T, P]] = {
baseTypeBinder.bind(key, value).right.flatMap { baseValue =>
refType.refine[P](baseValue)
}
}
override def unbind(key: String, value: R[T, P]): String = {
baseTypeBinder.unbind(key, refType.unwrap(value))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment