scala Quill找不到Case类属性

r8xiu3jd  于 2022-11-09  发布在  Scala
关注(0)|答案(1)|浏览(128)

使用ZIO和Quill编写简单的CRUD应用程序
域模型由用户和角色组成(一对多)
在尝试调用在Quill上下文中提升(或在构建查询的过程中获得)的实体之一上的方法时,我遇到了编译时异常:

Can't find case class property: typedCode
                roleSchema.filter(r => r.typedCode == lift(roleCode)).take(1)
val dc: db.Ctx.type = db.Ctx
import dc._

def findRoleByCode(roleCode: RoleCode): Result[Option[Role]] =
    dc.run(
        roleSchema.filter(r => r.typedCode == lift(roleCode)).take(1)
    ).map(_.headOption)

模式如下:
用户实体

case class User(
    id: String,
    firstName: String,
    lastName: String,
    age: Int
){
    def typedId: UserId = UserId(id)
}

角色实体

case class Role(
    code: String,
    name: String
){
    def typedCode: RoleCode = RoleCode(code)
}

标识符中的用户与角色关联和值类

case class UserToRole(roleId: RoleCode, userId: UserId)

case class RoleCode(code: String) extends AnyVal
case class UserId(id: String) extends AnyVal

我目前得出的结论是,不可能对出现在Quill上下文中的实体调用方法。我想这是由于宏观评估和某种类型替换
问题是--获得房产并不是问题。我可以获取role.code作为String,但不能调用.typedCode。同样适用于用户

编辑

尝试将.typedCode.typedId转换为接受Unit作为参数typedCode(): RoleCodetypedId(): UserId的方法会导致另一个异常:

Tree 'r.typedCode()' can't be parsed to 'Ast'
                roleSchema.filter(r => r.typedCode() == lift(roleCode)).take(1)

编辑2在查询中构建值类示例:RoleCode(r.code)结果:

exception during macro expansion: 
scala.reflect.macros.TypecheckException: package module4.homework.dao.entity is not a value
    at scala.reflect.macros.contexts.Typers.$anonfun$typecheck$3(Typers.scala:44)
    at scala.reflect.macros.contexts.Typers.$anonfun$typecheck$2(Typers.scala:38)
    at scala.reflect.macros.contexts.Typers.doTypecheck$1(Typers.scala:37)
    at scala.reflect.macros.contexts.Typers.$anonfun$typecheck$7(Typers.scala:50)
    at scala.reflect.internal.Trees.wrappingIntoTerm(Trees.scala:1891)
    at scala.reflect.internal.Trees.wrappingIntoTerm$(Trees.scala:1888)
    at scala.reflect.internal.SymbolTable.wrappingIntoTerm(SymbolTable.scala:28)
    at scala.reflect.macros.contexts.Typers.typecheck(Typers.scala:50)
    at scala.reflect.macros.contexts.Typers.typecheck$(Typers.scala:32)
    at scala.reflect.macros.contexts.Context.typecheck(Context.scala:18)
    at scala.reflect.macros.contexts.Context.typecheck(Context.scala:18)
    at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:540)
    at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:526)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:64)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
    at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:546)
    at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:526)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:64)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$functionApplyParser$1.applyOrElse(Parsing.scala:579)
    at io.getquill.quotation.Parsing$$anonfun$functionApplyParser$1.applyOrElse(Parsing.scala:578)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:558)
    at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:552)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:61)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
    at io.getquill.quotation.Parsing.io$getquill$quotation$Parsing$$equalityWithInnerTypechecksIdiomatic(Parsing.scala:603)
    at io.getquill.quotation.Parsing$$anonfun$equalityOperationParser$1.applyOrElse(Parsing.scala:624)
    at io.getquill.quotation.Parsing$$anonfun$equalityOperationParser$1.applyOrElse(Parsing.scala:622)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:553)
    at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:552)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:61)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
    at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:202)
    at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:187)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:50)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
    at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:230)
    at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:187)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:50)
    at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
    at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
    at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
    at io.getquill.quotation.Quotation.$anonfun$quote$1(Quotation.scala:28)
    at io.getquill.util.Interpolator$Traceable.andReturn(Interpolator.scala:145)
    at io.getquill.quotation.Quotation.quote(Quotation.scala:28)
    at io.getquill.quotation.Quotation.quote$(Quotation.scala:24)
    at io.getquill.dsl.QuotationMacro.quote(QuotationDsl.scala:34)
    at jdk.internal.reflect.GeneratedMethodAccessor61.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.$anonfun$resolveJavaReflectionRuntime$5(JavaReflectionRuntimes.scala:45)
    at scala.tools.nsc.typechecker.Macros.macroExpandWithRuntime(Macros.scala:770)

                roleSchema.filter(r => RoleCode(r.code) == lift(roleCode)).take(1)

编辑3我想按照设计,我们不能从查询上下文中调用方法。

解决大多数问题的唯一解决方案是更改域模型本身,并使用值类作为UserRole的案例类参数

tyu7yeag

tyu7yeag1#

正如评论中已经回答的那样,Quill将把字段和案例类翻译成所选的SQL方言。没有办法将RoleCode(code)转换为SQL。您应该能够使typedRoletypedCode成为Case类的一个字段,并通过覆盖这个特定Case类的apply方法来处理它。

相关问题