From cdaef3735d132f4dd3a901c7880c90c8a8aef883 Mon Sep 17 00:00:00 2001 From: Michael Bryzek Date: Wed, 1 Nov 2023 11:53:28 -0400 Subject: [PATCH] FIN-1544: Standard scalafmt settings (#281) --- .scalafmt.conf | 9 ++++ Jenkinsfile | 2 +- build.sbt | 8 ++-- project/plugins.sbt | 7 +-- src/main/scala/io/flow/log/LogUtil.scala | 16 +++---- src/main/scala/io/flow/log/Rollbar.scala | 17 ++++--- .../scala/io/flow/log/RollbarLogger.scala | 44 +++++++++---------- src/test/scala/io/flow/log/LogUtilSpec.scala | 4 +- 8 files changed, 61 insertions(+), 46 deletions(-) create mode 100644 .scalafmt.conf diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..45c7477 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,9 @@ +version = 3.5.9 +runner.dialect=scala213 +maxColumn = 120 +continuationIndent.callSite = 2 +continuationIndent.defnSite = 2 +continuationIndent.ctorSite = 2 +continuationIndent.extendSite = 2 +align.preset = none +project.excludePaths = [ "glob:**/generated/**" ] diff --git a/Jenkinsfile b/Jenkinsfile index f44ae6d..6214e0e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -34,7 +34,7 @@ pipeline { script { try { sh ''' - sbt clean compile flowLintLib test doc + sbt clean compile flowLintLib test scalafmtSbtCheck scalafmtCheck doc ''' } finally { junit allowEmptyResults: true, testResults: '**/target/test-reports/*.xml' diff --git a/build.sbt b/build.sbt index c4a6978..ce84430 100644 --- a/build.sbt +++ b/build.sbt @@ -14,10 +14,11 @@ lazy val allScalacOptions = Seq( "-unchecked", "-Xcheckinit", "-Xlint:adapted-args", - "-Ypatmat-exhaust-depth", "100", // Fixes: Exhaustivity analysis reached max recursion depth, not all missing cases are reported. + "-Ypatmat-exhaust-depth", + "100", // Fixes: Exhaustivity analysis reached max recursion depth, not all missing cases are reported. "-Wconf:src=generated/.*:silent", "-Wconf:src=target/.*:silent", // silence the unused imports errors generated by the Play Routes - "-release:17", + "-release:17" ) libraryDependencies ++= Seq( @@ -31,7 +32,7 @@ libraryDependencies ++= Seq( "net.logstash.logback" % "logstash-logback-encoder" % "6.3", // structured logging to sumo "org.scalatest" %% "scalatest" % "3.2.17" % Test, "com.typesafe.play" %% "play-json" % "2.9.4", - "com.typesafe.play" %% "play-json-joda" % "2.9.4", + "com.typesafe.play" %% "play-json-joda" % "2.9.4" ) resolvers += "Artifactory" at "https://flow.jfrog.io/flow/libs-release/" @@ -56,3 +57,4 @@ publishTo := { } scalacOptions ++= allScalacOptions +scalafmtOnCompile := true diff --git a/project/plugins.sbt b/project/plugins.sbt index 77daf52..9899963 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -6,9 +6,10 @@ resolvers += "Flow Plugins" at "https://flow.jfrog.io/flow/plugins-release/" addSbtPlugin("io.flow" % "sbt-flow-linter" % "0.0.41") // Resolve scala-xml version dependency mismatch, see https://github.com/sbt/sbt/issues/7007 - ThisBuild / libraryDependencySchemes ++= Seq( - "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always - ) +ThisBuild / libraryDependencySchemes ++= Seq( + "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always +) addSbtPlugin("com.github.sbt" % "sbt-git" % "2.0.1") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") diff --git a/src/main/scala/io/flow/log/LogUtil.scala b/src/main/scala/io/flow/log/LogUtil.scala index bf9ba8a..7c6d826 100644 --- a/src/main/scala/io/flow/log/LogUtil.scala +++ b/src/main/scala/io/flow/log/LogUtil.scala @@ -9,9 +9,9 @@ class LogUtil @Inject() (logger: RollbarLogger) { import RollbarLogger.Keys - /** - * @param frequency Log once per frequency. For instance, 100 means that the duration will be logged once every 100 - * calls on average. + /** @param frequency + * Log once per frequency. For instance, 100 means that the duration will be logged once every 100 calls on + * average. */ def duration[T]( info: String, @@ -22,7 +22,7 @@ class LogUtil @Inject() (logger: RollbarLogger) { orderNumber: Option[String] = None, requestId: Option[String] = None, data: Option[Map[String, String]] = None, - frequency: Long = 1L, + frequency: Long = 1L )(f: => T): T = { if (shouldLog(frequency)) { val start = System.currentTimeMillis() @@ -47,9 +47,9 @@ class LogUtil @Inject() (logger: RollbarLogger) { f } - /** - * @param frequency Log once per frequency. For instance, 100 means that the duration will be logged once every 100 - * calls on average. + /** @param frequency + * Log once per frequency. For instance, 100 means that the duration will be logged once every 100 calls on + * average. */ def durationF[T]( info: String, @@ -60,7 +60,7 @@ class LogUtil @Inject() (logger: RollbarLogger) { orderNumber: Option[String] = None, requestId: Option[String] = None, data: Option[Map[String, String]] = None, - frequency: Long = 1L, + frequency: Long = 1L )(f: => Future[T])(implicit ec: ExecutionContext): Future[T] = { if (shouldLog(frequency)) { val start = System.currentTimeMillis() diff --git a/src/main/scala/io/flow/log/Rollbar.scala b/src/main/scala/io/flow/log/Rollbar.scala index 33aa8ed..367f175 100644 --- a/src/main/scala/io/flow/log/Rollbar.scala +++ b/src/main/scala/io/flow/log/Rollbar.scala @@ -27,9 +27,8 @@ class RollbarModule extends AbstractModule with ScalaModule { } } -/** - * We use a provider because we don't want to keep initializing a new Rollbar notifier - * every time an instance of the logger is instantiated. +/** We use a provider because we don't want to keep initializing a new Rollbar notifier every time an instance of the + * logger is instantiated. * * Returns an optional Rollbar instance so a key isn't required in test and development mode. */ @@ -52,7 +51,7 @@ class RollbarLoggerProvider @Inject() ( // Necessary evil to allow us to copy instances of RollbarLogger, letting us have // nice methods like `withKeyValue` @Singleton -class RollbarFactory @Inject()( +class RollbarFactory @Inject() ( rollbarProvider: Provider[Option[Rollbar]] ) extends RollbarLogger.Factory { @AssistedInject @@ -132,9 +131,13 @@ object RollbarProvider { resultOpt match { case JsSuccess(res, _) => res - case JsError(errors) => new Result.Builder().code(-1).body( - JsError.toJson(errors).toString - ).build + case JsError(errors) => + new Result.Builder() + .code(-1) + .body( + JsError.toJson(errors).toString + ) + .build } } } diff --git a/src/main/scala/io/flow/log/RollbarLogger.scala b/src/main/scala/io/flow/log/RollbarLogger.scala index cf06493..58fec9c 100644 --- a/src/main/scala/io/flow/log/RollbarLogger.scala +++ b/src/main/scala/io/flow/log/RollbarLogger.scala @@ -12,16 +12,19 @@ import scala.util.Random object RollbarLogger { - /** - * Simple [[RollbarLogger]] that does not log to Rollbar. - * Useful for tests requiring a [[RollbarLogger]]. + /** Simple [[RollbarLogger]] that does not log to Rollbar. Useful for tests requiring a [[RollbarLogger]]. */ val SimpleLogger: RollbarLogger = RollbarLogger(rollbar = None, attributes = Map.empty, legacyMessage = None, shouldSendToRollbar = false) trait Factory { @AssistedInject - def rollbar(attributes: Map[String, JsValue], legacyMessage: Option[String], shouldSendToRollbar: Boolean, frequency: Long): RollbarLogger + def rollbar( + attributes: Map[String, JsValue], + legacyMessage: Option[String], + shouldSendToRollbar: Boolean, + frequency: Long + ): RollbarLogger } object Keys { @@ -52,14 +55,13 @@ case class RollbarLogger @AssistedInject() ( private val logger = LoggerFactory.getLogger("application") - /** - * Log once per frequency. - * For instance, 100 means that the message will be logged once every 100 calls on average. + /** Log once per frequency. For instance, 100 means that the message will be logged once every 100 calls on average. */ def withFrequency(frequency: Long): RollbarLogger = this.copy(frequency = frequency) def withKeyValue[T: Writes](keyValue: (String, T)): RollbarLogger = withKeyValue(keyValue._1, keyValue._2) - def withKeyValue[T: Writes](key: String, value: T): RollbarLogger = this.copy(attributes = attributes + (key -> Json.toJson(value))) + def withKeyValue[T: Writes](key: String, value: T): RollbarLogger = + this.copy(attributes = attributes + (key -> Json.toJson(value))) def fingerprint(value: String): RollbarLogger = withKeyValue(Keys.Fingerprint, value) def organization(value: String): RollbarLogger = withKeyValue(Keys.Organization, value) def channelId(value: String): RollbarLogger = withKeyValue(Keys.ChannelId, value) @@ -69,28 +71,25 @@ case class RollbarLogger @AssistedInject() ( def itemNumber(value: String): RollbarLogger = withKeyValue(Keys.ItemNumber, value) def experienceKey(value: String): RollbarLogger = withKeyValue(Keys.ExperienceKey, value) - /** - * Use for warnings or errors that: - * - are very high volume - * - should be recorded for audit purposes but no action needs to be taken + /** Use for warnings or errors that: + * - are very high volume + * - should be recorded for audit purposes but no action needs to be taken * * Structured errors will still be sent to Sumo. */ - def withSendToRollbar(sendToRollbar:Boolean): RollbarLogger = this.copy(shouldSendToRollbar = sendToRollbar) + def withSendToRollbar(sendToRollbar: Boolean): RollbarLogger = this.copy(shouldSendToRollbar = sendToRollbar) def withKeyValues[T: Writes](keyValue: (String, Seq[T])): RollbarLogger = withKeyValues(keyValue._1, keyValue._2) - /** - * Accepts a list of values and writes them as individual attributes. - * for example: - * withKeyValues("error", Seq("foo", "bar")) + /** Accepts a list of values and writes them as individual attributes. for example: withKeyValues("error", Seq("foo", + * "bar")) * * results in the attributes: - * - error_1: foo - * - error_2: bar + * - error_1: foo + * - error_2: bar */ - def withKeyValues[T: Writes](key: String, values: Seq[T])( - implicit maxValues: Int = MaxValuesToWrite + def withKeyValues[T: Writes](key: String, values: Seq[T])(implicit + maxValues: Int = MaxValuesToWrite ): RollbarLogger = { val logger = values.take(maxValues).zipWithIndex.foldLeft(this) { case (l, pair) => val value = pair._1 @@ -105,7 +104,8 @@ case class RollbarLogger @AssistedInject() ( } } - def withKeyValues[T: Writes](key: String, values: NonEmptyChain[T]): RollbarLogger = withKeyValues(key, values.toNonEmptyList.toList) + def withKeyValues[T: Writes](key: String, values: NonEmptyChain[T]): RollbarLogger = + withKeyValues(key, values.toNonEmptyList.toList) def debug(message: => String): Unit = debug(message, null) def info(message: => String): Unit = info(message, null) diff --git a/src/test/scala/io/flow/log/LogUtilSpec.scala b/src/test/scala/io/flow/log/LogUtilSpec.scala index 0b5816b..645401b 100644 --- a/src/test/scala/io/flow/log/LogUtilSpec.scala +++ b/src/test/scala/io/flow/log/LogUtilSpec.scala @@ -22,7 +22,7 @@ class LogUtilSpec extends AnyWordSpec with Matchers { callCount.incrementAndGet() } logUtil.duration(info = "test", fingerprint = "test", organizationId = "test")(f) - callCount.get must be (1) + callCount.get must be(1) } } @@ -36,7 +36,7 @@ class LogUtilSpec extends AnyWordSpec with Matchers { logUtil.durationF(info = "test", fingerprint = "test", organizationId = "test")(f), 10.millis ) - callCount.get must be (1) + callCount.get must be(1) } } }