/
copymanager.scala
166 lines (146 loc) · 8.61 KB
/
copymanager.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2013-2018 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.free
import cats.~>
import cats.effect.{ Async, ExitCase }
import cats.free.{ Free => FF } // alias because some algebras have an op called Free
import java.io.InputStream
import java.io.OutputStream
import java.io.Reader
import java.io.Writer
import java.lang.String
import org.postgresql.copy.{ CopyDual => PGCopyDual }
import org.postgresql.copy.{ CopyIn => PGCopyIn }
import org.postgresql.copy.{ CopyManager => PGCopyManager }
import org.postgresql.copy.{ CopyOut => PGCopyOut }
@SuppressWarnings(Array("org.wartremover.warts.Overloading"))
object copymanager { module =>
// Algebra of operations for PGCopyManager. Each accepts a visitor as an alternatie to pattern-matching.
sealed trait CopyManagerOp[A] {
def visit[F[_]](v: CopyManagerOp.Visitor[F]): F[A]
}
// Free monad over CopyManagerOp.
type CopyManagerIO[A] = FF[CopyManagerOp, A]
// Module of instances and constructors of CopyManagerOp.
object CopyManagerOp {
// Given a PGCopyManager we can embed a CopyManagerIO program in any algebra that understands embedding.
implicit val CopyManagerOpEmbeddable: Embeddable[CopyManagerOp, PGCopyManager] =
new Embeddable[CopyManagerOp, PGCopyManager] {
def embed[A](j: PGCopyManager, fa: FF[CopyManagerOp, A]) = Embedded.CopyManager(j, fa)
}
// Interface for a natural transformation CopyManagerOp ~> F encoded via the visitor pattern.
// This approach is much more efficient than pattern-matching for large algebras.
trait Visitor[F[_]] extends (CopyManagerOp ~> F) {
final def apply[A](fa: CopyManagerOp[A]): F[A] = fa.visit(this)
// Common
def raw[A](f: PGCopyManager => A): F[A]
def embed[A](e: Embedded[A]): F[A]
def delay[A](a: () => A): F[A]
def handleErrorWith[A](fa: CopyManagerIO[A], f: Throwable => CopyManagerIO[A]): F[A]
def raiseError[A](e: Throwable): F[A]
def async[A](k: (Either[Throwable, A] => Unit) => Unit): F[A]
def asyncF[A](k: (Either[Throwable, A] => Unit) => CopyManagerIO[Unit]): F[A]
def bracketCase[A, B](acquire: CopyManagerIO[A])(use: A => CopyManagerIO[B])(release: (A, ExitCase[Throwable]) => CopyManagerIO[Unit]): F[B]
// PGCopyManager
def copyDual(a: String): F[PGCopyDual]
def copyIn(a: String): F[PGCopyIn]
def copyIn(a: String, b: InputStream): F[Long]
def copyIn(a: String, b: InputStream, c: Int): F[Long]
def copyIn(a: String, b: Reader): F[Long]
def copyIn(a: String, b: Reader, c: Int): F[Long]
def copyOut(a: String): F[PGCopyOut]
def copyOut(a: String, b: OutputStream): F[Long]
def copyOut(a: String, b: Writer): F[Long]
}
// Common operations for all algebras.
final case class Raw[A](f: PGCopyManager => A) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.raw(f)
}
final case class Embed[A](e: Embedded[A]) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.embed(e)
}
final case class Delay[A](a: () => A) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.delay(a)
}
final case class HandleErrorWith[A](fa: CopyManagerIO[A], f: Throwable => CopyManagerIO[A]) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.handleErrorWith(fa, f)
}
final case class RaiseError[A](e: Throwable) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.raiseError(e)
}
final case class Async1[A](k: (Either[Throwable, A] => Unit) => Unit) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.async(k)
}
final case class AsyncF[A](k: (Either[Throwable, A] => Unit) => CopyManagerIO[Unit]) extends CopyManagerOp[A] {
def visit[F[_]](v: Visitor[F]) = v.asyncF(k)
}
final case class BracketCase[A, B](acquire: CopyManagerIO[A], use: A => CopyManagerIO[B], release: (A, ExitCase[Throwable]) => CopyManagerIO[Unit]) extends CopyManagerOp[B] {
def visit[F[_]](v: Visitor[F]) = v.bracketCase(acquire)(use)(release)
}
// PGCopyManager-specific operations.
final case class CopyDual(a: String) extends CopyManagerOp[PGCopyDual] {
def visit[F[_]](v: Visitor[F]) = v.copyDual(a)
}
final case class CopyIn(a: String) extends CopyManagerOp[PGCopyIn] {
def visit[F[_]](v: Visitor[F]) = v.copyIn(a)
}
final case class CopyIn1(a: String, b: InputStream) extends CopyManagerOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.copyIn(a, b)
}
final case class CopyIn2(a: String, b: InputStream, c: Int) extends CopyManagerOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.copyIn(a, b, c)
}
final case class CopyIn3(a: String, b: Reader) extends CopyManagerOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.copyIn(a, b)
}
final case class CopyIn4(a: String, b: Reader, c: Int) extends CopyManagerOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.copyIn(a, b, c)
}
final case class CopyOut(a: String) extends CopyManagerOp[PGCopyOut] {
def visit[F[_]](v: Visitor[F]) = v.copyOut(a)
}
final case class CopyOut1(a: String, b: OutputStream) extends CopyManagerOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.copyOut(a, b)
}
final case class CopyOut2(a: String, b: Writer) extends CopyManagerOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.copyOut(a, b)
}
}
import CopyManagerOp._
// Smart constructors for operations common to all algebras.
val unit: CopyManagerIO[Unit] = FF.pure[CopyManagerOp, Unit](())
def pure[A](a: A): CopyManagerIO[A] = FF.pure[CopyManagerOp, A](a)
def raw[A](f: PGCopyManager => A): CopyManagerIO[A] = FF.liftF(Raw(f))
def embed[F[_], J, A](j: J, fa: FF[F, A])(implicit ev: Embeddable[F, J]): FF[CopyManagerOp, A] = FF.liftF(Embed(ev.embed(j, fa)))
def delay[A](a: => A): CopyManagerIO[A] = FF.liftF(Delay(() => a))
def handleErrorWith[A](fa: CopyManagerIO[A], f: Throwable => CopyManagerIO[A]): CopyManagerIO[A] = FF.liftF[CopyManagerOp, A](HandleErrorWith(fa, f))
def raiseError[A](err: Throwable): CopyManagerIO[A] = FF.liftF[CopyManagerOp, A](RaiseError(err))
def async[A](k: (Either[Throwable, A] => Unit) => Unit): CopyManagerIO[A] = FF.liftF[CopyManagerOp, A](Async1(k))
def asyncF[A](k: (Either[Throwable, A] => Unit) => CopyManagerIO[Unit]): CopyManagerIO[A] = FF.liftF[CopyManagerOp, A](AsyncF(k))
def bracketCase[A, B](acquire: CopyManagerIO[A])(use: A => CopyManagerIO[B])(release: (A, ExitCase[Throwable]) => CopyManagerIO[Unit]): CopyManagerIO[B] = FF.liftF[CopyManagerOp, B](BracketCase(acquire, use, release))
// Smart constructors for CopyManager-specific operations.
def copyDual(a: String): CopyManagerIO[PGCopyDual] = FF.liftF(CopyDual(a))
def copyIn(a: String): CopyManagerIO[PGCopyIn] = FF.liftF(CopyIn(a))
def copyIn(a: String, b: InputStream): CopyManagerIO[Long] = FF.liftF(CopyIn1(a, b))
def copyIn(a: String, b: InputStream, c: Int): CopyManagerIO[Long] = FF.liftF(CopyIn2(a, b, c))
def copyIn(a: String, b: Reader): CopyManagerIO[Long] = FF.liftF(CopyIn3(a, b))
def copyIn(a: String, b: Reader, c: Int): CopyManagerIO[Long] = FF.liftF(CopyIn4(a, b, c))
def copyOut(a: String): CopyManagerIO[PGCopyOut] = FF.liftF(CopyOut(a))
def copyOut(a: String, b: OutputStream): CopyManagerIO[Long] = FF.liftF(CopyOut1(a, b))
def copyOut(a: String, b: Writer): CopyManagerIO[Long] = FF.liftF(CopyOut2(a, b))
// CopyManagerIO is an Async
implicit val AsyncCopyManagerIO: Async[CopyManagerIO] =
new Async[CopyManagerIO] {
val asyncM = FF.catsFreeMonadForFree[CopyManagerOp]
def bracketCase[A, B](acquire: CopyManagerIO[A])(use: A => CopyManagerIO[B])(release: (A, ExitCase[Throwable]) => CopyManagerIO[Unit]): CopyManagerIO[B] = module.bracketCase(acquire)(use)(release)
def pure[A](x: A): CopyManagerIO[A] = asyncM.pure(x)
def handleErrorWith[A](fa: CopyManagerIO[A])(f: Throwable => CopyManagerIO[A]): CopyManagerIO[A] = module.handleErrorWith(fa, f)
def raiseError[A](e: Throwable): CopyManagerIO[A] = module.raiseError(e)
def async[A](k: (Either[Throwable,A] => Unit) => Unit): CopyManagerIO[A] = module.async(k)
def asyncF[A](k: (Either[Throwable,A] => Unit) => CopyManagerIO[Unit]): CopyManagerIO[A] = module.asyncF(k)
def flatMap[A, B](fa: CopyManagerIO[A])(f: A => CopyManagerIO[B]): CopyManagerIO[B] = asyncM.flatMap(fa)(f)
def tailRecM[A, B](a: A)(f: A => CopyManagerIO[Either[A, B]]): CopyManagerIO[B] = asyncM.tailRecM(a)(f)
def suspend[A](thunk: => CopyManagerIO[A]): CopyManagerIO[A] = asyncM.flatten(module.delay(thunk))
}
}