Skip to content
This repository has been archived by the owner on May 21, 2018. It is now read-only.

Cannot auto-extract case class #228

Open
byF opened this issue Jul 25, 2016 · 5 comments
Open

Cannot auto-extract case class #228

byF opened this issue Jul 25, 2016 · 5 comments
Assignees

Comments

@byF
Copy link

byF commented Jul 25, 2016

Environment:

  • Scala 2.10.5
  • Rapture both 2.0.0-M5 and 2.0.0-M6
  • Spray JSON 1.3.2
package com.test

import org.scalatest.{FlatSpec, ShouldMatchers}
import rapture.json._
import rapture.json.jsonBackends.spray._

class RaptureTest extends FlatSpec with ShouldMatchers {

  case class CC(s: String, t: String)

  "Case class" should "be extracted from JSON" in {
    val json = json"""{"s":"abc","t":"def"}"""
    val cc = json.as[CC]
    cc should be CC("abc","def")
  }

}

This fails with

Error:(13, 21) cannot extract type RaptureTest.this.CC from rapture.json.Json.
    val cc = json.as[CC]

We've spent the whole afternoon on this, not sure where the problem lays.

@propensive
Copy link
Owner

It's interesting that when copy/pasting this into the REPL I get:

<console>:25: error: value CC is not a member of RaptureTest.this.ResultOfBeWordForAny[RaptureTest.this.CC]
    cc should be CC("abc","def")
                 ^

@propensive propensive self-assigned this Jul 25, 2016
@propensive
Copy link
Owner

Note that the following works fine:

scala> import rapture.json._
import rapture.json._

scala> import rapture.json.jsonBackends.spray._
import rapture.json.jsonBackends.spray._

scala> case class CC(s: String, t: String)
defined class CC

scala> val json = json"""{"s":"abc","t":"def"}"""
json: rapture.json.Json = json"""{"s":"abc","t":"def"}"""

scala> val cc = json.as[CC]
cc: CC = CC(abc,def)

@propensive
Copy link
Owner

I've noticed that if I change the test to this, it compiles:

package com.test

import org.scalatest.{FlatSpec, ShouldMatchers}
import rapture.json._
import rapture.json.jsonBackends.spray._

class RaptureTest extends FlatSpec with ShouldMatchers {
  case class CC(s: String, t: String)
  "Case class" should "be extracted from JSON" in {
    val json = json"""{"s":"abc","t":"def"}"""
    val cc = json.as[CC]
    cc shouldBe CC("abc", "def")
  }
}

My best guess is that there's a macro in ScalaTest (I'm not overly familiar with it), and it's getting confused in some way.

Does that help you at all?

@byF
Copy link
Author

byF commented Jul 26, 2016

Actually, I extracted the issue into a case so I could share with you. This is happening in our codebase, basically the same thing with some actual business logic around.

I worked around it defining a custom CC JSON extractor, however I'll investigate further this week.

@propensive
Copy link
Owner

Thanks for spending the time on it. I seem to remember I saw something similar a while ago (and was able to fix it in the same way), though I was never able to reduce it to such a small test case. As with your example, it worked fine copy/pasting into the REPL...

Let me know if you'd like me to schedule some time to investigate it further. If it's the same issue I was having, I'd really like to get to the bottom of it...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants