@@ -105,15 +105,6 @@ package object db {
105105 }
106106
107107 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-
117108 def uiWarning (f : Failure )(implicit F : Monad [F ]): FoxyT [F , Unit ] =
118109 StateT .modify(MetaResponse .Warning (f) :: _)
119110
@@ -149,6 +140,8 @@ package object db {
149140 case _ ⇒ EitherT .right(F .pure((s, ())))
150141 })
151142
143+ // TODO: maybe move the quasi-`sequence`-s from Functions to Ops? @michalrus
144+
152145 /** Just like ``sequence`` but—in case of a failure—unlawful, as it will join failures from all Foxies. */
153146 def seqCollectFailures [L [_], A ](lfa : L [FoxyT [F , A ]])(implicit L : TraverseFilter [L ],
154147 F : Monad [F ]): FoxyT [F , L [A ]] =
@@ -270,32 +263,40 @@ package object db {
270263 def appendForUpdate [A , B <: slick.dbio.NoStream ](sql : SqlAction [A , B , Effect .Read ]): DBIO [A ] =
271264 sql.overrideStatements(sql.statements.map(_ + " for update" ))
272265
273- // TODO: I don’t know… does this help ? @michalrus
266+ // TODO: Is this more readable than inlining ? @michalrus
274267 def ifElse [A ](condition : Boolean , ifBranch : ⇒ DbResultT [A ], elseBranch : ⇒ DbResultT [A ]) =
275268 if (condition) ifBranch else elseBranch
276269
277- def when [F [_]](p : Boolean , s : ⇒ F [Unit ])( implicit F : Applicative [ F ]): F [Unit ] =
278- if (p) s.void else F .pure(())
270+ def when [F [_]: Applicative ](p : Boolean , s : ⇒ F [Unit ]): F [Unit ] =
271+ if (p) s else ().pure[ F ]
279272
280- def doOrGood [A ](condition : Boolean , action : ⇒ DbResultT [A ], good : ⇒ A )(implicit ec : EC ): DbResultT [A ] =
281- if (condition) action else DbResultT .good(good)
273+ // TODO: Is this more readable than inlining? @michalrus
274+ def doOrGood [F [_]: Applicative , A ](p : Boolean , action : ⇒ F [A ], good : ⇒ A )(implicit ec : EC ): F [A ] =
275+ if (p) action else good.pure[F ]
282276
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)
277+ // TODO: Is this more readable than inlining? @michalrus
278+ def doOrFail [F [_]: Monad , A ](p : Boolean , action : ⇒ FoxyT [F , A ], failure : ⇒ Failure )(
279+ implicit ec : EC ): FoxyT [F , A ] =
280+ if (p) action else FoxyT [F ].failure(failure)
286281
282+ // TODO: Is this more readable than inlining? @michalrus
283+ // FIXME: should be defined over FoxyT, but inference fails then… @michalrus
287284 def failIf (condition : Boolean , failure : ⇒ Failure )(implicit ec : EC ): DbResultT [Unit ] =
288- if (condition) DbResultT .failure(failure) else DbResultT .unit
285+ if (condition) DbResultT .failure(failure) else ().pure[ DbResultT ]
289286
287+ // TODO: Is this more readable than inlining? @michalrus
288+ // FIXME: should be defined over FoxyT, but inference fails then… @michalrus
290289 def failIfNot (condition : Boolean , failure : ⇒ Failure )(implicit ec : EC ): DbResultT [Unit ] =
291290 failIf(! condition, failure)
292291
293- def failIfFailures (failures : Seq [Failure ])(implicit ec : EC ): DbResultT [Unit ] =
292+ // TODO: Is this more readable than inlining? @michalrus
293+ // TODO: There’s only one usage in the whole codebase. @michalrus
294+ def failIfFailures [F [_]: Monad ](failures : Seq [Failure ]): FoxyT [F , Unit ] =
294295 failures match {
295296 case head :: tail ⇒
296- DbResultT .failures(NonEmptyList .of(head, tail : _* ))
297+ FoxyT [ F ] .failures(NonEmptyList .of(head, tail : _* ))
297298 case _ ⇒
298- DbResultT .unit
299+ ().pure[ FoxyT [ F , ? ]]
299300 }
300301
301302 implicit class EnrichedSQLActionBuilder (val action : SQLActionBuilder ) extends AnyVal {
@@ -329,33 +330,32 @@ package object db {
329330
330331 def findOrCreate (r : DbResultT [R ])(implicit ec : EC ): DbResultT [R ] =
331332 dbio.dbresult.flatMap {
332- case Some (model) ⇒ DbResultT .good( model)
333+ case Some (model) ⇒ model.pure[ DbResultT ]
333334 case None ⇒ r
334335 }
335336
336337 // Last item in tuple determines if cart was created or not
337338 def findOrCreateExtended (r : DbResultT [R ])(implicit ec : EC ): DbResultT [(R , FoundOrCreated )] =
338339 dbio.dbresult.flatMap {
339- case Some (model) ⇒ DbResultT .good(( model, Found ))
340+ case Some (model) ⇒ ( model, Found : FoundOrCreated ).pure[ DbResultT ]
340341 case _ ⇒ r.map(result ⇒ (result, Created ))
341342 }
342343
343344 def mustFindOr (notFoundFailure : Failure )(implicit ec : EC ): DbResultT [R ] =
344345 dbio.dbresult.flatMap {
345- case Some (model) ⇒ DbResultT .good( model)
346+ case Some (model) ⇒ model.pure[ DbResultT ]
346347 case None ⇒ DbResultT .failure(notFoundFailure)
347348 }
348349
349350 def mustNotFindOr (shouldNotBeHere : Failure )(implicit ec : EC ): DbResultT [Unit ] =
350351 dbio.dbresult.flatMap {
351- case None ⇒ DbResultT .unit
352+ case None ⇒ ().pure[ DbResultT ]
352353 case Some (_) ⇒ DbResultT .failure(shouldNotBeHere)
353354 }
354355
355- // we only use this when we *know* we can call head safely on a query. (e.g., you've created a record which
356+ // we only use this when we *know* we can call head unsafely on a query. (e.g., you've created a record which
356357 // has a FK constraint to another table and you then fetch that associated record -- we already *know* it must
357358 // 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)
359+ def unsafeGet (implicit ec : EC ): DBIO [R ] = dbio.map(_.get)
360360 }
361361}
0 commit comments