-
Notifications
You must be signed in to change notification settings - Fork 37
/
doobie.scala
72 lines (64 loc) · 2.32 KB
/
doobie.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
61
62
63
64
65
66
67
68
69
70
71
72
package io.github.iltotore.iron
import cats.Show
import _root_.doobie.{Put, Get, Meta}
/**
* Implicit [[Meta]]s, [[Put]]s and [[Get]]s for refined types.
*/
object doobie:
/**
* A [[Get]] instance for refined types. Retrieve values using base type's [[Get]] then check the constraint on the
* retrieved unrefined values.
*
* @param get the base type's value getter
* @tparam A the base type
* @tparam C the constraint type
*/
inline given [A, C](using inline get: Get[A])(using Constraint[A, C], Show[A]): Get[A :| C] =
get.temap[A :| C](_.refineEither)
/**
* A [[Get]] instance for new types.
*
* @param m the meta information of the refined new type
* @param ev the value getter of the underlying type
* @tparam T the new type
*/
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Get[m.IronType]): Get[T] =
ev.asInstanceOf[Get[T]]
/**
* A [[Put]] instance for refined types. Retrieve values using base type's [[Put]] then check the constraint on the
* retrieved unrefined values.
*
* @param put the base type's value setter
* @tparam A the base type
* @tparam C the constraint type
*/
inline given [A, C](using inline put: Put[A])(using Constraint[A, C], Show[A]): Put[A :| C] =
put.tcontramap(identity)
/**
* A [[Put]] instance for new types.
*
* @param m the meta information of the refined new type
* @param ev the value setter of the underlying type
* @tparam T the new type
*/
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Put[m.IronType]): Put[T] =
ev.asInstanceOf[Put[T]]
/**
* A [[Meta]] instance for refined types. Retrieve values using base type's [[Meta]] then check the constraint on the
* retrieved unrefined values.
*
* @param meta the base type's value getter/setter
* @tparam A the base type
* @tparam C the constraint type
*/
inline given [A, C](using inline meta: Meta[A])(using Constraint[A, C], Show[A]): Meta[A :| C] =
meta.tiemap[A :| C](_.refineEither)(identity)
/**
* A [[Meta]] instance for new types.
*
* @param m the meta information of the refined new type
* @param ev the value getter/setter of the underlying type
* @tparam T the new type
*/
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Meta[m.IronType]): Meta[T] =
ev.asInstanceOf[Meta[T]]