【问题标题】:`exception during macro expansion: [error] scala.reflect.macros.TypecheckException` when using quill`宏扩展期间的异常:[错误]使用羽毛笔时的scala.reflect.macros.TypecheckException`
【发布时间】:2018-07-11 01:49:08
【问题描述】:

我对 Scala、Play 和 Quill 还很陌生,我不确定自己做错了什么。我将我的项目分成模型、存储库和服务(和控制器,但这与这个问题无关)。现在,我的服务中正在对数据库进行更改的行出现此错误:

exception during macro expansion:  scala.reflect.macros.TypecheckException: Can't find implicit `Decoder[models.AgentId]`. Please, do one of the following things:
1. ensure that implicit `Decoder[models.AgentId]` is provided and there are no other conflicting implicits; 
2. make `models.AgentId` `Embedded` case class or `AnyVal`.

我的服务中的所有其他行都收到此错误:

exception during macro expansion: [error] scala.reflect.macros.TypecheckException: not found: value quote

我发现了一个类似的ticket,但同样的修复方法对我不起作用(我已经需要ctx 作为隐式变量,所以我也无法导入它。我完全不知所措如果有人有任何建议,我很乐意尝试任何东西。我正在使用以下版本:

  • Scala 2.12.4
  • 羽毛笔 2.3.2
  • 播放 2.6.6

代码:

db/package.scala

package db

import io.getquill.{PostgresJdbcContext, SnakeCase}

package object db {
  class DBContext(config: String) extends PostgresJdbcContext(SnakeCase, config)

  trait Repository {
    val ctx: DBContext
  }
}

repositories/AgentsRepository.scala

package repositories

import db.db.Repository
import models.Agent

trait AgentsRepository extends Repository {
  import ctx._

  val agents = quote {
    query[Agent]
  }

  def agentById(id: AgentId) = quote { agents.filter(_.id == lift(id)) }

  def insertAgent(agent: Agent) = quote {
    query[Agent].insert(_.identifier -> lift(agent.identifier)
    ).returning(_.id)
  }
}

服务/AgentsService.scala

package services

import db.db.DBContext
import models.{Agent, AgentId}
import repositories.AgentsRepository
import scala.concurrent.ExecutionContext

class AgentService(implicit val ex: ExecutionContext, val ctx: DBContext)
  extends AgentsRepository {
  def list: List[Agent] =
    ctx.run(agents)

  def find(id: AgentId): List[Agent] =
    ctx.run(agentById(id))

  def create(agent: Agent): AgentId = {
    ctx.run(insertAgent(agent))
  }
}

模型/Agent.scala

package models

import java.time.LocalDateTime

case class AgentId(value: Long) extends AnyVal
case class Agent(
                  id: AgentId
                  , identifier: String
                )

【问题讨论】:

    标签: scala playframework-2.0 quill.io


    【解决方案1】:

    我已经需要 ctx 作为隐式变量,所以我也不能导入它

    您不需要导入上下文本身,而是需要导入其中的所有内容以使其工作

     import ctx._
    

    确保将其放在调用ctx.run 之前,如https://github.com/getquill/quill/issues/998#issuecomment-352189214

    【讨论】:

    • 我已经尝试过了,intelliJ 一直自动删除它,因为它已经在构造函数中被引用了。
    • 不要相信这里的idea,尝试用sbt编译。 Idea 此时无法展开所有宏来查看代码是否使用该导入。但是sbt可以
    • 呃,谢谢。首先使用 Vim 添加该行,然后最后修复 intelliJ,使其停止删除。
    猜你喜欢
    • 2018-08-22
    • 2013-02-10
    • 1970-01-01
    • 2016-09-22
    • 2017-02-14
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    相关资源
    最近更新 更多