【发布时间】: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