Skip to content

Instantly share code, notes, and snippets.

@ubourdon
Last active September 3, 2020 08:50
Show Gist options
  • Save ubourdon/6a4b796f45c461ef4b5d033d1b2e0fd8 to your computer and use it in GitHub Desktop.
Save ubourdon/6a4b796f45c461ef4b5d033d1b2e0fd8 to your computer and use it in GitHub Desktop.
Erreur chelou de play-json qui ne trouve pas un formateur pourtant existant.
import domain.common.collection.{NonEmptyListMaxSizeExceeded, NonEmptyListMaxSizeNewType}
import domain.common.collection.NonEmptyListMaxSizeNewType.NEL_MaxSize_2
import play.api.libs.json._
object NonEmptyListMaxSizeJsonFmt {
import NonEmptyListJsonFmt.nonEmptyListJsonFmt
implicit def nonemptyListMaxSize_2_JsonFmt[A](implicit fmt: Format[A]) = new Format[NEL_MaxSize_2.List_[A]] {
override def writes(o: NonEmptyListMaxSizeNewType.NEL_MaxSize_2.List_[A]): JsValue = nonEmptyListJsonFmt[A].writes(NEL_MaxSize_2.rawValue(o))
override def reads(json: JsValue): JsResult[NonEmptyListMaxSizeNewType.NEL_MaxSize_2.List_[A]] = nonEmptyListJsonFmt[A].reads(json).flatMap { nel =>
NEL_MaxSize_2.apply(nel).fold(
error => error match {
case NonEmptyListMaxSizeExceeded(maxSize, size) => JsError(s"list has size of $size that exceed the fixed max size of $maxSize")
},
nel => JsSuccess(nel)
)
}
}
case class Toto(s: String)
case class Titi(t: NEL_MaxSize_2.List_[Toto])
implicit val toto = Json.format[Toto]
implicit val titi = Json.format[Titi]
/**
No instance of play.api.libs.json.Format is available for domain.common.collection.NonEmptyListMaxSizeBuilder.NonEmptyListMaxSizeImpl[service.binding.json.NonEmptyListMaxSizeJsonFmt.Toto] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)
[error] implicit val titi = Json.format[Titi]
Et j'ai ce message d'erreur.
*/
}
import scalaz.Scalaz.ToEitherOps
import scalaz.{NonEmptyList, \/}
import service.utils.collection.NEL
object NonEmptyListMaxSizeNewType {
object NEL_MaxSize_2 extends NonEmptyListMaxSizeBuilder(2) {
//override def maxSize: Int = 2
}
}
sealed trait NonEmptyListMaxSizeError
case class NonEmptyListMaxSizeExceeded(maxSize: Int, size: Int) extends NonEmptyListMaxSizeError
class NonEmptyListMaxSizeBuilder(maxSize: Int) {
private[NonEmptyListMaxSizeBuilder] case class NEL_Impl[+A](h: A, t: List[A])
case class NonEmptyListMaxSizeImpl[+A](rawValue: NEL_Impl[A])
type List_[+A] = NonEmptyListMaxSizeImpl[A]
def apply[A](rawValue: NonEmptyList[A]): NonEmptyListMaxSizeError \/ List_[A] = {
if(rawValue.size <= maxSize) buildType(rawValue).right
else NonEmptyListMaxSizeExceeded(maxSize, rawValue.size).left
}
private def buildType[A](nel: NonEmptyList[A]) = NonEmptyListMaxSizeImpl(NEL_Impl(nel.head, nel.tail.toList))
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment