找不到FromRecord的隐含值

时间:2017-07-24 09:04:56

标签: scala avro4s

我正在努力开始使用avro& amp; avro4s和我遇到麻烦让一个愚蠢的例子工作。

我甚至无法在源代码库中找到我自己的测试版本来编译 - https://github.com/sksamuel/avro4s/blob/master/avro4s-core/src/test/scala/com/sksamuel/avro4s/AvroJsonInputTest.scala#L14-L20

我收到了标题could not find implicit value for parameter fromRecord: com.sksamuel.avro4s.FromRecord[Foo]中提到的错误。

package test

import java.io.ByteArrayInputStream

import com.sksamuel.avro4s._
import org.specs2.mutable.Specification

class AvroSpec extends Specification {
  "My classes" should {
    "be deserialized by avro" in {
      case class Foo(num: Int, name: String)
      val json = """{ "num": 17, "name": "jibba-jabbba" }"""
      val inst = Foo(num = 17, name = "jibba-jabba")
      val in =  new ByteArrayInputStream(json.getBytes("UTF-8"), 0, json.length)
      val foo = new AvroJsonInputStream[Foo](in).iterator.toSet
      in.close()

      foo mustEqual Set(inst)
    }
  }
}

我正在使用Scala 2.11.8和avro 1.7.0。我做错了什么?

1 个答案:

答案 0 :(得分:1)

将case类声明移到方法定义之外:

package test

import java.io.ByteArrayInputStream

import com.sksamuel.avro4s._
import org.specs2.mutable.Specification

case class Foo(num: Int, name: String)
class AvroSpec extends Specification {
  "My classes" should {
    "be deserialized by avro" in {
      val json = """{ "num": 17, "name": "jibba-jabbba" }"""
      val inst = Foo(num = 17, name = "jibba-jabba")
      val in =  new ByteArrayInputStream(json.getBytes("UTF-8"), 0, json.length)
      val foo = new AvroJsonInputStream[Foo](in).iterator.toSet
      in.close()

      foo mustEqual Set(inst)
    }
  }
}