Skip to content

Commit fd29f48

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

File tree

89 files changed

+325
-315
lines changed

Some content is hidden

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

89 files changed

+325
-315
lines changed

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

+1-1
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

+1-1
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

+5-4
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

+21-18
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

+28-31
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ package object db {
8686
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
8787
fa.map(rb).handleError(ra)
8888

89-
def meh(implicit M: Monad[F]): FoxyT[F, Unit] =
90-
fa.void // TODO: remove me? But it’s cute… @michalrus
91-
9289
def failuresToWarnings(valueIfWasFailed: A)(pf: PartialFunction[Failure, Boolean])(
9390
implicit F: Monad[F]): FoxyT[F, A] =
9491
fa.handleErrorWith { fs
@@ -105,15 +102,6 @@ package object db {
105102
}
106103

107104
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-
117105
def uiWarning(f: Failure)(implicit F: Monad[F]): FoxyT[F, Unit] =
118106
StateT.modify(MetaResponse.Warning(f) :: _)
119107

@@ -149,6 +137,8 @@ package object db {
149137
case _ EitherT.right(F.pure((s, ())))
150138
})
151139

140+
// TODO: maybe move the quasi-`sequence`-s from Functions to Ops? @michalrus
141+
152142
/** Just like ``sequence`` but—in case of a failure—unlawful, as it will join failures from all Foxies. */
153143
def seqCollectFailures[L[_], A](lfa: L[FoxyT[F, A]])(implicit L: TraverseFilter[L],
154144
F: Monad[F]): FoxyT[F, L[A]] =
@@ -270,32 +260,40 @@ package object db {
270260
def appendForUpdate[A, B <: slick.dbio.NoStream](sql: SqlAction[A, B, Effect.Read]): DBIO[A] =
271261
sql.overrideStatements(sql.statements.map(_ + " for update"))
272262

273-
// TODO: I don’t know… does this help? @michalrus
263+
// TODO: Is this more readable than inlining? @michalrus
274264
def ifElse[A](condition: Boolean, ifBranch: DbResultT[A], elseBranch: DbResultT[A]) =
275265
if (condition) ifBranch else elseBranch
276266

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]
279269

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]
282273

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)
286278

279+
// TODO: Is this more readable than inlining? @michalrus
280+
// FIXME: should be defined over FoxyT, but inference fails then… @michalrus
287281
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]
289283

284+
// TODO: Is this more readable than inlining? @michalrus
285+
// FIXME: should be defined over FoxyT, but inference fails then… @michalrus
290286
def failIfNot(condition: Boolean, failure: Failure)(implicit ec: EC): DbResultT[Unit] =
291287
failIf(!condition, failure)
292288

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] =
294292
failures match {
295293
case head :: tail
296-
DbResultT.failures(NonEmptyList.of(head, tail: _*))
294+
FoxyT[F].failures(NonEmptyList.of(head, tail: _*))
297295
case _
298-
DbResultT.unit
296+
().pure[FoxyT[F, ?]]
299297
}
300298

301299
implicit class EnrichedSQLActionBuilder(val action: SQLActionBuilder) extends AnyVal {
@@ -329,33 +327,32 @@ package object db {
329327

330328
def findOrCreate(r: DbResultT[R])(implicit ec: EC): DbResultT[R] =
331329
dbio.dbresult.flatMap {
332-
case Some(model) DbResultT.good(model)
330+
case Some(model) model.pure[DbResultT]
333331
case None r
334332
}
335333

336334
// Last item in tuple determines if cart was created or not
337335
def findOrCreateExtended(r: DbResultT[R])(implicit ec: EC): DbResultT[(R, FoundOrCreated)] =
338336
dbio.dbresult.flatMap {
339-
case Some(model) DbResultT.good((model, Found))
337+
case Some(model) (model, Found: FoundOrCreated).pure[DbResultT]
340338
case _ r.map(result (result, Created))
341339
}
342340

343341
def mustFindOr(notFoundFailure: Failure)(implicit ec: EC): DbResultT[R] =
344342
dbio.dbresult.flatMap {
345-
case Some(model) DbResultT.good(model)
343+
case Some(model) model.pure[DbResultT]
346344
case None DbResultT.failure(notFoundFailure)
347345
}
348346

349347
def mustNotFindOr(shouldNotBeHere: Failure)(implicit ec: EC): DbResultT[Unit] =
350348
dbio.dbresult.flatMap {
351-
case None DbResultT.unit
349+
case None ().pure[DbResultT]
352350
case Some(_) DbResultT.failure(shouldNotBeHere)
353351
}
354352

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
356354
// has a FK constraint to another table and you then fetch that associated record -- we already *know* it must
357355
// 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)
360357
}
361358
}

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

