tminglei / slick-pg

Slick extensions for PostgreSQL
BSD 2-Clause "Simplified" License
837 stars 180 forks source link

How to add enum fields to composite type #743

Open dejvid opened 2 weeks ago

dejvid commented 2 weeks ago

I have a straightforward data structure to represent the value of a currency.

case class CurrencyValue(value : Double, currency : CurrencyType)
enum CurrencyType:
   case EUR
   case GDP
   case ...

I represent CurrencyType in the database as

CREATE TYPE finance.currency AS ENUM ('USD', 'EUR', 'GBP', 'KM');

I have created a composite type to represent this currency value in the database.

case class DBCurrencyValue2(
    value: Double,
    currency: CurrencyType
) extends Struct

//I have added this to the MyPostgresAPI
implicit val fMapperCurrencyType: JdbcType[CurrencyType]                                                                                                   = createEnumJdbcType[CurrencyType]("finance.currency", _.toString, s => CurrencyType.valueOf(s), quoteName = false)
    implicit val fListTypeMapperCurrencyType: JdbcType[List[CurrencyType]]                                                                                     = createEnumListJdbcType[CurrencyType]("finance.currency", _.toString, s => CurrencyType.valueOf(s), quoteName = false)
    implicit val fColumnExtensionMethodsBuilderCurrencyType: Rep[CurrencyType] => EnumColumnExtensionMethods[CurrencyType, CurrencyType]                       = createEnumColumnExtensionMethodsBuilder[CurrencyType]
    implicit val fOptionColumnExtensionMethodsBuilderCurrencyType: Rep[Option[CurrencyType]] => EnumColumnExtensionMethods[CurrencyType, Option[CurrencyType]] = createEnumOptionColumnExtensionMethodsBuilder[CurrencyType]

And composite support:     

implicit val compositeWeSeeCommissionTypeMapper: GenericJdbcType[DBCurrencyValue2] = createCompositeJdbcType[DBCurrencyValue2]("finance.currency_value")

But when I try to compile this, I get this compile error (Scala 3):

/Users/technobug/razvoj/bbeacon/src/main/scala/logic/services/database/mappings/MyPostgresProfile.scala:52:91
No given instance of type com.github.tminglei.slickpg.TokenConverter[
  com.technobug.brzina.bot.libs.protocol.data.CurrencyType] was found.
I found:

    MyPostgresProfile.this.baseConverter[
      com.technobug.brzina.bot.libs.protocol.data.CurrencyType](
      /* missing */
        summon[
          com.github.tminglei.slickpg.utils.RegisteredTypeConverter[String,
            com.technobug.brzina.bot.libs.protocol.data.CurrencyType]
        ]
      ,
    ???)

But no implicit values were found that match type com.github.tminglei.slickpg.utils.RegisteredTypeConverter[String,
  com.technobug.brzina.bot.libs.protocol.data.CurrencyType].
    implicit val compositeWeSeeCommissionTypeMapper2: GenericJdbcType[DBCurrencyValue2] = createCompositeJdbcType[DBCurrencyValue2]("finance.currency_value")

Ok, then I say to myself, I haven't defined the proper typeclass, and I don't know how. Is there a macro that does this? I tried to compose the implicit instance of TokenConverter but I don't know how. Could you help me solve this issue? And I can make a PR adding this example into https://github.com/tminglei/slick-pg/blob/master/src/test/scala/com/github/tminglei/slickpg/PgCompositeSupportSuite.scala

Also I would add that on the README this link is broken

Screenshot 2024-10-18 at 15 56 05

I would appreciate your help.

dejvid commented 1 week ago

Is this project dead? I hope now. My codebase depends on it. Can please anyone help me with this? I don't know what to do.