This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val ipValidationF: F[ValidatedNel[PVError, String]] = | |
clientIp | |
.toValidNel(PVError.noIp) | |
.traverse(checkIp(_, hostname, normalizedPath)) | |
.map(_.andThen(identity) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val pathValidationF: F[ValidatedNel[PVError, String]] = | |
pageExists(hostname, normalizedPath).map( | |
Validated.condNel(_, normalizedPath, PVError.pageNotFound)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(uuidF, currentTimeF, ipValidationF, pathValidationF).traverseN { | |
(uuid, currentTime, ipValidation, pathValidation) => ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val timestampV: ValidatedNel[PVError, OffsetDateTime] = event | |
.displayedAt | |
.valid | |
.ensure(PVError.invalidTimestamp)(_.isBefore(currentTime)) | |
.toValidatedNel | |
( | |
uuid.valid, | |
pathValidation, | |
timestampV, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(uuidF, currentTimeF, ipValidationF, pathValidationF).traverseN { | |
(uuid, currentTime, ipValidation, pathValidation) => | |
val timestampV = event | |
.displayedAt | |
.valid | |
.ensure(PVError.invalidTimestamp)(_.isBefore(currentTime)) | |
.toValidatedNel | |
( | |
uuid.valid, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Column | Type | | |
------------------------------------------+ | |
| id | uuid | | |
| path | text | | |
| displayed_at | timestamp with time zone | | |
| hostname | text | | |
| client_ip | text | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def persist(pageView: PageView): F[Unit] = | |
sql""" | |
insert into page_views(id, path, displayed_at, hostname, client_ip) value | |
(${pageView.id}, ${pageView.path}, ${pageView.displayedAt}, ${pageView.hostname}, ${pageView.clientIp}) | |
""".update.run.transact(transactor).void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sideEffecting(): Map[String, List[String]] = { println("bazinga"); Map("a" -> List("a", "b")) } | |
//doesn't print anything here | |
val x1: String => List[String] = sideEffecting().getOrElse(_, Nil) | |
//but it'll print bazinga any time you call one of these: | |
x1("a") //here | |
x1("a") //also here | |
x1("b") //here too |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HasKF[F[_]] private { | |
type KF[A, B] = Kleisli[F, A, B] | |
} | |
object HasKF { | |
private val s: HasKF[Id] = new HasKF[Id] | |
def apply[F[_]]: HasKF[F] = s.asInstanceOf[HasKF[F]] | |
} | |
class AService[F[_] : Applicative] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait SlickRunIOImplicits { self: API => | |
implicit class SlickRunIO(db: Database) { | |
/** Return an effect that runs a DB action. */ | |
def runIO[R](a: DBIOAction[R, NoStream, Nothing])(implicit cs: ContextShift[IO]): IO[R] = | |
IO.fromFuture(IO(db.run(a))).guarantee(cs.shift) | |
/** Return an effect that runs a DB action with timing metrics collected */ | |
def runIO[R](a: DBIOAction[R, NoStream, Nothing], timerName: String)( | |
implicit metric: MetricsF |