All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.maif.izanami.web.FeatureContextPath.scala Maven / Gradle / Ivy

package fr.maif.izanami.web

import play.api.mvc.{PathBindable, QueryStringBindable}

case class FeatureContextPath(elements: Seq[String] = Seq())

object FeatureContextPath {
  implicit def pathBinder(implicit strBinder: PathBindable[String]): PathBindable[FeatureContextPath] =
    new PathBindable[FeatureContextPath] {
      override def bind(key: String, value: String): Either[String, FeatureContextPath] = {
        strBinder
          .bind(key, value)
          .map(str => {
            FeatureContextPath(str.split("/").toSeq.filter(s => s.nonEmpty))
          })
      }
      override def unbind(key: String, path: FeatureContextPath): String = {
        path.elements.mkString("/")
      }
    }

  implicit def queryStringBindable(implicit
      seqBinder: QueryStringBindable[Seq[String]]
  ): QueryStringBindable[FeatureContextPath] =
    new QueryStringBindable[FeatureContextPath] {
      override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, FeatureContextPath]] = {
        for (eitherContext <- seqBinder.bind("context", params)) yield {
          Right(
            FeatureContextPath(elements =
              eitherContext.map(seq => seq.filter(str => str.nonEmpty).flatMap(str => str.split("/").toSeq.filter(s => s.nonEmpty))).getOrElse(Seq())
            )
          )
        }
      }

      override def unbind(key: String, request: FeatureContextPath): String = {
        throw new NotImplementedError("")
      }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy