forked from tpolecat/doobie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instances.scala
60 lines (43 loc) · 1.74 KB
/
Instances.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2013-2020 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.postgres.circe
import cats.syntax.either.*
import cats.syntax.show.*
import doobie.Get
import doobie.Put
import io.circe.*
import io.circe.jawn.*
import io.circe.syntax.*
object Instances {
trait JsonbInstances {
implicit val jsonbPut: Put[Json] =
doobie.postgres.instances.json.jsonbPutFromString(_.noSpaces)
implicit val jsonbGet: Get[Json] =
doobie.postgres.instances.json.jsonbGetFromString(parse(_).leftMap(_.show))
def pgEncoderPutT[A: Encoder]: Put[A] =
Put[Json].tcontramap(_.asJson)
def pgEncoderPut[A: Encoder]: Put[A] =
Put[Json].contramap(_.asJson)
def pgDecoderGetT[A: Decoder]: Get[A] =
Get[Json].temap(json => json.as[A].leftMap(_.show))
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def pgDecoderGet[A: Decoder]: Get[A] =
Get[Json].map(json => json.as[A].fold(throw _, identity))
}
trait JsonInstances {
implicit val jsonPut: Put[Json] =
doobie.postgres.instances.json.jsonPutFromString(_.noSpaces)
implicit val jsonGet: Get[Json] =
doobie.postgres.instances.json.jsonGetFromString(parse(_).leftMap(_.show))
def pgEncoderPutT[A: Encoder]: Put[A] =
Put[Json].tcontramap(_.asJson)
def pgEncoderPut[A: Encoder]: Put[A] =
Put[Json].contramap(_.asJson)
def pgDecoderGetT[A: Decoder]: Get[A] =
Get[Json].temap(json => json.as[A].leftMap(_.show))
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def pgDecoderGet[A: Decoder]: Get[A] =
Get[Json].map(json => json.as[A].fold(throw _, identity))
}
}