@@ -86,9 +86,6 @@ package object db {
86
86
def fold [B ](ra : Failures ⇒ B , rb : A ⇒ B )(implicit F : Monad [F ]): FoxyT [F , B ] = // TODO: this is not fold… Find a better name or remove it? @michalrus
87
87
fa.map(rb).handleError(ra)
88
88
89
- def meh (implicit M : Monad [F ]): FoxyT [F , Unit ] =
90
- fa.void // TODO: remove me? But it’s cute… @michalrus
91
-
92
89
def failuresToWarnings (valueIfWasFailed : A )(pf : PartialFunction [Failure , Boolean ])(
93
90
implicit F : Monad [F ]): FoxyT [F , A ] =
94
91
fa.handleErrorWith { fs ⇒
@@ -105,15 +102,6 @@ package object db {
105
102
}
106
103
107
104
trait FoxyTFunctions [F [_]] {
108
- def good [A ](a : A )(implicit F : Monad [F ]): FoxyT [F , A ] = // TODO: remove me @michalrus
109
- a.pure[FoxyT [F , ? ]]
110
-
111
- def unit (implicit F : Monad [F ]): FoxyT [F , Unit ] =
112
- ().pure[FoxyT [F , ? ]] // TODO: remove me? @michalrus
113
-
114
- def none [A ](implicit F : Monad [F ]): FoxyT [F , Option [A ]] =
115
- (None : Option [A ]).pure[FoxyT [F , ? ]] // TODO: remove me? @michalrus
116
-
117
105
def uiWarning (f : Failure )(implicit F : Monad [F ]): FoxyT [F , Unit ] =
118
106
StateT .modify(MetaResponse .Warning (f) :: _)
119
107
@@ -149,6 +137,8 @@ package object db {
149
137
case _ ⇒ EitherT .right(F .pure((s, ())))
150
138
})
151
139
140
+ // TODO: maybe move the quasi-`sequence`-s from Functions to Ops? @michalrus
141
+
152
142
/** Just like ``sequence`` but—in case of a failure—unlawful, as it will join failures from all Foxies. */
153
143
def seqCollectFailures [L [_], A ](lfa : L [FoxyT [F , A ]])(implicit L : TraverseFilter [L ],
154
144
F : Monad [F ]): FoxyT [F , L [A ]] =
@@ -270,32 +260,40 @@ package object db {
270
260
def appendForUpdate [A , B <: slick.dbio.NoStream ](sql : SqlAction [A , B , Effect .Read ]): DBIO [A ] =
271
261
sql.overrideStatements(sql.statements.map(_ + " for update" ))
272
262
273
- // TODO: I don’t know… does this help ? @michalrus
263
+ // TODO: Is this more readable than inlining ? @michalrus
274
264
def ifElse [A ](condition : Boolean , ifBranch : ⇒ DbResultT [A ], elseBranch : ⇒ DbResultT [A ]) =
275
265
if (condition) ifBranch else elseBranch
276
266
277
- def when [F [_]](p : Boolean , s : ⇒ F [Unit ])( implicit F : Applicative [ F ]): F [Unit ] =
278
- if (p) s.void else F .pure(())
267
+ def when [F [_]: Applicative ](p : Boolean , s : ⇒ F [Unit ]): F [Unit ] =
268
+ if (p) s else ().pure[ F ]
279
269
280
- def doOrGood [A ](condition : Boolean , action : ⇒ DbResultT [A ], good : ⇒ A )(implicit ec : EC ): DbResultT [A ] =
281
- if (condition) action else DbResultT .good(good)
270
+ // TODO: Is this more readable than inlining? @michalrus
271
+ def doOrGood [F [_]: Applicative , A ](p : Boolean , action : ⇒ F [A ], good : ⇒ A )(implicit ec : EC ): F [A ] =
272
+ if (p) action else good.pure[F ]
282
273
283
- def doOrFail [A ](condition : Boolean , action : ⇒ DbResultT [A ], failure : ⇒ Failure )(
284
- implicit ec : EC ): DbResultT [A ] =
285
- if (condition) action else DbResultT .failure(failure)
274
+ // TODO: Is this more readable than inlining? @michalrus
275
+ def doOrFail [F [_]: Monad , A ](p : Boolean , action : ⇒ FoxyT [F , A ], failure : ⇒ Failure )(
276
+ implicit ec : EC ): FoxyT [F , A ] =
277
+ if (p) action else FoxyT [F ].failure(failure)
286
278
279
+ // TODO: Is this more readable than inlining? @michalrus
280
+ // FIXME: should be defined over FoxyT, but inference fails then… @michalrus
287
281
def failIf (condition : Boolean , failure : ⇒ Failure )(implicit ec : EC ): DbResultT [Unit ] =
288
- if (condition) DbResultT .failure(failure) else DbResultT .unit
282
+ if (condition) DbResultT .failure(failure) else ().pure[ DbResultT ]
289
283
284
+ // TODO: Is this more readable than inlining? @michalrus
285
+ // FIXME: should be defined over FoxyT, but inference fails then… @michalrus
290
286
def failIfNot (condition : Boolean , failure : ⇒ Failure )(implicit ec : EC ): DbResultT [Unit ] =
291
287
failIf(! condition, failure)
292
288
293
- def failIfFailures (failures : Seq [Failure ])(implicit ec : EC ): DbResultT [Unit ] =
289
+ // TODO: Is this more readable than inlining? @michalrus
290
+ // TODO: There’s only one usage in the whole codebase. @michalrus
291
+ def failIfFailures [F [_]: Monad ](failures : Seq [Failure ]): FoxyT [F , Unit ] =
294
292
failures match {
295
293
case head :: tail ⇒
296
- DbResultT .failures(NonEmptyList .of(head, tail : _* ))
294
+ FoxyT [ F ] .failures(NonEmptyList .of(head, tail : _* ))
297
295
case _ ⇒
298
- DbResultT .unit
296
+ ().pure[ FoxyT [ F , ? ]]
299
297
}
300
298
301
299
implicit class EnrichedSQLActionBuilder (val action : SQLActionBuilder ) extends AnyVal {
@@ -329,33 +327,32 @@ package object db {
329
327
330
328
def findOrCreate (r : DbResultT [R ])(implicit ec : EC ): DbResultT [R ] =
331
329
dbio.dbresult.flatMap {
332
- case Some (model) ⇒ DbResultT .good( model)
330
+ case Some (model) ⇒ model.pure[ DbResultT ]
333
331
case None ⇒ r
334
332
}
335
333
336
334
// Last item in tuple determines if cart was created or not
337
335
def findOrCreateExtended (r : DbResultT [R ])(implicit ec : EC ): DbResultT [(R , FoundOrCreated )] =
338
336
dbio.dbresult.flatMap {
339
- case Some (model) ⇒ DbResultT .good(( model, Found ))
337
+ case Some (model) ⇒ ( model, Found : FoundOrCreated ).pure[ DbResultT ]
340
338
case _ ⇒ r.map(result ⇒ (result, Created ))
341
339
}
342
340
343
341
def mustFindOr (notFoundFailure : Failure )(implicit ec : EC ): DbResultT [R ] =
344
342
dbio.dbresult.flatMap {
345
- case Some (model) ⇒ DbResultT .good( model)
343
+ case Some (model) ⇒ model.pure[ DbResultT ]
346
344
case None ⇒ DbResultT .failure(notFoundFailure)
347
345
}
348
346
349
347
def mustNotFindOr (shouldNotBeHere : Failure )(implicit ec : EC ): DbResultT [Unit ] =
350
348
dbio.dbresult.flatMap {
351
- case None ⇒ DbResultT .unit
349
+ case None ⇒ ().pure[ DbResultT ]
352
350
case Some (_) ⇒ DbResultT .failure(shouldNotBeHere)
353
351
}
354
352
355
- // we only use this when we *know* we can call head safely on a query. (e.g., you've created a record which
353
+ // we only use this when we *know* we can call head unsafely on a query. (e.g., you've created a record which
356
354
// has a FK constraint to another table and you then fetch that associated record -- we already *know* it must
357
355
// exist.
358
- // FIXME: if you know it, prove it. Or s/safe/unsafe/ in the name *AND* comment. @michalrus
359
- def safeGet (implicit ec : EC ): DBIO [R ] = dbio.map(_.get)
356
+ def unsafeGet (implicit ec : EC ): DBIO [R ] = dbio.map(_.get)
360
357
}
361
358
}
0 commit comments