+3-2
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

+1-1
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

+2-2
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

+2-1
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

+2-1
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
}

phoenix-scala/objectframework/app/objectframework/services/ContentManager.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object ContentManager {
5252

5353
type FullContentRelations = Map[String, Seq[Content]]
5454
def getRelations(content: Content)(implicit ec: EC): DbResultT[FullContentRelations] = {
55-
val empty = (Map.empty : FullContentRelations).pure[DbResultT]
55+
val empty = (Map.empty: FullContentRelations).pure[DbResultT]
5656

5757
content.relations.foldLeft(empty) { (accRelations, relation)
5858
accRelations.flatMap { relations
@@ -86,7 +86,7 @@ object ContentManager {
8686
acc :+ (for {
8787
actualIds * <~ ContentQueries.filterCommitIds(kind, expectedIds).result
8888
_ * <~ validateAllCommits(kind, expectedIds, actualIds)
89-
} yield {})
89+
} yield ())
9090
}
9191

9292
private def validateAllCommits(kind: String,

phoenix-scala/phoenix/app/phoenix/models/account/User.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ object Users extends FoxTableQuery[User, Users](new Users(_)) with ReturningId[U
139139
maybeEmail match {
140140
case Some(email)
141141
otherUserByEmail(email, accountId).one.mustNotFindOr(UserEmailNotUnique)
142-
case None DbResultT.unit
142+
case None ().pure[DbResultT]
143143
}
144144

145145
}

phoenix-scala/phoenix/app/phoenix/models/activity/Dimension.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package phoenix.models.activity
22

3+
import cats.implicits._
34
import cats.data.ValidatedNel
45
import core.db.ExPostgresDriver.api._
56
import core.db._
@@ -58,7 +59,7 @@ object Dimensions
5859

5960
def findOrCreateByName(name: String)(implicit ec: EC): DbResultT[Dimension] =
6061
findByName(name).one.dbresult.flatMap {
61-
case Some(dimension) DbResultT.good(dimension)
62+
case Some(dimension) dimension.pure[DbResultT]
6263
case None create(Dimension(name = name, description = name.capitalize))
6364
}
6465
}

phoenix-scala/phoenix/app/phoenix/models/coupon/IlluminatedCoupon.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ case class IlluminatedCoupon(id: Int, context: IlluminatedContext, attributes: J
2020

2121
implicit val formats = JsonFormatters.phoenixFormats
2222

23+
// FIXME: duplicated code with IlluminatedModel? @michalrus
2324
def mustBeActive: Either[Failures, IlluminatedCoupon] = {
2425
val activeFrom = (attributes \ "activeFrom" \ "v").extractOpt[Instant]
2526
val activeTo = (attributes \ "activeTo" \ "v").extractOpt[Instant]
@@ -58,7 +59,7 @@ case class IlluminatedCoupon(id: Int, context: IlluminatedContext, attributes: J
5859
.couponMustBeUsable(id, accountId, rules.usesPerCustomer.getOrElse(0), code.code)
5960

6061
case Some(rules) if rules.isUnlimitedPerCode && rules.isUnlimitedPerCustomer
61-
DbResultT.unit
62+
().pure[DbResultT]
6263

6364
case _
6465
DbResultT.failure(CouponUsageRulesAreEmpty(code.code))

0 commit comments

Comments
 (0)