Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my example won't compile #34

Closed
jarlah opened this issue Jun 28, 2016 · 2 comments
Closed

my example won't compile #34

jarlah opened this issue Jun 28, 2016 · 2 comments

Comments

@jarlah
Copy link

jarlah commented Jun 28, 2016

The example below won't compile. Fails with this message:

[error]   could not find Lazy implicit value of type julienrf.json.derived.DerivedReads[A]
[error]   implicit lazy val format: OFormat[Event] = flat.oformat((__ \ "type").format[String])
[error]                                                          ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] application -

! @70h4nj2oc - Internal server error, for (POST) [/] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[could not find Lazy implicit value of type julienrf.json.derived.DerivedReads[A]]
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
    at scala.Option.map(Option.scala:145)
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49)
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44)
    at scala.Option.map(Option.scala:145)
    at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44)
    at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40)
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)

Do I have to implement standard Json.format[Event] formatter in addition?

import play.api.mvc.{Action, BodyParsers, Controller}
import play.api.libs.json._
import julienrf.json.derived._
import scala.concurrent.Future
import Event._

class EventController extends Controller {

  def insert() = Action.async(BodyParsers.parse.json) { request =>
    request.body.validate[Event].asEither match {
      case Right(event) => println(event)
      case Left(error) => println(error.mkString)
    }
    Future.successful(Ok)
  }
}

sealed trait Event {
  val id: Option[Long]
  val links: Option[Seq[String]]
  val note: Option[String]
}

object Event {
  implicit lazy val format: OFormat[Event] = flat.oformat((__ \ "type").format[String])
}

case class Move(
  id: Option[Long],
  links: Option[Seq[String]],
  note: Option[String]
) extends Event

case class Control(
  id: Option[Long],
  links: Option[Seq[String]],
  note: Option[String]
) extends Event
@julienrf
Copy link
Owner

Hi, that’s a known limitation of knownDirectSubclasses: you have to move the Event object after all the class definitions that extend Event.

@jarlah
Copy link
Author

jarlah commented Jun 28, 2016

thanks so much :)

@jarlah jarlah closed this as completed Jun 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants