-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uuid/json/enum
- Loading branch information
Showing
14 changed files
with
234 additions
and
54 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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
package doobiex.enums | ||
|
||
import cats.effect._ | ||
import cats.implicits._ | ||
import doobie._ | ||
import doobie.implicits._ | ||
import doobiex.xa | ||
import java.util.UUID | ||
|
||
/** Postgres ENUM internal implementation | ||
* syntax: | ||
* {{{ | ||
* create type message_type AS ENUM('trace', 'debug', 'info', 'warn', 'error', 'fatal') | ||
* }}} | ||
* motivation: | ||
* - internal state just 4-byte integer | ||
* - ordering works out of the box | ||
*/ | ||
object EnumMappingApp extends IOApp.Simple { | ||
|
||
/** required to derive Get[MyRow] | ||
* import MetaInstances._ | ||
*/ | ||
|
||
import doobiex.uuid.InstancesUuidMeta._ | ||
|
||
case class MyRow(id: UUID, typ: MessageType, msg: String) | ||
|
||
val program: ConnectionIO[List[MyRow]] = sql"select id, typ, msg from messages" | ||
.query[MyRow] | ||
.to[List] | ||
|
||
val io: IO[List[MyRow]] = program.transact(xa[IO]) | ||
|
||
override def run: IO[Unit] = | ||
program | ||
.transact(xa[IO]) | ||
.flatMap(_.traverse_(x => IO(pprint.pprintln(x)))) | ||
} |
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,18 @@ | ||
package doobiex.enums | ||
|
||
import enumeratum._ | ||
import enumeratum.EnumEntry._ | ||
|
||
sealed trait MessageType extends EnumEntry with Lowercase | ||
|
||
object MessageType extends Enum[MessageType] with CirceEnum[MessageType] with DoobieEnum[MessageType] { | ||
|
||
case object Trace extends MessageType | ||
case object Debug extends MessageType | ||
case object Info extends MessageType | ||
case object Warn extends MessageType | ||
case object Error extends MessageType | ||
case object Fatal extends MessageType | ||
|
||
override def values: IndexedSeq[MessageType] = findValues | ||
} |
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 @@ | ||
INSERT | ||
INTO messages (typ, msg) | ||
VALUES | ||
('warn', 'message_warn'), | ||
('fatal', 'message_fatal'), | ||
('debug', 'message_debug'), | ||
('trace', 'message_trace'), | ||
('error', 'message_error'); |
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,10 @@ | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
|
||
create type message_type AS ENUM('trace', 'debug', 'info', 'warn', 'error', 'fatal'); | ||
|
||
create table if not exists messages | ||
( | ||
id uuid default uuid_generate_v4() not null constraint messages_pk primary key, | ||
typ message_type, | ||
msg text | ||
); |
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,23 @@ | ||
package doobiex.json | ||
|
||
import cats.Show | ||
import cats.data.NonEmptyList | ||
import cats.implicits._ | ||
import doobie._ | ||
import io.circe._ | ||
import org.postgresql.util.PGobject | ||
|
||
object InstancesJsonGet { | ||
|
||
implicit val showPGobject: Show[PGobject] = Show.show(_.getValue) | ||
|
||
implicit val jsonGet: Get[Json] = | ||
Get.Advanced | ||
.other[PGobject](NonEmptyList.of("json")) | ||
.temap[Json] { o: PGobject => | ||
val parsed: Either[ParsingFailure, Json] = io.circe.parser.parse(o.getValue) | ||
val remapped: Either[String, Json] = parsed.leftMap(_.show) | ||
remapped | ||
} | ||
|
||
} |
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 doobiex.json | ||
|
||
import cats.implicits._ | ||
import doobie._ | ||
import io.circe.Json | ||
import org.postgresql.util.PGobject | ||
|
||
object InstancesJsonMeta { | ||
|
||
implicit val JsonbMeta: Meta[Json] = Meta.Advanced | ||
.other[PGobject]("jsonb") | ||
.timap[Json] { pgo: PGobject => | ||
io.circe.parser | ||
.parse(pgo.getValue) | ||
.leftMap[Json](err => throw err) | ||
.merge | ||
} { json: Json => | ||
val o = new PGobject | ||
o.setType("jsonb") | ||
o.setValue(json.noSpaces) | ||
o | ||
} | ||
|
||
} |
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,26 @@ | ||
package doobiex.json | ||
|
||
import cats.effect._ | ||
import cats.implicits._ | ||
import doobie.implicits._ | ||
import doobiex.xa | ||
import io.circe.Json | ||
import java.util.UUID | ||
import doobiex.uuid.InstancesUuidMeta | ||
|
||
object JsonMappingApp extends IOApp.Simple { | ||
|
||
import InstancesUuidMeta._ | ||
import InstancesJsonMeta._ | ||
case class Row(id: UUID, value: Option[Json]) | ||
|
||
val program = sql"select id, value from jsons" | ||
.query[Row] | ||
.to[List] | ||
|
||
override def run: IO[Unit] = | ||
program | ||
.transact(xa[IO]) | ||
.flatMap(x => x.traverse_(x => IO(pprint.pprintln(x)))) | ||
|
||
} |
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,25 @@ | ||
package doobiex.uuid | ||
|
||
import doobie.Meta | ||
import java.util.UUID | ||
|
||
object InstancesUuidMeta { | ||
|
||
/** the rule how to Map Scala types to Postgres | ||
* | ||
* `Meta[A] = Get[A] + Put[A]` | ||
* | ||
* actually, just a map + contramap combination. | ||
* In case of `UUID` we describe two functions: | ||
* | ||
* - `String => UUID` | ||
* - `UUID => String` | ||
*/ | ||
implicit val uuidMeta: Meta[UUID] = | ||
Meta[String].imap { raw: String => | ||
UUID.fromString(raw) | ||
} { uuid: UUID => | ||
uuid.toString | ||
} | ||
|
||
} |
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,42 @@ | ||
package doobiex.uuid | ||
|
||
import cats.effect._ | ||
import cats.implicits._ | ||
import doobie._ | ||
import doobie.implicits._ | ||
import doobiex.xa | ||
import java.util.UUID | ||
|
||
/** Postgres extensions to handle UUID generation | ||
* {{{ | ||
* CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
* }}} | ||
* it allows syntax | ||
* {{{ | ||
* id uuid default uuid_generate_v4() | ||
* }}} | ||
*/ | ||
object UuidMappingApp extends IOApp.Simple { | ||
|
||
/** required to derive Get[MyRow] | ||
* import MetaInstances._ | ||
*/ | ||
|
||
import InstancesUuidMeta._ | ||
|
||
case class MyRow(id: UUID) | ||
|
||
/** Meta combines Get & Put */ | ||
|
||
val program: ConnectionIO[List[MyRow]] = sql"select id from uuids" | ||
.query[MyRow] | ||
.to[List] | ||
|
||
val io: IO[List[MyRow]] = program.transact(xa[IO]) | ||
|
||
override def run: IO[Unit] = | ||
program | ||
.transact(xa[IO]) | ||
.flatMap(_.traverse_(x => IO(pprint.pprintln(x)))) | ||
|
||
} |
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,6 @@ | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
|
||
create table if not exists uuids | ||
( | ||
id uuid default uuid_generate_v4() not null constraint uuids_pk primary key | ||
); |
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