gpt4 book ai didi

scala - Slick 2.10-RC1,Scala 2.11.x,使用 case 类绕过 22 arity 限制(异构)

转载 作者:行者123 更新时间:2023-12-04 14:27:39 25 4
gpt4 key购买 nike

我在将具有 > 22 列的表专门映射到 case class 时遇到问题,假设您有以下代码

import slick.driver.PostgresDriver
import scala.slick.collection.heterogenous._
import syntax._
import shapeless.Generic

case class TwentyThreeCaseClass(
val id:Option[Long],
val one:String,
val two:String,
val three:String,
val four:String,
val five:String,
val six:String,
val seven:String,
val eight:String,
val nine:String,
val ten:String,
val eleven:String,
val twelve:String,
val thirteen:String,
val fourteen:String,
val fifteen:String,
val sixteen:String,
val seventeen:String,
val eighteen:String,
val nineteen:String,
val twenty:String,
val twentyOne:String,
val twentyTwo:String,
val twentyThree:String,
val twentyFour:String
)

class TwentyThreeTable(tag:Tag) extends Table[TwentyThreeCaseClass](tag,"twenty_three_table") {
def id = column[Long]("id",O.PrimaryKey,O.AutoInc)
def one = column[String]("one")
def two = column[String]("two")
def three = column[String]("three")
def four = column[String]("four")
def five = column[String]("five")
def six = column[String]("six")
def seven = column[String]("seven")
def eight = column[String]("eight")
def nine = column[String]("nine")
def ten = column[String]("ten")
def eleven = column[String]("eleven")
def twelve = column[String]("twelve")
def thirteen = column[String]("thirteen")
def fourteen = column[String]("fourteen")
def fifteen = column[String]("fifteen")
def sixteen = column[String]("sixteen")
def seventeen = column[String]("seventeen")
def eighteen = column[String]("eighteen")
def nineteen = column[String]("nineteen")
def twenty = column[String]("twenty")
def twentyOne = column[String]("twentyOne")
def twentyTwo = column[String]("twentyTwo")
def twentyThree = column[String]("twentyThree")
def twentyFour = column[String]("twentyFour")

private def iso[L <: HList, M <: HList](l: L)
(implicit iso: Generic.Aux[TwentyThreeCaseClass, M], eq: L =:= M): TwentyThreeCaseClass = iso.from(l)

def * =
id.? ::
one ::
two ::
three ::
four ::
five ::
six ::
seven ::
eight ::
nine ::
ten ::
elven ::
twelve ::
thirteen ::
fourteen ::
fifteen ::
sixteen ::
seventeen ::
eighteen ::
nineteen ::
twenty ::
twentyOne ::
twentyTwo ::
twentyThree ::
twentyFour ::
HNil
// Do stuff here to map to a case class

}

您将如何将表构建/提取到 TwentyThreeCaseClass 中? .给出了如何制作光滑的示例代码 Table映射到 HList,但没有给出关于如何通过 HList 将表映射到案例类 > 22 个参数的代码(您不能使用元组,因为 Scala 中的元数限制仍然适用于元组,您不能创建包含超过 22 个元素的元组)
iso在那里,因为我们使用这个通用的 iso 代码从 HList 映射到 case class在 slick 之外的无形状代码中具有相同的形状,因此从理论上讲,您应该能够使用 iso 从 HList 构造案例类,我只是不知道如何在光滑形状的上下文中使用 iso

编辑:
在 slick github 上也有同样的问题作为问题在这里 https://github.com/slick/slick/issues/519#issuecomment-48327043

最佳答案

想通了,虽然它很丑,因为它不是通用的。

def * =
(id.? ::
one ::
two ::
three ::
four ::
five ::
six ::
seven ::
eight ::
nine ::
ten ::
elven ::
twelve ::
thirteen ::
fourteen ::
fifteen ::
sixteen ::
seventeen ::
eighteen ::
nineteen ::
twenty ::
twentyOne ::
twentyTwo ::
twentyThree ::
twentyFour ::
HNil).shaped <>
({case x => TwentyThreeCaseClass(
x(0),
x(1),
x(2),
x(3),
x(4),
x(5),
x(6),
x(7),
x(8),
x(9),
x(10),
x(11),
x(12),
x(13),
x(14),
x(15),
x(16),
x(17),
x(18),
x(19),
x(20),
x(21),
x(22),
x(23),
x(24)
)}, ({x:TwentyThreeCaseClass =>
Option((
x.id ::
x.one ::
x.two ::
x.three ::
x.four ::
x.five ::
x.six ::
x.seven ::
x.eight ::
x.nine ::
x.ten ::
x.eleven ::
x.twelve ::
x.thirteen ::
x.fourteen ::
x.fifteen ::
x.sixteen ::
x.seventeen ::
x.eighteen ::
x.nineteen ::
x.twenty ::
x.twentyOne ::
x.twentyTwo ::
x.twentyThree ::
x.twentyFour ::
HNil
))
}))

事实证明有几件事
  • 这个和shapeless没有关系,Slick用的是自己的HList实现(与 shapeless 的语法完全相同!)
  • 据我所知,Slick HList似乎没有任何通用方法来处理诸如映射到 case class 之类的事情。 (并从案例类到 Slick `HLIst)
  • 或者是一个转换 Slick 的库 Hlist到无形HList会很方便,或者 Slick 的通用能力 Hlist .前者可能是更好的选择,因为 shapeless 已经比 Slick 做通用的东西好得多,而且它可能超出了 Slicks 的范围

  • 做类似的事情
    def gen = Generic[TwentyThreeCaseClass]
    ...
    .shaped <>
    ({case x => gen.from(x)}, {TwentyThreeCaseClass => Option(gen.to(x))})

    会更理想

    这也是另一个例子

    https://github.com/playframework/play-slick/issues/214

    关于scala - Slick 2.10-RC1,Scala 2.11.x,使用 case 类绕过 22 arity 限制(异构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24978042/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com