Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/core/src/main/scala-3/doobie/util/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ private[util] inline final def summonLabels[T <: Tuple]: List[String] =
private[util] inline final def summonSingletonCases[T <: Tuple, A](inline typeName: String): List[A] =
inline erasedValue[T] match
case _: EmptyTuple => Nil
case _: (h *: t) =>
case _: (h *: t) =>
inline summonInline[Mirror.Of[h]] match
case m: Mirror.Singleton => m.fromProduct(EmptyTuple).asInstanceOf[A] :: summonSingletonCases[t, A](typeName)
case m: Mirror =>
case m: Mirror =>
error("Enum " + typeName + " contains non singleton case " + constValue[m.MirroredLabel])
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ trait GetDBSuitePlatform { self: munit.CatsEffectSuite & TransactorProvider =>
given Get[EnumWithOnlySingletonCases] = Get.deriveEnumString

test("Get should properly read existing value of enum") {
sql"select 'One'".query[EnumWithOnlySingletonCases].unique.transact(xa).attempt.assertEquals(
sql"select 'One', 'Two', 'Three'".query[(
EnumWithOnlySingletonCases,
EnumWithOnlySingletonCases,
EnumWithOnlySingletonCases)].unique.transact(xa).attempt.assertEquals(
Right(
EnumWithOnlySingletonCases.One
EnumWithOnlySingletonCases.One,
EnumWithOnlySingletonCases.Two,
EnumWithOnlySingletonCases.Three
)
)
}
Expand Down
19 changes: 19 additions & 0 deletions modules/docs/src/main/mdoc/docs/12-Custom-Mappings.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ implicit val jsonMeta: Meta[Json] =

There are similar constructors for array types and other possibilities, but `other` is by far the most common in user code and we won't discuss the others here. See the Scaladoc for more information.

## Derivation for Scala 3 enums

You can derive `Get` and `Put` instances for Scala 3 enums using the `Get.deriveEnumString` or `Put.deriveEnumString`.

```scala
enum Color:
case Red, Green, Blue

object Color:
given Get[Color] = Get.deriveEnumString[Color]
given Put[Color] = Put.deriveEnumString[Color]

fr"SELECT 'Red'".query[Color].unique
```

As shown above,

- The instances need to be explicitly derived
- The string values converts from/to are the literal name of the enum cases

## Column Vector Mappings

Expand Down