Skip to content

Commit

Permalink
Format with scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Oct 16, 2019
1 parent 4e6fef3 commit a139a1c
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 91 deletions.
29 changes: 29 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version=2.1.0
align.openParenCallSite = true
align.openParenDefnSite = true
maxColumn = 120
continuationIndent.defnSite = 2
continuationIndent.extendSite = 2

assumeStandardLibraryStripMargin = true
danglingParentheses = true
rewrite.rules = [AvoidInfix, SortImports, RedundantBraces, RedundantParens, SortModifiers, PreferCurlyFors]

align = some
align.arrowEnumeratorGenerator = true

newlines.alwaysBeforeTopLevelStatements = true

optIn.breakChainOnFirstMethodDot = false
includeCurlyBraceInSelectChains = true
includeNoParensInSelectChains = true

runner.optimizer.forceConfigStyleMinArgCount = 3

verticalMultiline.excludeDanglingParens = [
"`trait`"
]
verticalMultiline.newlineAfterOpenParen = true
verticalMultiline.newlineBeforeImplicitKW = true
verticalMultiline.newlineAfterImplicitKW = false
verticalMultilineAtDefinitionSite = true
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inThisBuild(
)

val compilerPlugins = List(
compilerPlugin("org.typelevel" % "kind-projector" % kindProjectorVersion cross CrossVersion.full)
compilerPlugin(("org.typelevel" % "kind-projector" % kindProjectorVersion).cross(CrossVersion.full))
)

val commonSettings = Seq(
Expand Down
11 changes: 6 additions & 5 deletions microsite/src/main/scala/sup/microsite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ object microsite {
if (rest.isEmpty) s" += ${sbtDependency(module1)}"
else
s""" ++= Seq(
| ${sbtDependency(module1)},
| ${rest.map(sbtDependency).mkString(",\n ")}
|)""".stripMargin
| ${sbtDependency(module1)},
| ${rest.map(sbtDependency).mkString(",\n ")}
|)""".stripMargin

println("```scala\n" + prefix + remaining + "\n```")
}

val version = sup.buildinfo.BuildInfo.lastStableVersion

def sbtDependency(moduleName: String): String =
s""""com.kubukoz" %% "sup-$moduleName" % "$version""""

def ammDependency(moduleName: String): Unit =
println(s"""```
|import $$ivy.`com.kubukoz::sup-$moduleName:$version`
|```""".stripMargin)
|import $$ivy.`com.kubukoz::sup-$moduleName:$version`
|```""".stripMargin)
}
8 changes: 6 additions & 2 deletions modules/circe/src/main/scala/sup/modules/circe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import sup.HealthResult

