Skip to content

Commit ddf83a5

Browse files
committed
Get rid of some more custom functions
1 parent 326a316 commit ddf83a5

File tree

87 files changed

+315
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+315
-303
lines changed

phoenix-scala/core/app/core/db/ExceptionWrapper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object ExceptionWrapper {
1111
import scala.util.{Failure, Success}
1212

1313
dbio.asTry.dbresult.flatMap {
14-
case Success(value) DbResultT.good(value)
14+
case Success(value) value.pure[DbResultT]
1515
case Failure(e) DbResultT.failure(DatabaseFailure(e.getMessage))
1616
}
1717
}

phoenix-scala/core/app/core/db/FoxTableQuery.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ abstract class FoxTableQuery[M <: FoxModel[M], T <: FoxTable[M]](construct: Tag
8686
}
8787

8888
def refresh(model: M)(implicit ec: EC): DBIO[M] =
89-
findOneById(model.id).safeGet
89+
findOneById(model.id).unsafeGet
9090

9191
type QuerySeq = Query[T, M, Seq]
9292

phoenix-scala/core/app/core/db/SearchTerms.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package core.db
22

3+
import cats.implicits._
34
import core.failures.{Failure, NotFoundFailure400, NotFoundFailure404}
45
import core.utils.Strings._
56
import slick.jdbc.PostgresProfile.api._
@@ -24,7 +25,7 @@ trait SearchById[M <: FoxModel[M], T <: FoxTable[M]] {
2425
private def mustFindById(id: M#Id, notFoundFailure: M#Id Failure = notFound404K)(implicit ec: EC,
2526
db: DB): DbResultT[M] =
2627
this.findOneById(id).dbresult.flatMap {
27-
case Some(model) DbResultT.good(model)
28+
case Some(model) model.pure[DbResultT]
2829
case None DbResultT.failure(notFoundFailure(id))
2930
}
3031
}
@@ -37,7 +38,7 @@ trait SearchByRefNum[M <: FoxModel[M], T <: FoxTable[M]] extends SearchById[M, T
3738
implicit ec: EC,
3839
db: DB): DbResultT[M] =
3940
findOneByRefNum(refNum).dbresult.flatMap {
40-
case Some(model) DbResultT.good(model)
41+
case Some(model) model.pure[DbResultT]
4142
case None DbResultT.failure(notFoundFailure(refNum))
4243
}
4344
}
@@ -49,7 +50,7 @@ trait SearchByCode[M <: FoxModel[M], T <: FoxTable[M]] extends SearchById[M, T]
4950
def mustFindByCode(code: String, notFoundFailure: String Failure = notFound404K)(implicit ec: EC,
5051
db: DB): DbResultT[M] =
5152
findOneByCode(code).dbresult.flatMap {
52-
case Some(model) DbResultT.good(model)
53+
case Some(model) model.pure[DbResultT]
5354
case None DbResultT.failure(notFoundFailure(code))
5455
}
5556
}
@@ -62,7 +63,7 @@ trait SearchByIdAndName[M <: FoxModel[M], T <: FoxTable[M]] extends SearchById[M
6263
implicit ec: EC,
6364
db: DB): DbResultT[M] =
6465
findOneByIdAndName(id, name).dbresult.flatMap {
65-
case Some(model) DbResultT.good(model)
66+
case Some(model) model.pure[DbResultT]
6667
case None DbResultT.failure(notFoundFailure(name))
6768
}
6869
}

