-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
2 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
27 changes: 27 additions & 0 deletions
27
enumeratum-doobie-postgres/src/main/scala/enumeratum/postgres/DoobiePgEnum.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 enumeratum.postgres | ||
|
||
import doobie._ | ||
import doobie.postgres.implicits.pgEnumStringOpt | ||
import enumeratum._ | ||
|
||
import scala.reflect.runtime.universe.TypeTag | ||
|
||
object DoobiePgEnum { | ||
|
||
/** | ||
* {{{ | ||
* trait Foo extends EnumEntry | ||
* object Foo extends Enum[Foo] { | ||
* case object Bar extends Foo | ||
* | ||
* val values = findValues | ||
* implicit val doobieMeta: Meta[Foo] = DoobiePgEnum.meta("foo", Foo) | ||
* } | ||
* }}} | ||
* @param name type in postgres | ||
* @return Meta[A] for the given enum | ||
*/ | ||
def meta[A <: EnumEntry: TypeTag](name: String, enum: Enum[A]): Meta[A] = { | ||
pgEnumStringOpt(name, enum.withNameOption, _.entryName) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
enumeratum-doobie-postgres/src/main/scala/enumeratum/postgres/values/DoobiePgValueEnum.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,13 @@ | ||
package enumeratum.postgres.values | ||
|
||
import doobie._ | ||
import doobie.postgres.implicits.pgEnumStringOpt | ||
import enumeratum.values.{StringEnumEntry, ValueEnum} | ||
|
||
import scala.reflect.runtime.universe.TypeTag | ||
|
||
object DoobiePgValueEnum { | ||
def meta[A <: StringEnumEntry: TypeTag](name: String, enum: ValueEnum[String, A]): Meta[A] = { | ||
pgEnumStringOpt(name, enum.withValueOpt, _.value) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
enumeratum-doobie-postgres/src/test/scala/enumeratum/posrgres/DoobiePgEnumSpec.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,34 @@ | ||
package enumeratum.posrgres | ||
|
||
import enumeratum._ | ||
import org.scalatest.{FunSpec, Matchers} | ||
import doobie.{Read, Write, Meta} | ||
import enumeratum.postgres.DoobiePgEnum | ||
|
||
import scala.collection.immutable | ||
|
||
class DoobiePgEnumSpec extends FunSpec with Matchers { | ||
describe("A DoobieEnum") { | ||
|
||
it("should have a Write") { | ||
Write[DoobieShirt] | ||
} | ||
|
||
it("should have a Read") { | ||
Read[DoobieShirt] | ||
} | ||
|
||
} | ||
} | ||
|
||
sealed trait DoobieShirtSize extends EnumEntry | ||
case object DoobieShirtSize extends Enum[DoobieShirtSize] { | ||
case object Small extends DoobieShirtSize | ||
case object Medium extends DoobieShirtSize | ||
case object Large extends DoobieShirtSize | ||
|
||
override val values: immutable.IndexedSeq[DoobieShirtSize] = findValues | ||
implicit val doobieMeta: Meta[DoobieShirtSize] = DoobiePgEnum.meta("shirt_size", DoobieShirtSize) | ||
} | ||
|
||
case class DoobieShirt(size: DoobieShirtSize) |
37 changes: 37 additions & 0 deletions
37
...tum-doobie-postgres/src/test/scala/enumeratum/posrgres/values/DoobiePgValueEnumSpec.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,37 @@ | ||
package enumeratum.posrgres.values | ||
|
||
import org.scalatest.{FunSpec, Matchers} | ||
import doobie.{Meta, Read, Write} | ||
import enumeratum.postgres.values.DoobiePgValueEnum | ||
import enumeratum.values.{StringEnum, StringEnumEntry} | ||
|
||
import scala.collection.immutable | ||
|
||
class DoobiePgValueEnumSpec extends FunSpec with Matchers { | ||
describe("A StringDoobieEnum") { | ||
|
||
it("should have a Write") { | ||
Write[DoobieComputer] | ||
} | ||
|
||
it("should have a Read") { | ||
Read[DoobieComputer] | ||
} | ||
|
||
} | ||
} | ||
|
||
sealed abstract class DoobieOperatingSystem(val value: String) extends StringEnumEntry | ||
case object DoobieOperatingSystem extends StringEnum[DoobieOperatingSystem] { | ||
|
||
case object Linux extends DoobieOperatingSystem("linux") | ||
case object OSX extends DoobieOperatingSystem("osx") | ||
case object Windows extends DoobieOperatingSystem("windows") | ||
case object Android extends DoobieOperatingSystem("android") | ||
|
||
override val values: immutable.IndexedSeq[DoobieOperatingSystem] = findValues | ||
implicit val doobieMeta: Meta[DoobieOperatingSystem] = | ||
DoobiePgValueEnum.meta("operating_system", DoobieOperatingSystem) | ||
} | ||
|
||
case class DoobieComputer(operatingSystem: DoobieOperatingSystem) |