Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add enum fields to composite type #743

Open
dejvid opened this issue Oct 18, 2024 · 2 comments
Open

How to add enum fields to composite type #743

dejvid opened this issue Oct 18, 2024 · 2 comments

Comments

@dejvid
Copy link

dejvid commented Oct 18, 2024

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
Copy link
Author

dejvid commented Oct 27, 2024

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.

tminglei added a commit that referenced this issue Dec 24, 2024
@tminglei
Copy link
Owner

tminglei commented Dec 24, 2024

Hi @dejvid, I added some sample codes for this. Pls check it.


p.s. L75 is the key codes. You need register type converters for the enum, before you define a type mapper for the composite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants