Skip to content

Commit 66ee764

Browse files
committed
Add docs for Scala 3 enum derivation
#2233
1 parent 50998f6 commit 66ee764

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

modules/core/src/main/scala-3/doobie/util/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ private[util] inline final def summonLabels[T <: Tuple]: List[String] =
1515
private[util] inline final def summonSingletonCases[T <: Tuple, A](inline typeName: String): List[A] =
1616
inline erasedValue[T] match
1717
case _: EmptyTuple => Nil
18-
case _: (h *: t) =>
18+
case _: (h *: t) =>
1919
inline summonInline[Mirror.Of[h]] match
2020
case m: Mirror.Singleton => m.fromProduct(EmptyTuple).asInstanceOf[A] :: summonSingletonCases[t, A](typeName)
21-
case m: Mirror =>
21+
case m: Mirror =>
2222
error("Enum " + typeName + " contains non singleton case " + constValue[m.MirroredLabel])

modules/core/src/test/scala-3/doobie/util/GetSuitePlatform.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ trait GetDBSuitePlatform { self: munit.CatsEffectSuite & TransactorProvider =>
4646
given Get[EnumWithOnlySingletonCases] = Get.deriveEnumString
4747

4848
test("Get should properly read existing value of enum") {
49-
sql"select 'One'".query[EnumWithOnlySingletonCases].unique.transact(xa).attempt.assertEquals(
49+
sql"select 'One', 'Two', 'Three'".query[(
50+
EnumWithOnlySingletonCases,
51+
EnumWithOnlySingletonCases,
52+
EnumWithOnlySingletonCases)].unique.transact(xa).attempt.assertEquals(
5053
Right(
51-
EnumWithOnlySingletonCases.One
54+
EnumWithOnlySingletonCases.One,
55+
EnumWithOnlySingletonCases.Two,
56+
EnumWithOnlySingletonCases.Three
5257
)
5358
)
5459
}

modules/docs/src/main/mdoc/docs/12-Custom-Mappings.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@ implicit val jsonMeta: Meta[Json] =
177177

178178
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.
179179

180+
## Derivation for Scala 3 enums
181+
182+
You can derive `Get` and `Put` instances for Scala 3 enums using the `Get.deriveEnumString` or `Put.deriveEnumString`.
183+
184+
```scala
185+
enum Color:
186+
case Red, Green, Blue
187+
188+
object Color:
189+
given Get[Color] = Get.deriveEnumString[Color]
190+
given Put[Color] = Put.deriveEnumString[Color]
191+
192+
fr"SELECT 'Red'".query[Color].unique
193+
```
194+
195+
As shown above,
196+
197+
- The instances need to be explicitly derived
198+
- The string values converts from/to are the literal name of the enum cases
180199

181200
## Column Vector Mappings
182201

0 commit comments

Comments
 (0)