`宏扩展期间的异常:[error] scala.reflect.macros.TypecheckException` when using quill
`exception during macro expansion: [error] scala.reflect.macros.TypecheckException` when using quill
我对 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)
}
}
services/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))
}
}
models/Agent.scala
package models
import java.time.LocalDateTime
case class AgentId(value: Long) extends AnyVal
case class Agent(
id: AgentId
, identifier: String
)
I am already requiring ctx as an implicit variable, so I can't import it as well
您不需要导入上下文本身,但需要导入其中的所有内容才能使其正常工作
import ctx._
确保将其放在 ctx.run
调用之前,如 https://github.com/getquill/quill/issues/998#issuecomment-352189214
我对 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)
}
}
services/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))
}
}
models/Agent.scala
package models
import java.time.LocalDateTime
case class AgentId(value: Long) extends AnyVal
case class Agent(
id: AgentId
, identifier: String
)
I am already requiring ctx as an implicit variable, so I can't import it as well
您不需要导入上下文本身,但需要导入其中的所有内容才能使其正常工作
import ctx._
确保将其放在 ctx.run
调用之前,如 https://github.com/getquill/quill/issues/998#issuecomment-352189214