Slick 2.10-RC1, Scala 2.11.x, bypassing 22 arity limit with case class (heterogenous)

后端 未结 1 1141
暗喜
暗喜 2021-02-09 15:25

I am having issues in mapping a Table that has > 22 columns specifically into a case class, assuming you have the following code

import slick.driver         


        
相关标签:
1条回答
  • 2021-02-09 15:48

    Figured it out, its pretty ugly though since its not generic.

    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
                ))
              }))
    

    It turns out there are a few things

    1. This has nothing to do with shapeless, Slick uses its own HList implementation (which has the exact same syntax as shapeless!)
    2. As far as I am aware, Slick HList doesn't appear to have any generic methods for dealing with stuff like mapping to a case class (and going from a case class to a Slick `HLIst)
    3. Eiher a library that converts Slick Hlist to a shapeless HList would be handy, or generic abilities for Slick Hlist. The former may be a better option because shapeless already does generic stuff much better than Slick, and it may be out of Slicks scope

    Doing something like

    def gen = Generic[TwentyThreeCaseClass]
    ...
    .shaped <>
          ({case x => gen.from(x)}, {TwentyThreeCaseClass => Option(gen.to(x))})
    

    Would be much more ideal

    here is another example as well

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

    0 讨论(0)
提交回复
热议问题