amnaredo / test

0 stars 0 forks source link

Compile error: scala.ScalaReflectionException: type A is not a class #125

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

I'm trying to create a body parser in play framework that will read/unpickle the request body. This code will not compile and I can't see why:

trait ProfileController extends Controller {

  def pickled[A](maxLength: Int): BodyParser[A] = BodyParser("pickled, maxLength=" + maxLength) { request =>

    import Execution.Implicits.trampoline
    import upickle._

    Traversable.takeUpTo[Array[Byte]](maxLength)
      .transform(Iteratee.consume[Array[Byte]]().map(c => read[A](c.toString)))
      .flatMap(Iteratee.eofOrElse(Results.EntityTooLarge))
  }

  def addUser = Action(pickled[Profile](10000)) { req =>
    Ok("Got a user: " + req.body)
  }

}

object ProfileController extends ProfileController

The error is:

[error] scala.ScalaReflectionException: type A is not a class [error] at scala.reflect.api.Symbols$SymbolApi$class.asClass(Symbols.scala:275) [error] at scala.reflect.internal.Symbols$SymbolContextApiImpl.asClass(Symbols.scala:84) [error] at upickle.Macros$.picklerFor(Macros.scala:75) [error] at upickle.Macros$.macroRImpl(Macros.scala:35) [error] .transform(Iteratee.consume[Array[Byte]]().map(c => readA)) [error] ^ [error] /Users/janne/projects/private/gb/play/app/controllers/ProfileController.scala:16: missing arguments for method apply in trait EofOrElse; [error] follow this method with `_' if you want to treat it as a partially applied function [error] .flatMap(Iteratee.eofOrElse(Results.EntityTooLarge)) [error] ^ [error] two errors found error Compilation failed

ID: 85 Original Author: jn73

amnaredo commented 3 years ago

I got it to work by pulling the implicit Reader for A into scope by declaring the function as:

def pickled[A : Reader](maxLength: Int): BodyParser[A] = BodyParser("pickled, maxLength=" + maxLength) { request =>

..i also changed c.toString to new String(c) so that the byte array would correctly be converted to a string..

So, now it works :)

Original Author: jn73