amnaredo / test

0 stars 0 forks source link

Couldn't derive type ... #131

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago
case class Result2(name : String,
                   whatever : String,
                   types : List[String]
                    )

case class GeoCoding2(results : List[Result2], status: String)

object UPickleNewVersionService {
  def getSample() = {
    val g = read[GeoCoding2]("")
    g
  }
}

//mytest case
test("testderiveType") {
   UPickleNewVersionService.getSample()
  }

error :

/UPickleNewVersionService.scala:9: Couldn't derive type (List[Result2], String) [error] val g = read[GeoCoding2]("")

ID: 95 Original Author: chandu0101

amnaredo commented 3 years ago

I am facing the same error in my (closed-source) project with upickle v0.3.0.

However, creating a minimal test-case is a challenge. I tried the above code by @chandu0101 and it didn't fail, in a fresh project.

Original Author: hrj

amnaredo commented 3 years ago

Odd, this works for me in both 2.10.4 and 2.11.7 in the REPL:

scala> case class Result2(name : String,
     |                    whatever : String,
     |                    types : List[String]
     |                     )
defined class Result2

scala>

scala> case class GeoCoding2(results : List[Result2], status: String)
defined class GeoCoding2

scala>

scala> read[GeoCoding2]("")
<console>:15: error: not found: value read
       read[GeoCoding2]("")
       ^

scala> upickle.default.read[GeoCoding2]("")
upickle.Invalid$Json
  at upickle.json.package$.read(package.scala:11)
  at upickle.Types$class.read(Types.scala:133)
  at upickle.default$.read(Api.scala:25)
  ... 43 elided

Original Author: lihaoyi

amnaredo commented 3 years ago

Ok, I figured it. If the classes (like GeoCoding and Result) are defined in another file, and UPickleNewVersionService is defined and used in a different file, I get the error.

Original Author: hrj

amnaredo commented 3 years ago

Here's a further minimalised test:

file1.scala

package test
case class C1(name : String, types : List[String])
case class C2(results : List[C1])

file2.scala

package test

import upickle.default._

object Test {
  object Service {
    def getSample() = {
      read[C2]("")
    }
  }

  //mytest case
  def xyz(x:String) {
    Service.getSample()
  }
}

build.sbt

libraryDependencies += "com.lihaoyi" %% "upickle" % "0.3.0"

scalaVersion := "2.11.7"

Output

[error] file2.scala:8: Couldn't derive type (List[test.C1],)
[error]       read[C2]("")

Original Author: hrj

amnaredo commented 3 years ago

@lihaoyi it works in REPL but fail in the test. Just add the code

object OtherStuffs {
  case class Result2(name : String, whatever : String, types : List[String])
  case class GeoCoding2(results : List[Result2], status: String)
}
 import OtherStuffs._

in https://github.com/lihaoyi/upickle-pprint/blob/master/upickle/shared/src/test/scala/example/ExampleTests.scala and

    'complex {
      read[GeoCoding2]("")
    }

produces this:

 uPickle does not know how to read [example.OtherSuffs.GeoCoding2]s; define an implicit Reader[example.OtherSuffs.GeoCoding2] to teach it how
[error]       read[GeoCoding2]("")
[error]                       ^
[error] /Users/binh/codes/scalajs/upickle-pprint/upickle/shared/src/test/scala/example/ExampleTests.scala:84: uPickle does not know how to read [example.OtherSuffs.GeoCoding2]s; define an implicit Reader[example.OtherSuffs.GeoCoding2] to teach it how
[error]       read[GeoCoding2]("")
[error]                       ^

Original Author: ngbinh

amnaredo commented 3 years ago

FYI...I hit this too but for a different reason. I had >21 params on my case class.

Original Author: bjenkinsgit