Created
May 23, 2017 23:51
-
-
Save derekmorr/89d5a12d700bb55c29bf79a76a4394d7 to your computer and use it in GitHub Desktop.
Play 2.x PathBinable for Refined Types
This file contains hidden or 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 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