phoenix-scala/core/app/core/db/Star.scala

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ object * extends LazyLogging {
1717
def <~[A](v: SqlAction[A, NoStream, Effect.All])(implicit ec: EC): DbResultT[A] =
1818
<~(v.map(Either.right))
1919

20-
def <~[A](v: DBIO[A])(implicit ec: EC): DbResultT[A] =
21-
DbResultT.fromF(v)
20+
def <~[A](fa: DBIO[A])(implicit ec: EC): DbResultT[A] =
21+
DbResultT.fromF(fa)
2222

23-
def <~[A](v: Either[Failures, A])(implicit ec: EC): DbResultT[A] =
24-
DbResultT.fromEither(v)
23+
def <~[A](fa: Either[Failures, A])(implicit ec: EC): DbResultT[A] =
24+
DbResultT.fromEither(fa)
2525

26-
def <~[A](v: Future[Either[Failures, A]])(implicit M1: Monad[DBIO], M2: Monad[Future]): DbResultT[A] =
27-
DbResultT.fromResult(Result.fromFEither(v))
26+
def <~[A](gfa: Future[Either[Failures, A]])(implicit M1: Monad[DBIO], M2: Monad[Future]): DbResultT[A] =
27+
DbResultT.fromResult(Result.fromFEither(gfa))
2828

29-
def <~[A](v: Future[A])(implicit ec: EC): DbResultT[A] =
30-
<~(v.map(Either.right(_)).recover {
29+
def <~[A](fa: Future[A])(implicit ec: EC): DbResultT[A] =
30+
<~(fa.map(Either.right).recover {
3131
case ex
3232
logger.error("A Future failed during conversion to DbResultT.", ex)
3333
Either.left(GeneralFailure(ex.getMessage).single)
@@ -36,22 +36,25 @@ object * extends LazyLogging {
3636
def <~[A](fa: Result[A])(implicit ec: EC): DbResultT[A] =
3737
DbResultT.fromResult(fa)
3838

39-
def <~[A](v: A)(implicit ec: EC): DbResultT[A] =
40-
v.pure[DbResultT]
39+
// TODO: Is this more readable than inlining? @michalrus
40+
def <~[A](a: A)(implicit ec: EC): DbResultT[A] =
41+
a.pure[DbResultT]
4142

4243
def <~[A](v: Validated[Failures, A])(implicit ec: EC): DbResultT[A] =
4344
DbResultT.fromEither(v.toEither)
4445

45-
def <~[M[_]: TraverseFilter, A](v: M[DbResultT[A]])(implicit ec: EC): DbResultT[M[A]] =
46-
DbResultT.seqCollectFailures(v)
46+
def <~[M[_]: TraverseFilter, A](fas: M[DbResultT[A]])(implicit ec: EC): DbResultT[M[A]] =
47+
DbResultT.seqCollectFailures(fas)
4748

4849
// FIXME: Remove this function after switching all Seqs to List/Vector. Cats don’t have instances for Seq and Seq is unsound. PM me or @kjanosz for details. @michalrus
49-
def <~[A](v: Seq[DbResultT[A]])(implicit ec: EC): DbResultT[List[A]] =
50-
DbResultT.seqCollectFailures(v.toList)
50+
def <~[A](fas: Seq[DbResultT[A]])(implicit ec: EC): DbResultT[List[A]] =
51+
DbResultT.seqCollectFailures(fas.toList)
5152

52-
def <~[A](v: DbResultT[A]): DbResultT[A] =
53-
v
53+
// TODO: Is this more readable than inlining? @michalrus
54+
def <~[A](fa: DbResultT[A]): DbResultT[A] =
55+
fa
5456

55-
def <~[A](v: Option[DbResultT[A]])(implicit ec: EC): DbResultT[Option[A]] = // TODO: sequence? @michalrus - yes, please! @aafa
56-
v.fold(DbResultT.none[A])(_.map(Some(_)))
57+
// TODO: Is this more readable than inlining? @michalrus
58+
def <~[A](ofa: Option[DbResultT[A]])(implicit ec: EC): DbResultT[Option[A]] =
59+
ofa.sequence
5760
}

phoenix-scala/core/app/core/db/package.scala

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

phoenix-scala/objectframework/app/objectframework/IlluminateAlgorithm.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package objectframework
22

33
import java.time.Instant
44

5+
import cats.implicits._
56
import cats.data.NonEmptyList
67
import com.networknt.schema.JsonSchemaFactory
78
import com.typesafe.scalalogging.LazyLogging
@@ -34,7 +35,7 @@ object IlluminateAlgorithm extends LazyLogging {
3435
implicit ec: ExecutionContext): DbResultT[JValue] = {
3536
val illuminated = projectFlatAttributes(form.attributes, shadow.attributes)
3637
getInternalAttributes(schema).fold {
37-
DbResultT.good(illuminated)
38+
illuminated.pure[DbResultT]
3839
} { jsonSchema
3940
val jsonSchemaFactory = new JsonSchemaFactory
4041
val validator = jsonSchemaFactory.getSchema(asJsonNode(jsonSchema))
@@ -45,7 +46,7 @@ object IlluminateAlgorithm extends LazyLogging {
4546
ObjectValidationFailure(form.kind, shadow.id, err.getMessage)
4647
} match {
4748
case head :: tail DbResultT.failures[JValue](NonEmptyList(head, tail))
48-
case Nil DbResultT.good(illuminated)
49+
case Nil illuminated.pure[DbResultT]
4950
}
5051
}
5152
}

phoenix-scala/objectframework/app/objectframework/ObjectUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ object ObjectUtils {
184184
.copy[T](form = updateResult.form, shadow = updateResult.shadow)
185185
updateHead(newObject, commit.id)
186186
case _
187-
DbResultT.good(fullObject)
187+
fullObject.pure[DbResultT]
188188
})
189189
} yield committedObject
190190

phoenix-scala/objectframework/app/objectframework/models/ObjectHeadLinks.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ object ObjectHeadLinks {
8484
case right if !linkedRightIds.contains(right.id)
8585
create(build(left, right))
8686
}
87-
} yield {}
87+
} yield ()
8888

8989
def createIfNotExist(left: L, right: R)(implicit ec: EC, db: DB): DbResultT[Unit] =
9090
for {
9191
linkExists * <~ filterLeft(left).filter(_.rightId === right.id).exists.result
9292
_ * <~ when(!linkExists, create(build(left, right)).void)
93-
} yield {}
93+
} yield ()
9494

9595
def build(left: L, right: R): M
9696
}

phoenix-scala/objectframework/app/objectframework/models/OrderedLinks.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package objectframework.models
22

3+
import cats.implicits._
34
import core.db.ExPostgresDriver.api._
45
import core.db._
56
import objectframework.ObjectFailures.{LinkAtPositionCannotBeFound, LinkCannotBeFound}
@@ -44,7 +45,7 @@ abstract class OrderedObjectHeadLinkQueries[M <: OrderedObjectHeadLink[M],
4445
replacedLink * <~ allLefts
4546
.filter(_.position === newPosition)
4647
.mustFindOneOr(LinkAtPositionCannotBeFound(baseTableRow.getClass, left.id, newPosition))
47-
newLinks * <~ (if (link.position == newPosition) DbResultT.good((link, replacedLink))
48+
newLinks * <~ (if (link.position == newPosition) (link, replacedLink).pure[DbResultT]
4849
else swapLinkPositions(link, replacedLink))
4950
(updatedLink, _) = newLinks
5051
} yield updatedLink

phoenix-scala/objectframework/app/objectframework/payloads/ObjectSchemaValidation.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package objectframework.payloads
22

3+
import cats.implicits._
34
import cats.data.NonEmptyList
45
import com.networknt.schema.JsonSchemaFactory
56
import core.db._
@@ -35,7 +36,7 @@ object ObjectSchemaValidation {
3536
PayloadValidationFailure(error.getMessage)
3637
} match {
3738
case head :: tail DbResultT.failures[M](NonEmptyList(head, tail))
38-
case Nil DbResultT.good(payload)
39+
case Nil payload.pure[DbResultT]
3940
}
4041

4142
}

0 commit comments

Comments
 (0)