forked from typelevel/doobie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeGen2.scala
605 lines (560 loc) · 27.3 KB
/
FreeGen2.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import sbt._, Keys._
import java.lang.reflect._
import scala.reflect.ClassTag
import Predef._
object FreeGen2 {
lazy val freeGen2Classes = settingKey[List[Class[_]]]("classes for which free algebras should be generated")
lazy val freeGen2Dir = settingKey[File]("directory where free algebras go")
lazy val freeGen2Package = settingKey[String]("package where free algebras go")
lazy val freeGen2Renames = settingKey[Map[Class[_], String]]("map of imports that must be renamed")
lazy val freeGen2KleisliInterpreterImportExcludes = settingKey[Set[Class[_]]]("Imports to exclude for the generator kleisliinterpreter.scala file (to avoid unused import warning) ")
lazy val freeGen2 = taskKey[Seq[File]]("generate free algebras")
lazy val freeGen2Settings = Seq(
freeGen2Classes := Nil,
freeGen2Dir := (Compile / sourceManaged).value,
freeGen2Package := "doobie.free",
freeGen2Renames := Map(classOf[java.sql.Array] -> "SqlArray"),
freeGen2KleisliInterpreterImportExcludes := Set.empty,
freeGen2 :=
new FreeGen2(
freeGen2Classes.value,
freeGen2Package.value,
freeGen2Renames.value,
freeGen2KleisliInterpreterImportExcludes.value,
state.value.log
).gen(freeGen2Dir.value),
Compile / compile := (Compile / compile).dependsOn(freeGen2).value
)
}
class FreeGen2(
managed: List[Class[_]],
pkg: String,
renames: Map[Class[_], String],
kleisliImportExcludes: Set[Class[_]],
log: Logger
) {
// These Java classes will have non-Java names in our generated code
val ClassBoolean = classOf[Boolean]
val ClassByte = classOf[Byte]
val ClassShort = classOf[Short]
val ClassInt = classOf[Int]
val ClassLong = classOf[Long]
val ClassFloat = classOf[Float]
val ClassDouble = classOf[Double]
val ClassObject = classOf[Object]
val ClassVoid = Void.TYPE
def tparams(t: Type): List[String] =
t match {
case t: GenericArrayType => tparams(t.getGenericComponentType)
case t: ParameterizedType => t.getActualTypeArguments.toList.flatMap(tparams)
case t: TypeVariable[_] => List(t.toString)
case _ => Nil
}
def toScalaType(t: Type): String =
t match {
case t: GenericArrayType => s"Array[${toScalaType(t.getGenericComponentType)}]"
case t: ParameterizedType => s"${toScalaType(t.getRawType)}${t.getActualTypeArguments.map(toScalaType).mkString("[", ", ", "]")}"
case t: WildcardType =>
t.getUpperBounds.toList.filterNot(_ == classOf[Object]) match {
case (c: Class[_]) :: Nil => s"_ <: ${c.getName}"
case Nil => "_"
case cs => sys.error("unhandled upper bounds: " + cs.toList)
}
case t: TypeVariable[_] => t.toString
case ClassVoid => "Unit"
case ClassBoolean => "Boolean"
case ClassByte => "Byte"
case ClassShort => "Short"
case ClassInt => "Int"
case ClassLong => "Long"
case ClassFloat => "Float"
case ClassDouble => "Double"
case ClassObject => "AnyRef"
case x: Class[_] =>
if (x.isArray) {
s"Array[${toScalaType(x.getComponentType)}]"
} else if (x.getName == "java.util.Map") {
x.getName
}
else {
renames.getOrElse(x, x.getSimpleName)
}
}
// Each constructor for our algebra maps to an underlying method, and an index is provided to
// disambiguate in cases of overloading.
case class Ctor(method: Method, index: Int) {
// The method name, unchanged
def mname: String =
method.getName
// The case class constructor name, capitalized and with an index when needed
def cname: String = {
val s = mname(0).toUpper +: mname.drop(1)
(if (index == 0) s else s"$s$index")
}
// Constructor parameter type names
def cparams: List[String] =
method.getGenericParameterTypes.toList.map(toScalaType)
def ctparams: String = {
val ss = (method.getGenericParameterTypes.toList.flatMap(tparams) ++ tparams(method.getGenericReturnType)).toSet
if (ss.isEmpty) "" else ss.mkString("[", ", ", "]")
}
// Constructor arguments, a .. z zipped with the right type
def cargs: List[String] =
"abcdefghijklmnopqrstuvwxyz".toList.zip(cparams).map {
case (n, t) => s"$n: $t"
}
// Return type name
def ret: String = toScalaType(method.getGenericReturnType)
// Case class/object declaration
def ctor(opname: String): String =
((cparams match {
case Nil => s"|case object $cname"
case ps => s"|final case class $cname$ctparams(${cargs.mkString(", ")})"
}) +
s""" extends ${opname}[$ret] {
| def visit[F[_]](v: Visitor[F]) = v.$mname${if (args.isEmpty) "" else s"($args)"}
| }""").trim.stripMargin
// Argument list: a, b, c, ... up to the proper arity
def args: String =
"abcdefghijklmnopqrstuvwxyz".toList.take(cparams.length).mkString(", ")
// Pattern to match the constructor
def pat: String =
cparams match {
case Nil => s"object $cname"
case ps => s"class $cname(${cargs.mkString(", ")})"
}
// Case clause mapping this constructor to the corresponding primitive action
def prim(sname: String): String =
(if (cargs.isEmpty)
s"case $cname => primitive(_.$mname)"
else
s"case $cname($args) => primitive(_.$mname($args))")
// Smart constructor
def lifted(ioname: String): String =
if (cargs.isEmpty) {
s"val $mname: ${ioname}[$ret] = FF.liftF(${cname})"
} else {
s"def $mname$ctparams(${cargs.mkString(", ")}): ${ioname}[$ret] = FF.liftF(${cname}($args))"
}
def visitor: String =
if (cargs.isEmpty) s"| def $mname: F[$ret]"
else s"| def $mname$ctparams(${cargs.mkString(", ")}): F[$ret]"
def stub: String =
if (cargs.isEmpty) s"""| def $mname: F[$ret] = sys.error("Not implemented: $mname")"""
else s"""| def $mname$ctparams(${cargs.mkString(", ")}): F[$ret] = sys.error("Not implemented: $mname$ctparams(${cparams.mkString(", ")})")"""
def kleisliImpl(oname: String): String =
if (cargs.isEmpty) s"| override def $mname: Kleisli[M, $oname, $ret] = primitive(_.$mname)"
else s"| override def $mname$ctparams(${cargs.mkString(", ")}) = primitive(_.$mname($args))"
}
// This class, plus any superclasses and interfaces, "all the way up"
def closure(c: Class[_]): List[Class[_]] =
(c :: (Option(c.getSuperclass).toList ++ c.getInterfaces.toList).flatMap(closure)).distinct
.filterNot(_.getName == "java.lang.AutoCloseable") // not available in jdk1.6
.filterNot(_.getName == "java.lang.Object") // we don't want .equals, etc.
implicit class MethodOps(m: Method) {
def isStatic: Boolean =
(m.getModifiers & Modifier.STATIC) != 0
}
// All non-deprecated methods for this class and any superclasses/interfaces
def methods(c: Class[_]): List[Method] =
closure(c).flatMap(_.getDeclaredMethods.toList).distinct
.filterNot(_.isStatic)
.filter(_.getAnnotation(classOf[Deprecated]) == null)
// Ctor values for all methods in of A plus superclasses, interfaces, etc.
def ctors[A](implicit ev: ClassTag[A]): List[Ctor] =
methods(ev.runtimeClass).groupBy(_.getName).toList.flatMap { case (n, ms) =>
ms.sortBy(_.getGenericParameterTypes.map(toScalaType).mkString(",")).zipWithIndex.map {
case (m, i) => Ctor(m, i)
}
}.sortBy(c => (c.mname, c.index))
// Fully qualified rename, if any
def renameImport(c: Class[_]): String = {
val origName = c.getSimpleName
renames.get(c) match {
case None => s"import ${c.getName}"
case Some(renamed) => s"import ${c.getPackage.getName}.{ $origName => $renamed }"
}
}
import scala.util.chaining._
// All types referenced by all methods on A, superclasses, interfaces, etc.
def imports[A](excludeImports: Set[Class[_]])(implicit ev: ClassTag[A]): List[String] = {
(renameImport(ev.runtimeClass) :: ctors.map(_.method).flatMap { m =>
m.getReturnType :: m.getParameterTypes.toList
}.map { t =>
if (t.isArray) t.getComponentType else t
}.filterNot(t => t.isPrimitive || t == classOf[Object] || excludeImports.contains(t)).map { c =>
renameImport(c)
}).distinct.sorted
}
// The algebra module for A
def module[A](implicit ev: ClassTag[A]): String = {
val oname = ev.runtimeClass.getSimpleName // original name, without name mapping
val sname = toScalaType(ev.runtimeClass)
val opname = s"${oname}Op"
val ioname = s"${oname}IO"
val mname = oname.toLowerCase
s"""
|package $pkg
|
|import cats.{~>, Applicative, Semigroup, Monoid}
|import cats.effect.kernel.{ CancelScope, Poll, Sync }
|import cats.free.{ Free => FF } // alias because some algebras have an op called Free
|import doobie.util.log.LogEvent
|import doobie.WeakAsync
|import scala.concurrent.Future
|import scala.concurrent.duration.FiniteDuration
|
|${imports[A](excludeImports = Set.empty).mkString("\n")}
|
|// This file is Auto-generated using FreeGen2.scala
|object $mname { module =>
|
| // Algebra of operations for $sname. Each accepts a visitor as an alternative to pattern-matching.
| sealed trait ${opname}[A] {
| def visit[F[_]](v: ${opname}.Visitor[F]): F[A]
| }
|
| // Free monad over ${opname}.
| type ${ioname}[A] = FF[${opname}, A]
|
| // Module of instances and constructors of ${opname}.
| object ${opname} {
|
| // Given a $sname we can embed a ${ioname} program in any algebra that understands embedding.
| implicit val ${opname}Embeddable: Embeddable[${opname}, ${sname}] =
| new Embeddable[${opname}, ${sname}] {
| def embed[A](j: ${sname}, fa: FF[${opname}, A]) = Embedded.${oname}(j, fa)
| }
|
| // Interface for a natural transformation ${opname} ~> F encoded via the visitor pattern.
| // This approach is much more efficient than pattern-matching for large algebras.
| trait Visitor[F[_]] extends (${opname} ~> F) {
| final def apply[A](fa: ${opname}[A]): F[A] = fa.visit(this)
|
| // Common
| def raw[A](f: $sname => A): F[A]
| def embed[A](e: Embedded[A]): F[A]
| def raiseError[A](e: Throwable): F[A]
| def handleErrorWith[A](fa: ${ioname}[A])(f: Throwable => ${ioname}[A]): F[A]
| def monotonic: F[FiniteDuration]
| def realTime: F[FiniteDuration]
| def delay[A](thunk: => A): F[A]
| def suspend[A](hint: Sync.Type)(thunk: => A): F[A]
| def forceR[A, B](fa: ${ioname}[A])(fb: ${ioname}[B]): F[B]
| def uncancelable[A](body: Poll[${ioname}] => ${ioname}[A]): F[A]
| def poll[A](poll: Any, fa: ${ioname}[A]): F[A]
| def canceled: F[Unit]
| def onCancel[A](fa: ${ioname}[A], fin: ${ioname}[Unit]): F[A]
| def fromFuture[A](fut: ${ioname}[Future[A]]): F[A]
| def fromFutureCancelable[A](fut: ${ioname}[(Future[A], ${ioname}[Unit])]): F[A]
| def performLogging(event: LogEvent): F[Unit]
|
| // $sname
${ctors[A].map(_.visitor).mkString("\n ")}
|
| }
|
| // Common operations for all algebras.
| final case class Raw[A](f: $sname => A) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.raw(f)
| }
| final case class Embed[A](e: Embedded[A]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.embed(e)
| }
| final case class RaiseError[A](e: Throwable) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.raiseError(e)
| }
| final case class HandleErrorWith[A](fa: ${ioname}[A], f: Throwable => ${ioname}[A]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.handleErrorWith(fa)(f)
| }
| case object Monotonic extends ${opname}[FiniteDuration] {
| def visit[F[_]](v: Visitor[F]) = v.monotonic
| }
| case object Realtime extends ${opname}[FiniteDuration] {
| def visit[F[_]](v: Visitor[F]) = v.realTime
| }
| case class Suspend[A](hint: Sync.Type, thunk: () => A) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.suspend(hint)(thunk())
| }
| case class ForceR[A, B](fa: ${ioname}[A], fb: ${ioname}[B]) extends ${opname}[B] {
| def visit[F[_]](v: Visitor[F]) = v.forceR(fa)(fb)
| }
| case class Uncancelable[A](body: Poll[${ioname}] => ${ioname}[A]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.uncancelable(body)
| }
| case class Poll1[A](poll: Any, fa: ${ioname}[A]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.poll(poll, fa)
| }
| case object Canceled extends ${opname}[Unit] {
| def visit[F[_]](v: Visitor[F]) = v.canceled
| }
| case class OnCancel[A](fa: ${ioname}[A], fin: ${ioname}[Unit]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.onCancel(fa, fin)
| }
| case class FromFuture[A](fut: ${ioname}[Future[A]]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.fromFuture(fut)
| }
| case class FromFutureCancelable[A](fut: ${ioname}[(Future[A], ${ioname}[Unit])]) extends ${opname}[A] {
| def visit[F[_]](v: Visitor[F]) = v.fromFutureCancelable(fut)
| }
| case class PerformLogging(event: LogEvent) extends ${opname}[Unit] {
| def visit[F[_]](v: Visitor[F]) = v.performLogging(event)
| }
|
| // $sname-specific operations.
| ${ctors[A].map(_.ctor(opname)).mkString("\n ")}
|
| }
| import ${opname}._
|
| // Smart constructors for operations common to all algebras.
| val unit: ${ioname}[Unit] = FF.pure[${opname}, Unit](())
| def pure[A](a: A): ${ioname}[A] = FF.pure[${opname}, A](a)
| def raw[A](f: $sname => A): ${ioname}[A] = FF.liftF(Raw(f))
| def embed[F[_], J, A](j: J, fa: FF[F, A])(implicit ev: Embeddable[F, J]): FF[${opname}, A] = FF.liftF(Embed(ev.embed(j, fa)))
| def raiseError[A](err: Throwable): ${ioname}[A] = FF.liftF[${opname}, A](RaiseError(err))
| def handleErrorWith[A](fa: ${ioname}[A])(f: Throwable => ${ioname}[A]): ${ioname}[A] = FF.liftF[${opname}, A](HandleErrorWith(fa, f))
| val monotonic = FF.liftF[${opname}, FiniteDuration](Monotonic)
| val realtime = FF.liftF[${opname}, FiniteDuration](Realtime)
| def delay[A](thunk: => A) = FF.liftF[${opname}, A](Suspend(Sync.Type.Delay, () => thunk))
| def suspend[A](hint: Sync.Type)(thunk: => A) = FF.liftF[${opname}, A](Suspend(hint, () => thunk))
| def forceR[A, B](fa: ${ioname}[A])(fb: ${ioname}[B]) = FF.liftF[${opname}, B](ForceR(fa, fb))
| def uncancelable[A](body: Poll[${ioname}] => ${ioname}[A]) = FF.liftF[${opname}, A](Uncancelable(body))
| def capturePoll[M[_]](mpoll: Poll[M]) = new Poll[${ioname}] {
| def apply[A](fa: ${ioname}[A]) = FF.liftF[${opname}, A](Poll1(mpoll, fa))
| }
| val canceled = FF.liftF[${opname}, Unit](Canceled)
| def onCancel[A](fa: ${ioname}[A], fin: ${ioname}[Unit]) = FF.liftF[${opname}, A](OnCancel(fa, fin))
| def fromFuture[A](fut: ${ioname}[Future[A]]) = FF.liftF[${opname}, A](FromFuture(fut))
| def fromFutureCancelable[A](fut: ${ioname}[(Future[A], ${ioname}[Unit])]) = FF.liftF[${opname}, A](FromFutureCancelable(fut))
| def performLogging(event: LogEvent) = FF.liftF[${opname}, Unit](PerformLogging(event))
|
| // Smart constructors for $oname-specific operations.
| ${ctors[A].map(_.lifted(ioname)).mkString("\n ")}
|
| // Typeclass instances for ${ioname}
| implicit val WeakAsync${ioname}: WeakAsync[${ioname}] =
| new WeakAsync[${ioname}] {
| val monad = FF.catsFreeMonadForFree[${opname}]
| override val applicative = monad
| override val rootCancelScope = CancelScope.Cancelable
| override def pure[A](x: A): ${ioname}[A] = monad.pure(x)
| override def flatMap[A, B](fa: ${ioname}[A])(f: A => ${ioname}[B]): ${ioname}[B] = monad.flatMap(fa)(f)
| override def tailRecM[A, B](a: A)(f: A => ${ioname}[Either[A, B]]): ${ioname}[B] = monad.tailRecM(a)(f)
| override def raiseError[A](e: Throwable): ${ioname}[A] = module.raiseError(e)
| override def handleErrorWith[A](fa: ${ioname}[A])(f: Throwable => ${ioname}[A]): ${ioname}[A] = module.handleErrorWith(fa)(f)
| override def monotonic: ${ioname}[FiniteDuration] = module.monotonic
| override def realTime: ${ioname}[FiniteDuration] = module.realtime
| override def suspend[A](hint: Sync.Type)(thunk: => A): ${ioname}[A] = module.suspend(hint)(thunk)
| override def forceR[A, B](fa: ${ioname}[A])(fb: ${ioname}[B]): ${ioname}[B] = module.forceR(fa)(fb)
| override def uncancelable[A](body: Poll[${ioname}] => ${ioname}[A]): ${ioname}[A] = module.uncancelable(body)
| override def canceled: ${ioname}[Unit] = module.canceled
| override def onCancel[A](fa: ${ioname}[A], fin: ${ioname}[Unit]): ${ioname}[A] = module.onCancel(fa, fin)
| override def fromFuture[A](fut: ${ioname}[Future[A]]): ${ioname}[A] = module.fromFuture(fut)
| override def fromFutureCancelable[A](fut: ${ioname}[(Future[A], ${ioname}[Unit])]): ${ioname}[A] = module.fromFutureCancelable(fut)
| }
|
| implicit def Monoid$ioname[A : Monoid]: Monoid[$ioname[A]] = new Monoid[$ioname[A]] {
| override def empty: $ioname[A] = Applicative[$ioname].pure(Monoid[A].empty)
| override def combine(x: $ioname[A], y: $ioname[A]): $ioname[A] =
| Applicative[$ioname].product(x, y).map { case (x, y) => Monoid[A].combine(x, y) }
| }
| implicit def Semigroup$ioname[A : Semigroup]: Semigroup[$ioname[A]] = new Semigroup[$ioname[A]] {
| override def combine(x: $ioname[A], y: $ioname[A]): $ioname[A] =
| Applicative[$ioname].product(x, y).map { case (x, y) => Semigroup[A].combine(x, y) }
| }
|}
|""".trim.stripMargin
}
def embed[A](implicit ev: ClassTag[A]): String = {
val sname = ev.runtimeClass.getSimpleName
s"final case class $sname[A](j: ${ev.runtimeClass.getName}, fa: ${sname}IO[A]) extends Embedded[A]"
}
// Import for the IO type for a carrer type, with renaming
def ioImport(c: Class[_]): String = {
val sn = c.getSimpleName
s"import ${sn.toLowerCase}.${sn}IO"
}
// The Embedded definition for all modules.
def embeds: String =
s"""
|package $pkg
|
|import cats.free.Free
|
|${managed.map(ioImport).mkString("\n")}
|
|// A pair (J, Free[F, A]) with constructors that tie down J and F.
|sealed trait Embedded[A]
|
|object Embedded {
| ${managed.map(ClassTag(_)).map(embed(_)).mkString("\n ")}
|}
|
|// Typeclass for embeddable pairs (J, F)
|trait Embeddable[F[_], J] {
| def embed[A](j: J, fa: Free[F, A]): Embedded[A]
|}
|""".trim.stripMargin
def interp[A](implicit ev: ClassTag[A]): String = {
val oname = ev.runtimeClass.getSimpleName // original name, without name mapping
val sname = toScalaType(ev.runtimeClass)
val opname = s"${oname}Op"
val ioname = s"${oname}IO"
def kles(retType: String) = s"Kleisli[M, $sname, $retType]"
val klesA = kles("A")
val klesUnit = kles("Unit")
val mname = oname.toLowerCase
s"""
| trait ${oname}Interpreter extends ${oname}Op.Visitor[Kleisli[M, $sname, *]] {
|
| // common operations delegate to outer interpreter
| override def raw[A](f: ${sname} => A): $klesA = outer.raw(f)
| override def embed[A](e: Embedded[A]): $klesA = outer.embed(e)
| override def raiseError[A](e: Throwable): $klesA = outer.raiseError(e)
| override def monotonic: ${kles("FiniteDuration")} = outer.monotonic[${sname}]
| override def realTime: ${kles("FiniteDuration")} = outer.realTime[${sname}]
| override def delay[A](thunk: => A): $klesA = outer.delay(thunk)
| override def suspend[A](hint: Sync.Type)(thunk: => A): $klesA = outer.suspend(hint)(thunk)
| override def canceled: $klesUnit = outer.canceled[${sname}]
|
| override def performLogging(event: LogEvent): $klesUnit = Kleisli(_ => logHandler.run(event))
|
| // for operations using ${ioname} we must call ourself recursively
| override def handleErrorWith[A](fa: ${ioname}[A])(f: Throwable => ${ioname}[A]): $klesA = outer.handleErrorWith(this)(fa)(f)
| override def forceR[A, B](fa: ${ioname}[A])(fb: ${ioname}[B]): ${kles("B")} = outer.forceR(this)(fa)(fb)
| override def uncancelable[A](body: Poll[${ioname}] => ${ioname}[A]): $klesA = outer.uncancelable(this, ${pkg}.${mname}.capturePoll)(body)
| override def poll[A](poll: Any, fa: ${ioname}[A]): $klesA = outer.poll(this)(poll, fa)
| override def onCancel[A](fa: ${ioname}[A], fin: ${ioname}[Unit]): $klesA = outer.onCancel(this)(fa, fin)
| override def fromFuture[A](fut: ${ioname}[Future[A]]): $klesA = outer.fromFuture(this)(fut)
| override def fromFutureCancelable[A](fut: ${ioname}[(Future[A], ${ioname}[Unit])]): $klesA = outer.fromFutureCancelable(this)(fut)
|
| // domain-specific operations are implemented in terms of `primitive`
|${ctors[A].map(_.kleisliImpl(sname)).mkString("\n")}
|
| }
|""".trim.stripMargin
}
def interpreterDef(c: Class[_]): String = {
val oname = c.getSimpleName // original name, without name mapping
val sname = toScalaType(c)
val opname = s"${oname}Op"
val ioname = s"${oname}IO"
val mname = oname.toLowerCase
s"lazy val ${oname}Interpreter: ${opname} ~> Kleisli[M, $sname, *] = new ${oname}Interpreter { }"
}
// template for a kleisli interpreter
def kleisliInterpreter: String =
s"""
|package $pkg
|
|// Library imports
|import cats.~>
|import cats.data.Kleisli
|import cats.effect.kernel.{ Poll, Sync }
|import cats.free.Free
|import doobie.WeakAsync
|import doobie.util.log.{LogEvent, LogHandler}
|import scala.concurrent.Future
|import scala.concurrent.duration.FiniteDuration
|
|// Types referenced in the JDBC API
|${managed.map(ClassTag(_)).flatMap(c => imports(kleisliImportExcludes)(c)).distinct.sorted.mkString("\n")}
|
|// Algebras and free monads thereof referenced by our interpreter.
|${managed.map(_.getSimpleName).map(c => s"import ${pkg}.${c.toLowerCase}.{ ${c}IO, ${c}Op }").mkString("\n")}
|
|object KleisliInterpreter {
| def apply[M[_]: WeakAsync](logHandler: LogHandler[M]): KleisliInterpreter[M] =
| new KleisliInterpreter[M](logHandler)
|}
|
|// Family of interpreters into Kleisli arrows for some monad M.
|class KleisliInterpreter[M[_]](logHandler: LogHandler[M])(implicit val asyncM: WeakAsync[M]) { outer =>
| import WeakAsync._
|
| // The ${managed.length} interpreters, with definitions below. These can be overridden to customize behavior.
| ${managed.map(interpreterDef).mkString("\n ")}
|
| // Some methods are common to all interpreters and can be overridden to change behavior globally.
| def primitive[J, A](f: J => A): Kleisli[M, J, A] = Kleisli { a =>
| // primitive JDBC methods throw exceptions and so do we when reading values
| // so catch any non-fatal exceptions and lift them into the effect
| try {
| asyncM.blocking(f(a))
| } catch {
| case scala.util.control.NonFatal(e) => asyncM.raiseError(e)
| }
| }
| def raw[J, A](f: J => A): Kleisli[M, J, A] = primitive(f)
| def raiseError[J, A](e: Throwable): Kleisli[M, J, A] = Kleisli(_ => asyncM.raiseError(e))
| def monotonic[J]: Kleisli[M, J, FiniteDuration] = Kleisli(_ => asyncM.monotonic)
| def realTime[J]: Kleisli[M, J, FiniteDuration] = Kleisli(_ => asyncM.realTime)
| def delay[J, A](thunk: => A): Kleisli[M, J, A] = Kleisli(_ => asyncM.delay(thunk))
| def suspend[J, A](hint: Sync.Type)(thunk: => A): Kleisli[M, J, A] = Kleisli(_ => asyncM.suspend(hint)(thunk))
| def canceled[J]: Kleisli[M, J, Unit] = Kleisli(_ => asyncM.canceled)
|
| // for operations using free structures we call the interpreter recursively
| def handleErrorWith[G[_], J, A](interpreter: G ~> Kleisli[M, J, *])(fa: Free[G, A])(f: Throwable => Free[G, A]): Kleisli[M, J, A] = Kleisli (j =>
| asyncM.handleErrorWith(fa.foldMap(interpreter).run(j))(f.andThen(_.foldMap(interpreter).run(j)))
| )
| def forceR[G[_], J, A, B](interpreter: G ~> Kleisli[M, J, *])(fa: Free[G, A])(fb: Free[G, B]): Kleisli[M, J, B] = Kleisli (j =>
| asyncM.forceR(fa.foldMap(interpreter).run(j))(fb.foldMap(interpreter).run(j))
| )
| def uncancelable[G[_], J, A](interpreter: G ~> Kleisli[M, J, *], capture: Poll[M] => Poll[Free[G, *]])(body: Poll[Free[G, *]] => Free[G, A]): Kleisli[M, J, A] = Kleisli(j =>
| asyncM.uncancelable(body.compose(capture).andThen(_.foldMap(interpreter).run(j)))
| )
| def poll[G[_], J, A](interpreter: G ~> Kleisli[M, J, *])(mpoll: Any, fa: Free[G, A]): Kleisli[M, J, A] = Kleisli(j =>
| mpoll.asInstanceOf[Poll[M]].apply(fa.foldMap(interpreter).run(j))
| )
| def onCancel[G[_], J, A](interpreter: G ~> Kleisli[M, J, *])(fa: Free[G, A], fin: Free[G, Unit]): Kleisli[M, J, A] = Kleisli (j =>
| asyncM.onCancel(fa.foldMap(interpreter).run(j), fin.foldMap(interpreter).run(j))
| )
| def fromFuture[G[_], J, A](interpreter: G ~> Kleisli[M, J, *])(fut: Free[G, Future[A]]): Kleisli[M, J, A] = Kleisli(j =>
| asyncM.fromFuture(fut.foldMap(interpreter).run(j))
| )
| def fromFutureCancelable[G[_], J, A](interpreter: G ~> Kleisli[M, J, *])(fut: Free[G, (Future[A], Free[G, Unit])]): Kleisli[M, J, A] = Kleisli(j =>
| asyncM.fromFutureCancelable(fut.map { case (f, g) => (f, g.foldMap(interpreter).run(j)) }.foldMap(interpreter).run(j))
| )
| def embed[J, A](e: Embedded[A]): Kleisli[M, J, A] =
| e match {
| ${managed.map(_.getSimpleName).map(n => s"case Embedded.${n}(j, fa) => Kleisli(_ => fa.foldMap(${n}Interpreter).run(j))").mkString("\n ")}
| }
|
| // Interpreters
|${managed.map(ClassTag(_)).map(interp(_)).mkString("\n")}
|
|}
|""".trim.stripMargin
def gen(base: File): Seq[java.io.File] = {
import java.io._
log.info("Generating free algebras into " + base)
val fs = managed.map { c =>
base.mkdirs
val mod = module(ClassTag(c))
val file = new File(base, s"${c.getSimpleName.toLowerCase}.scala")
val pw = new PrintWriter(file)
pw.println(mod)
pw.close()
log.info(s"${c.getName} -> ${file.getName}")
file
}
val e = {
val file = new File(base, s"embedded.scala")
val pw = new PrintWriter(file)
pw.println(embeds)
pw.close()
log.info(s"... -> ${file.getName}")
file
}
val ki = {
val file = new File(base, s"kleisliinterpreter.scala")
val pw = new PrintWriter(file)
pw.println(kleisliInterpreter)
pw.close()
log.info(s"... -> ${file.getName}")
file
}
ki :: e :: fs
}
}