-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from theiterators/doobie-support
Basic Scala 2 & 3 doobie support
- Loading branch information
Showing
12 changed files
with
314 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pl.iterators.kebs | ||
|
||
import doobie.Meta | ||
import pl.iterators.kebs.instances.InstanceConverter | ||
import pl.iterators.kebs.macros.CaseClass1Rep | ||
|
||
import scala.reflect.ClassTag | ||
|
||
trait Kebs { | ||
implicit def caseClass1RepMeta[A, M](implicit cc1Rep: CaseClass1Rep[A, M], m: Meta[M]): Meta[A] = m.imap(cc1Rep.apply)(cc1Rep.unapply) | ||
|
||
implicit def caseClass1RepArrayMeta[A, M](implicit cc1Rep: CaseClass1Rep[A, M], m: Meta[Array[M]], cta: ClassTag[A], ctm: ClassTag[M]): Meta[Array[A]] = m.imap(_.map(cc1Rep.apply))(_.map(cc1Rep.unapply)) | ||
|
||
implicit def caseClass1RepOptionArrayMeta[A, M](implicit cc1Rep: CaseClass1Rep[A, M], m: Meta[Array[Option[M]]], cta: ClassTag[A], ctm: ClassTag[M]): Meta[Array[Option[A]]] = m.imap(_.map(_.map(cc1Rep.apply)))(_.map(_.map(cc1Rep.unapply))) | ||
|
||
implicit def instanceConverterMeta[A, M](implicit instanceConverter: InstanceConverter[A, M], m: Meta[M]): Meta[A] = m.imap(instanceConverter.decode)(instanceConverter.encode) | ||
|
||
implicit def instanceConverterArrayMeta[A, M](implicit instanceConverter: InstanceConverter[A, M], m: Meta[Array[M]], cta: ClassTag[A], ctm: ClassTag[M]): Meta[Array[A]] = m.imap(_.map(instanceConverter.decode))(_.map(instanceConverter.encode)) | ||
|
||
implicit def instanceConverterOptionArrayMeta[A, M](implicit instanceConverter: InstanceConverter[A, M], m: Meta[Array[Option[M]]], cta: ClassTag[A], ctm: ClassTag[M]): Meta[Array[Option[A]]] = m.imap(_.map(_.map(instanceConverter.decode)))(_.map(_.map(instanceConverter.encode))) | ||
} |
27 changes: 27 additions & 0 deletions
27
doobie/src/main/scala-2/pl/iterators/kebs/enums/KebsEnums.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.iterators.kebs.enums | ||
|
||
import doobie.Meta | ||
import enumeratum.EnumEntry | ||
import pl.iterators.kebs.macros.enums.EnumOf | ||
|
||
import scala.reflect.ClassTag | ||
|
||
trait KebsEnums { | ||
implicit def enumMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[String]): Meta[E] = m.imap(e.`enum`.withName)(_.toString) | ||
implicit def enumArrayMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[Array[String]], cte: ClassTag[E]): Meta[Array[E]] = m.imap(_.map(e.`enum`.withName))(_.map(_.toString)) | ||
implicit def enumOptionArrayMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[Array[Option[String]]], cte: ClassTag[Option[E]]): Meta[Array[Option[E]]] = m.imap(_.map(_.map(e.`enum`.withName)))(_.map(_.map(_.toString))) | ||
|
||
trait Uppercase { | ||
implicit def enumUppercaseMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[String]): Meta[E] = m.imap(e.`enum`.withNameUppercaseOnly)(_.toString.toUpperCase) | ||
implicit def enumUppercaseArrayMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[Array[String]], cte: ClassTag[E]): Meta[Array[E]] = m.imap(_.map(e.`enum`.withNameUppercaseOnly))(_.map(_.toString.toUpperCase)) | ||
implicit def enumUppercaseOptionArrayMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[Array[Option[String]]], cte: ClassTag[E]): Meta[Array[Option[E]]] = m.imap(_.map(_.map(e.`enum`.withNameUppercaseOnly)))(_.map(_.map(_.toString.toUpperCase))) | ||
} | ||
|
||
trait Lowercase { | ||
implicit def enumLowercaseMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[String]): Meta[E] = m.imap(e.`enum`.withNameLowercaseOnly)(_.toString.toLowerCase) | ||
implicit def enumLowercaseArrayMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[Array[String]], cte: ClassTag[E]): Meta[Array[E]] = m.imap(_.map(e.`enum`.withNameLowercaseOnly))(_.map(_.toString.toLowerCase)) | ||
implicit def enumLowercaseOptionArrayMeta[E <: EnumEntry](implicit e: EnumOf[E], m: Meta[Array[Option[String]]], cte: ClassTag[E]): Meta[Array[Option[E]]] = m.imap(_.map(_.map(e.`enum`.withNameLowercaseOnly)))(_.map(_.map(_.toString.toLowerCase))) | ||
} | ||
} | ||
|
||
object KebsEnums extends KebsEnums |
8 changes: 8 additions & 0 deletions
8
doobie/src/main/scala-2/pl/iterators/kebs/enums/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pl.iterators.kebs | ||
|
||
package object enums extends KebsEnums { | ||
object uppercase extends Uppercase | ||
|
||
object lowercase extends Lowercase | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package pl.iterators | ||
|
||
package object kebs extends Kebs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package pl.iterators.kebs | ||
|
||
import doobie.{Get, Put, Meta} | ||
import pl.iterators.kebs.enums.KebsEnums | ||
import pl.iterators.kebs.instances.InstanceConverter | ||
import pl.iterators.kebs.macros.CaseClass1Rep | ||
|
||
import scala.reflect.ClassTag | ||
|
||
trait Kebs { | ||
inline given[A, M](using cc1Rep: CaseClass1Rep[A, M], m: Meta[M]): Meta[A] = m.imap(cc1Rep.apply)(cc1Rep.unapply) | ||
|
||
inline given[A, M](using cc1Rep: CaseClass1Rep[A, M], m: Meta[Option[M]]): Meta[Option[A]] = m.imap(_.map(cc1Rep.apply))(_.map(cc1Rep.unapply)) | ||
|
||
inline given[A, M](using cc1Rep: CaseClass1Rep[A, M], m: Meta[Array[M]], cta: ClassTag[A], ctm: ClassTag[M]): Meta[Array[A]] = m.imap(_.map(cc1Rep.apply))(_.map(cc1Rep.unapply)) | ||
|
||
inline given[A, M](using cc1Rep: CaseClass1Rep[A, M], m: Meta[Array[Option[M]]], cta: ClassTag[Option[A]]): Meta[Array[Option[A]]] = m.imap(_.map(_.map(cc1Rep.apply)))(_.map(_.map(cc1Rep.unapply))) | ||
|
||
inline given[A, M](using instanceConverter: InstanceConverter[A, M], m: Meta[M]): Meta[A] = m.imap(instanceConverter.decode)(instanceConverter.encode) | ||
|
||
inline given[A, M](using instanceConverter: InstanceConverter[A, M], m: Meta[Array[M]], cta: ClassTag[A], ctm: ClassTag[M]): Meta[Array[A]] = m.imap(_.map(instanceConverter.decode))(_.map(instanceConverter.encode)) | ||
|
||
inline given[A, M](using instanceConverter: InstanceConverter[A, M], m: Meta[Array[Option[M]]], cta: ClassTag[Option[A]]): Meta[Array[Option[A]]] = m.imap(_.map(_.map(instanceConverter.decode)))(_.map(_.map(instanceConverter.encode))) | ||
} |
24 changes: 24 additions & 0 deletions
24
doobie/src/main/scala-3/pl/iterators/kebs/enums/KebsEnums.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package pl.iterators.kebs.enums | ||
|
||
import doobie.Meta | ||
import pl.iterators.kebs.macros.enums.{EnumOf, EnumLike} | ||
import scala.reflect.ClassTag | ||
import scala.reflect.Enum | ||
|
||
trait KebsEnums { | ||
inline given enumMeta[E <: Enum](using e: EnumOf[E]): Meta[E] = Meta.StringMeta.imap(e.`enum`.valueOf)(_.toString) | ||
inline given enumArrayMeta[E <: Enum](using e: EnumOf[E], m: Meta[Array[String]], ct: ClassTag[E]): Meta[Array[E]] = m.imap(_.map(e.`enum`.valueOf))(_.map(_.toString)) | ||
inline given enumOptionArrayMeta[E <: Enum](using e: EnumOf[E], m: Meta[Array[Option[String]]], ct: ClassTag[Option[E]]): Meta[Array[Option[E]]] = m.imap(_.map(_.map(e.`enum`.valueOf)))(_.map(_.map(_.toString))) | ||
|
||
trait Uppercase { | ||
inline given enumUppercaseMeta[E <: Enum](using e: EnumOf[E]): Meta[E] = Meta.StringMeta.imap(s => e.`enum`.values.find(_.toString.toUpperCase == s).getOrElse(throw new IllegalArgumentException(s"enum case not found: $s")))(_.toString.toUpperCase) | ||
inline given enumUppercaseArrayMeta[E <: Enum](using e: EnumOf[E], m: Meta[Array[String]], ct: ClassTag[E]): Meta[Array[E]] = m.imap(_.map(s => e.`enum`.values.find(_.toString.toUpperCase == s).getOrElse(throw new IllegalArgumentException(s"enum case not found: $s"))))(_.map(_.toString.toUpperCase)) | ||
inline given enumUppercaseOptionArrayMeta[E <: Enum](using e: EnumOf[E], m: Meta[Array[Option[String]]], ct: ClassTag[Option[E]]): Meta[Array[Option[E]]] = m.imap(_.map(_.map(s => e.`enum`.values.find(_.toString.toUpperCase == s).getOrElse(throw new IllegalArgumentException(s"enum case not found: $s")))))(_.map(_.map(_.toString.toUpperCase))) | ||
} | ||
|
||
trait Lowercase { | ||
inline given enumLowercaseMeta[E <: Enum](using e: EnumOf[E]): Meta[E] = Meta.StringMeta.imap(s => e.`enum`.values.find(_.toString.toLowerCase == s).getOrElse(throw new IllegalArgumentException(s"enum case not found: $s")))(_.toString.toLowerCase) | ||
inline given enumLowercaseMeta[E <: Enum](using e: EnumOf[E], m: Meta[Array[String]], ct: ClassTag[E]): Meta[Array[E]] = m.imap(_.map(s => e.`enum`.values.find(_.toString.toLowerCase == s).getOrElse(throw new IllegalArgumentException(s"enum case not found: $s"))))(_.map(_.toString.toLowerCase)) | ||
inline given enumLowercaseOptionArrayMeta[E <: Enum](using e: EnumOf[E], m: Meta[Array[Option[String]]], ct: ClassTag[Option[E]]): Meta[Array[Option[E]]] = m.imap(_.map(_.map(s => e.`enum`.values.find(_.toString.toLowerCase == s).getOrElse(throw new IllegalArgumentException(s"enum case not found: $s")))))(_.map(_.map(_.toString.toLowerCase))) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
doobie/src/main/scala-3/pl/iterators/kebs/enums/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pl.iterators.kebs | ||
|
||
package object enums extends KebsEnums { | ||
object uppercase extends Uppercase | ||
|
||
object lowercase extends Lowercase | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package pl.iterators | ||
|
||
package object kebs extends Kebs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import enumeratum.{Enum, EnumEntry} | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import java.util.Currency | ||
import doobie._ | ||
import doobie.implicits._ | ||
import doobie.postgres._ | ||
import doobie.postgres.implicits._ | ||
import pl.iterators.kebs.enums._ | ||
import pl.iterators.kebs._ | ||
import pl.iterators.kebs.instances.KebsInstances._ | ||
|
||
class ComplexTypesTests extends AnyFunSuite with Matchers { | ||
case class Name(name: String) | ||
sealed trait EyeColor extends EnumEntry | ||
object EyeColor extends Enum[EyeColor] { | ||
case object Blue extends EyeColor | ||
case object Green extends EyeColor | ||
case object Brown extends EyeColor | ||
case object Other extends EyeColor | ||
def values = findValues | ||
} | ||
case class Person(name: Name, eyeColor: EyeColor, preferredCurrency: Currency, relatives: List[Name], eyeballsInTheJar: Array[EyeColor]) | ||
|
||
test("Put & Get exist") { | ||
"implicitly[Get[Name]]" should compile | ||
"implicitly[Put[Name]]" should compile | ||
"implicitly[Get[List[Name]]]" should compile | ||
"implicitly[Put[List[Name]]]" should compile | ||
"implicitly[Get[Array[Name]]]" should compile | ||
"implicitly[Put[Array[Name]]]" should compile | ||
"implicitly[Get[Vector[Name]]]" should compile | ||
"implicitly[Put[Vector[Name]]]" should compile | ||
"implicitly[Get[List[Option[Name]]]]" should compile | ||
"implicitly[Put[List[Option[Name]]]]" should compile | ||
"implicitly[Get[Array[Option[Name]]]]" should compile | ||
"implicitly[Put[Array[Option[Name]]]]" should compile | ||
"implicitly[Get[Vector[Option[Name]]]]" should compile | ||
"implicitly[Put[Vector[Option[Name]]]]" should compile | ||
|
||
"implicitly[Get[Currency]]" should compile | ||
"implicitly[Put[Currency]]" should compile | ||
"implicitly[Get[List[Currency]]]" should compile | ||
"implicitly[Put[List[Currency]]]" should compile | ||
"implicitly[Get[Array[Currency]]]" should compile | ||
"implicitly[Put[Array[Currency]]]" should compile | ||
"implicitly[Get[Vector[Currency]]]" should compile | ||
"implicitly[Put[Vector[Currency]]]" should compile | ||
"implicitly[Get[List[Option[Currency]]]]" should compile | ||
"implicitly[Put[List[Option[Currency]]]]" should compile | ||
"implicitly[Get[Array[Option[Currency]]]]" should compile | ||
"implicitly[Put[Array[Option[Currency]]]]" should compile | ||
"implicitly[Get[Vector[Option[Currency]]]]" should compile | ||
"implicitly[Put[Vector[Option[Currency]]]]" should compile | ||
|
||
"implicitly[Get[EyeColor]]" should compile | ||
"implicitly[Put[EyeColor]]" should compile | ||
"implicitly[Get[List[EyeColor]]]" should compile | ||
"implicitly[Put[List[EyeColor]]]" should compile | ||
"implicitly[Get[Array[EyeColor]]]" should compile | ||
"implicitly[Put[Array[EyeColor]]]" should compile | ||
"implicitly[Get[Vector[EyeColor]]]" should compile | ||
"implicitly[Put[Vector[EyeColor]]]" should compile | ||
"implicitly[Get[List[Option[EyeColor]]]]" should compile | ||
"implicitly[Put[List[Option[EyeColor]]]]" should compile | ||
"implicitly[Get[Array[Option[EyeColor]]]]" should compile | ||
"implicitly[Put[Array[Option[EyeColor]]]]" should compile | ||
"implicitly[Get[Vector[Option[EyeColor]]]]" should compile | ||
"implicitly[Put[Vector[Option[EyeColor]]]]" should compile | ||
} | ||
|
||
test("Query should compile") { | ||
"""sql"SELECT * FROM people".query[Person].unique""" should compile | ||
} | ||
} |
Oops, something went wrong.