2

我对 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

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

  • 斯卡拉 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
  }
}

存储库/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
                )
4

1 回答 1

2

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

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

 import ctx._

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

于 2018-01-31T21:54:27.833 回答