object circe {
implicit val healthCirceEncoder: Encoder[Health] = Encoder[String].contramap(_.toString)

implicit val healthCirceDecoder: Decoder[Health] =
Decoder[String].emap(s => Health.fromString(s).toRight(s"$s is not a valid ${Health.getClass.getName}"))

implicit def taggedCirceEncoder[Tag: Encoder, H: Encoder]: Encoder[Tagged[Tag, H]] = Encoder.forProduct2("tag", "health")(tagged => (tagged.tag, tagged.health))
implicit def taggedCirceDecoder[Tag: Decoder, H: Decoder]: Decoder[Tagged[Tag, H]] = Decoder.forProduct2("tag", "health")(Tagged.apply)
implicit def taggedCirceEncoder[Tag: Encoder, H: Encoder]: Encoder[Tagged[Tag, H]] =
Encoder.forProduct2("tag", "health")(tagged => (tagged.tag, tagged.health))

implicit def taggedCirceDecoder[Tag: Decoder, H: Decoder]: Decoder[Tagged[Tag, H]] =
Decoder.forProduct2("tag", "health")(Tagged.apply)

implicit def reportCirceEncoder[G[_], H[_], A: Encoder](implicit H: Encoder[G[H[A]]]): Encoder[Report[G, H, A]] =
Encoder.forProduct2("health", "checks")(report => (report.health, report.checks))
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/sup/Health.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ sealed trait Health extends Product with Serializable {

object Health {
case object Healthy extends Health
case object Sick extends Health
case object Sick extends Health

val healthy: Health = Healthy
val sick: Health = Sick
val sick: Health = Sick

val fromBoolean: Boolean => Health = {
case true => Healthy
Expand All @@ -39,7 +39,7 @@ object Health {
* A monoid that'll return [[Sick]] if any of the combined values are sick, [[Healthy]] otherwise.
* */
implicit val allHealthyCommutativeMonoid: CommutativeMonoid[Health] = new CommutativeMonoid[Health] {
override val empty: Health = healthy
override val empty: Health = healthy
override def combine(x: Health, y: Health): Health = if (x.isHealthy) y else sick
}

Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/main/scala/sup/HealthCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object HealthCheck {
* Lifts a Boolean-returning action to a healthcheck that yields Sick if the action returns false.
* */
def liftFBoolean[F[_]: Functor](fb: F[Boolean]): HealthCheck[F, Id] =
liftF(fb.map(Health.fromBoolean andThen HealthResult.one))
liftF(fb.map(Health.fromBoolean.andThen(HealthResult.one)))

/**
* Combines two healthchecks by running the first one and recovering with the second one in case of failure in F.
Expand Down Expand Up @@ -115,6 +115,7 @@ object HealthCheck {
): Monoid[HealthCheck[F, H]] =
new Monoid[HealthCheck[F, H]] {
override val empty: HealthCheck[F, H] = HealthCheck.const[F, H](M.empty)

override def combine(x: HealthCheck[F, H], y: HealthCheck[F, H]): HealthCheck[F, H] = liftF {
Applicative.monoid[F, HealthResult[H]].combine(x.check, y.check)
}
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/main/scala/sup/HealthResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object HealthResult {
implicit def monoid[H[_]: Applicative](implicit M: Monoid[Health]): Monoid[HealthResult[H]] =
new Monoid[HealthResult[H]] {
override val empty: HealthResult[H] = HealthResult.const[H](M.empty)

override def combine(x: HealthResult[H], y: HealthResult[H]): HealthResult[H] =
HealthResult(Applicative.monoid[H, Health].combine(x.value, y.value))
}
Expand Down
13 changes: 10 additions & 3 deletions modules/core/src/main/scala/sup/data/HealthReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ object HealthReporter {
/**
* Equivalent to [[wrapChecks]] for G = NonEmptyList, with more pleasant syntax.
* */
def fromChecks[F[_]: Apply, H[_]: Reducible](first: HealthCheck[F, H], rest: HealthCheck[F, H]*): HealthReporter[F, NonEmptyList, H] =
def fromChecks[F[_]: Apply, H[_]: Reducible](
first: HealthCheck[F, H],
rest: HealthCheck[F, H]*
): HealthReporter[F, NonEmptyList, H] =
wrapChecks(NonEmptyList(first, rest.toList))

/**
Expand All @@ -24,7 +27,9 @@ object HealthReporter {
*
* e.g. if all checks need to return Healthy for the whole thing to be healthy, use [[Health.allHealthyCommutativeMonoid]].
*/
def wrapChecks[F[_]: Apply, G[_]: NonEmptyTraverse, H[_]: Reducible](checks: G[HealthCheck[F, H]]): HealthReporter[F, G, H] =
def wrapChecks[F[_]: Apply, G[_]: NonEmptyTraverse, H[_]: Reducible](
checks: G[HealthCheck[F, H]]
): HealthReporter[F, G, H] =
HealthCheck.liftF {

checks.nonEmptyTraverse(_.check).map(fromResults[G, H])
Expand All @@ -43,6 +48,8 @@ object HealthReporter {
Parallel.parNonEmptyTraverse(checks)(_.check).map(fromResults[G, H])
}

def fromResults[G[_]: Reducible: Functor, H[_]: Reducible](results: G[HealthResult[H]]): HealthResult[Report[G, H, ?]] =
def fromResults[G[_]: Reducible: Functor, H[_]: Reducible](
results: G[HealthResult[H]]
): HealthResult[Report[G, H, ?]] =
HealthResult(Report.fromResults[G, H, Health](results.map(_.value)))
}
3 changes: 2 additions & 1 deletion modules/core/src/main/scala/sup/data/Report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object Report extends ReportInstances {
}

trait ReportInstances {

implicit def catsReducibleForReport[G[_], H[_]](implicit F: Foldable[Nested[G, H, ?]]): Reducible[Report[G, H, ?]] =
new ReportReducible[G, H]

Expand All @@ -30,6 +31,6 @@ trait ReportInstances {
}

private[data] class ReportReducible[G[_], H[_]](implicit F: Foldable[Nested[G, H, ?]])
extends NonEmptyReducible[Report[G, H, ?], Nested[G, H, ?]] {
extends NonEmptyReducible[Report[G, H, ?], Nested[G, H, ?]] {
override def split[A](fa: Report[G, H, A]): (A, Nested[G, H, A]) = (fa.health, Nested(fa.checks))
}
2 changes: 2 additions & 0 deletions modules/core/src/main/scala/sup/data/Tagged.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ object Tagged {

def by[F[_], G[_]: Reducible](fg: F ~> G): Reducible[F] = new Reducible[F] {
override def reduceLeftTo[A, B](fa: F[A])(f: A => B)(g: (B, A) => B): B = Reducible[G].reduceLeftTo(fg(fa))(f)(g)

override def reduceRightTo[A, B](fa: F[A])(f: A => B)(g: (A, Eval[B]) => Eval[B]): Eval[B] =
Reducible[G].reduceRightTo(fg(fa))(f)(g)
override def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B = Reducible[G].foldLeft(fg(fa), b)(f)

override def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
Reducible[G].foldRight(fg(fa), lb)(f)
}
Expand Down
24 changes: 18 additions & 6 deletions modules/core/src/test/scala/sup/CatsTaglessInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ import org.scalacheck.{Arbitrary, Gen}
import scala.util.Try

object CatsTaglessInstances {

implicit val catsDataArbitraryOptionList: Arbitrary[FunctionK[Option, List]] = Arbitrary(
Gen.const(λ[FunctionK[Option, List]](_.toList)))
Gen.const(λ[FunctionK[Option, List]](_.toList))
)

implicit val catsDataArbitraryListOption: Arbitrary[FunctionK[List, Option]] = Arbitrary(
Gen.const(λ[FunctionK[List, Option]](_.headOption)))
Gen.const(λ[FunctionK[List, Option]](_.headOption))
)

implicit val catsDataArbitraryTryOption: Arbitrary[FunctionK[Try, Option]] = Arbitrary(
Gen.const(λ[FunctionK[Try, Option]](_.toOption)))
Gen.const(λ[FunctionK[Try, Option]](_.toOption))
)

implicit val catsDataArbitraryOptionTry: Arbitrary[FunctionK[Option, Try]] = Arbitrary(
Gen.const(λ[FunctionK[Option, Try]](o => Try(o.get))))
Gen.const(λ[FunctionK[Option, Try]](o => Try(o.get)))
)

implicit val catsDataArbitraryListVector: Arbitrary[FunctionK[List, Vector]] = Arbitrary(
Gen.const(λ[FunctionK[List, Vector]](_.toVector)))
Gen.const(λ[FunctionK[List, Vector]](_.toVector))
)

implicit val catsDataArbitraryVectorList: Arbitrary[FunctionK[Vector, List]] = Arbitrary(
Gen.const(λ[FunctionK[Vector, List]](_.toList)))
Gen.const(λ[FunctionK[Vector, List]](_.toList))
)

implicit val eqThrow: Eq[Throwable] = Eq.fromUniversalEquals
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import CatsTaglessInstances._
class HealthCheckCatsInstancesTests extends CatsSuite {
import ScalacheckInstances._

checkAll("FunctorK[HealthCheck[Option, ?[_]]", FunctorKTests[HealthCheck[Option, ?[_]]].functorK[Try, Option, List, Int])
checkAll(
"FunctorK[HealthCheck[Option, ?[_]]",
FunctorKTests[HealthCheck[Option, ?[_]]].functorK[Try, Option, List, Int]
)
checkAll("Monoid[HealthCheck[Try, Option]]", MonoidTests[HealthCheck[Try, Option]].monoid)
checkAll("Eq[HealthCheck[Try, Option]]", EqTests[HealthCheck[Try, Option]].eqv)
}
9 changes: 7 additions & 2 deletions modules/core/src/test/scala/sup/ScalacheckInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ import org.scalacheck.Arbitrary
import sup.data.Tagged

object ScalacheckInstances {

implicit def arbitraryTagged[Tag: Arbitrary, E: Arbitrary]: Arbitrary[Tagged[Tag, E]] = Arbitrary {
for {
tag <- Arbitrary.arbitrary[Tag]
tag <- Arbitrary.arbitrary[Tag]
elem <- Arbitrary.arbitrary[E]
} yield Tagged(tag, elem)
}

implicit def arbitraryHealthResult[H[_]](implicit F: Arbitrary[H[Health]]): Arbitrary[HealthResult[H]] = Arbitrary {
F.arbitrary.map(HealthResult(_))
}

implicit val arbitraryHealth: Arbitrary[Health] = Arbitrary(Arbitrary.arbitrary[Boolean].map(Health.fromBoolean))
implicit val cogenHealth: Cogen[Health] = Cogen.cogenBoolean.contramap(_.isHealthy)

implicit def arbitraryHealthCheck[F[_], H[_]](implicit A: Arbitrary[F[HealthResult[H]]]): Arbitrary[HealthCheck[F, H]] =
implicit def arbitraryHealthCheck[F[_], H[_]](
implicit A: Arbitrary[F[HealthResult[H]]]
): Arbitrary[HealthCheck[F, H]] =
Arbitrary(A.arbitrary.map(HealthCheck.liftF))

implicit def cogenHealthResult[H[_]](implicit C: Cogen[H[Health]]): Cogen[HealthResult[H]] = C.contramap(_.value)

implicit def cogenHealthCheck[F[_], H[_]](implicit C: Cogen[F[HealthResult[H]]]): Cogen[HealthCheck[F, H]] =
C.contramap(_.check)

Expand Down
6 changes: 5 additions & 1 deletion modules/doobie/src/main/scala/sup/modules/doobie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ object doobie {
* Note: Errors aren't recovered in this healthcheck. If you want error handling,
* consider using [[HealthCheck.through]] with [[sup.mods.recoverToSick]].
* */
def connectionCheck[F[_]: Bracket[?[_], Throwable]](xa: Transactor[F])(timeout: Option[FiniteDuration]): HealthCheck[F, Id] = {
def connectionCheck[F[_]: Bracket[?[_], Throwable]](
xa: Transactor[F]
)(
timeout: Option[FiniteDuration]
): HealthCheck[F, Id] = {
//zero means infinite in JDBC
val actualTimeoutSeconds = timeout.foldMap(_.toSeconds.toInt)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ object http4sclient {
/**
* Fetches the result of a remote health check.
* */
def remoteHealthCheck[F[_], H[_]](call: Request[F])(implicit client: Client[F],
decoder: EntityDecoder[F, HealthResult[H]]): HealthCheck[F, H] =
def remoteHealthCheck[F[_], H[_]](
call: Request[F]
)(
implicit client: Client[F],
decoder: EntityDecoder[F, HealthResult[H]]
): HealthCheck[F, H] =
HealthCheck.liftF(client.expect[HealthResult[H]](call))
}
12 changes: 9 additions & 3 deletions modules/http4s/src/main/scala/sup/modules/http4s.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ object http4s {
def healthCheckRoutes[F[_]: Sync, H[_]: Reducible](
healthCheck: HealthCheck[F, H],
path: String = "health-check"
)(implicit encoder: EntityEncoder[F, HealthResult[H]], combineHealthChecks: Semigroup[Health]): HttpRoutes[F] = {
)(
implicit encoder: EntityEncoder[F, HealthResult[H]],
combineHealthChecks: Semigroup[Health]
): HttpRoutes[F] = {

val dsl = new Http4sDsl[F] {}
import dsl._
Expand All @@ -31,9 +34,12 @@ object http4s {
}
}

def healthCheckResponse[F[_]: Monad, H[_]: Reducible](healthCheck: HealthCheck[F, H])(
def healthCheckResponse[F[_]: Monad, H[_]: Reducible](
healthCheck: HealthCheck[F, H]
)(
implicit encoder: EntityEncoder[F, HealthResult[H]],
combineHealthChecks: Semigroup[Health]): F[Response[F]] = {
combineHealthChecks: Semigroup[Health]
): F[Response[F]] = {

val dsl = new Http4sDsl[F] {}
import dsl._
Expand Down
9 changes: 5 additions & 4 deletions modules/log4cats/src/main/scala/sup/modules/log4cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ object log4cats {
* Wraps a healthcheck with a debug log message before and after making the call.
* See [[mods.surround]] for a more customizable version.
* */
def logged[F[_]: MessageLogger: FlatMap, H[_]](label: String)(
implicit showH: Show[H[Health]]): HealthCheckEndoMod[F, H] = {

def logged[F[_]: MessageLogger: FlatMap, H[_]](
label: String
)(
implicit showH: Show[H[Health]]
): HealthCheckEndoMod[F, H] =
mods.surround(MessageLogger[F].debug(show"Checking health for $label")) { result =>
MessageLogger[F].debug(show"Health result for $label: ${result.value}")
}
}
}
8 changes: 5 additions & 3 deletions modules/redis/src/main/scala/sup/modules/redis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ object redis {
* Creates a healthcheck for a redis4cats connection. If the check fails, the result is [[Health.Sick]].
* */
def pingCheck[F[_], E](implicit cmd: Ping[F], F: ApplicativeError[F, E]): HealthCheck[F, Id] =
HealthCheck.liftF {
cmd.ping.as(HealthResult.one(Health.healthy))
}.through(sup.mods.recoverToSick)
HealthCheck
.liftF {
cmd.ping.as(HealthResult.one(Health.healthy))
}
.through(sup.mods.recoverToSick)
}
14 changes: 9 additions & 5 deletions modules/scalacache/src/main/scala/sup/modules/scalacache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ object scalacache {
/**
* Caches a healthcheck for the given amount of time (or forever, if `ttl` is empty).
* */
def cached[F[_], H[_]](key: String, ttl: Option[Duration])(implicit cache: Cache[HealthResult[H]],
mode: Mode[F],
flags: Flags): HealthCheckEndoMod[F, H] = _.transform {
action =>
cache.cachingForMemoizeF(key)(ttl)(action)
def cached[F[_], H[_]](
key: String,
ttl: Option[Duration]
)(
implicit cache: Cache[HealthResult[H]],
mode: Mode[F],
flags: Flags
): HealthCheckEndoMod[F, H] = _.transform { action =>
cache.cachingForMemoizeF(key)(ttl)(action)
}
}
Loading

0 comments on commit a139a1c

Please sign in to comment.