Skip to content

Commit

Permalink
Scalafix (#106)
Browse files Browse the repository at this point in the history
* configure scalafix plugin

* Configure scalafix

* Fix build

* Apply scalafix
  • Loading branch information
mkuthan authored Sep 23, 2023
1 parent 355fbfa commit 97c9f50
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 62 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ jobs:
with:
name: scapegoat-report
path: '**/scapegoat-report/*.html'


scalafix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- run: sbt "scalafixAll --check"

test:
runs-on: ubuntu-latest
needs: [scalafmt, scapegoat, scalafix]
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
Expand Down
7 changes: 7 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules = [
ExplicitResultTypes
LeakingImplicitClassVal
ProcedureSyntax
RedundantSyntax
RemoveUnused
]
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt._
import Dependencies._
import Settings._

addCommandAlias("check", "clean; scalafmtAll; scapegoat; testOnly -- -l org.scalatest.tags.Slow")
addCommandAlias("check", "clean; scalafixAll; scalafmtAll; scapegoat; testOnly -- -l org.scalatest.tags.Slow")

lazy val root = (project in file("."))
.settings(
Expand Down
14 changes: 10 additions & 4 deletions project/Settings.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import sbt.*
import sbt.nio.Keys.*
import sbt.Keys.*
import sbt._
import sbt.nio.Keys.onChangedBuildSource
import sbt.nio.Keys.ReloadOnSourceChanges
import sbt.Keys._

import com.github.sbt.jacoco.JacocoKeys.jacocoReportSettings
import com.github.sbt.jacoco.JacocoKeys.JacocoReportFormats
import com.github.sbt.jacoco.JacocoPlugin.autoImport.JacocoReportSettings
import scalafix.sbt.ScalafixPlugin.autoImport._

object Settings {
val commonSettings = Seq(
Expand Down Expand Up @@ -39,6 +41,10 @@ object Settings {
excludeDependencies += "commons-logging" % "commons-logging",
// enable XML report for codecov
jacocoReportSettings := JacocoReportSettings()
.withFormats(JacocoReportFormats.XML, JacocoReportFormats.HTML)
.withFormats(JacocoReportFormats.XML, JacocoReportFormats.HTML),
// scalafix
ThisBuild / semanticdbEnabled := true,
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision,
ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value)
)
}
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ addSbtPlugin("com.github.sbt" % "sbt-jacoco" % "3.4.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
// code analysis
addSbtPlugin("com.sksamuel.scapegoat" %% "sbt-scapegoat" % "1.2.1")
// automated code changes
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.1")
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ case class MaxBatchBytesSize(value: Int) extends PubsubWriteParam {
}

object MaxBatchBytesSize {
val HighThroughput = MaxBatchBytesSize(8 * 1024 * 1024)
val HighThroughput: MaxBatchBytesSize = MaxBatchBytesSize(8 * 1024 * 1024)
}

case class MaxBatchSize(value: Int) extends PubsubWriteParam {
Expand All @@ -74,5 +74,5 @@ case class MaxBatchSize(value: Int) extends PubsubWriteParam {
}

object MaxBatchSize {
val HighThroughput = MaxBatchSize(1000)
val HighThroughput: MaxBatchSize = MaxBatchSize(1000)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ trait IntegrationTestFixtures extends JodaTimeArbitrary {
)
}

val SampleClassBigQueryType = BigQueryType[SampleClass]
val SampleClassBigQueryType: BigQueryType[SampleClass] = BigQueryType[SampleClass]
val SampleClassBigQuerySchema = SampleClassBigQueryType.schema

val SampleObject1 = SampleClass(
val SampleObject1: SampleClass = SampleClass(
stringField = "complex 1",
optionalStringField = Some("complex 1"),
intField = 1,
Expand All @@ -44,9 +44,9 @@ trait IntegrationTestFixtures extends JodaTimeArbitrary {
localDateField = LocalDate.parse("2014-09-10")
)

val SampleJson1 = JsonSerde.writeJsonAsBytes(SampleObject1)
val SampleJson1: Array[Byte] = JsonSerde.writeJsonAsBytes(SampleObject1)

val SampleObject2 = SampleClass(
val SampleObject2: SampleClass = SampleClass(
stringField = "complex 2",
optionalStringField = None,
intField = 2,
Expand All @@ -55,12 +55,12 @@ trait IntegrationTestFixtures extends JodaTimeArbitrary {
localDateField = LocalDate.parse("2014-09-10")
)

val SampleJson2 = JsonSerde.writeJsonAsBytes(SampleObject2)
val SampleJson2: Array[Byte] = JsonSerde.writeJsonAsBytes(SampleObject2)

val InvalidJson = "invalid json".getBytes(StandardCharsets.UTF_8)
val InvalidJson: Array[Byte] = "invalid json".getBytes(StandardCharsets.UTF_8)

val SampleMap1 = Map("key1" -> "value1")
val SampleMap2 = Map("key2" -> "value2")
val SampleMap1: Map[String, String] = Map("key1" -> "value1")
val SampleMap2: Map[String, String] = Map("key2" -> "value2")
}

object IntegrationTestFixtures {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ class IoDiagnosticTest extends AnyFlatSpec with Matchers
with IntegrationTestScioContext
with BigQueryContext {

val ioDiagnosticType = BigQueryType[IoDiagnostic.Record]
val ioDiagnosticType: BigQueryType[IoDiagnostic.Record] = BigQueryType[IoDiagnostic.Record]

val anyIoDiagnostic = IoDiagnostic(
val anyIoDiagnostic: IoDiagnostic = IoDiagnostic(
id = "any-id",
reason = "any reason"
)

val ioDiagnostic1 = anyIoDiagnostic.copy(id = "id1")
val ioDiagnostic2 = anyIoDiagnostic.copy(id = "id2")
val ioDiagnostic1: IoDiagnostic = anyIoDiagnostic.copy(id = "id1")
val ioDiagnostic2: IoDiagnostic = anyIoDiagnostic.copy(id = "id2")

val tenMinutes = Duration.standardMinutes(10)
val tenMinutes: Duration = Duration.standardMinutes(10)

val windowOptions = WindowOptions(
val windowOptions: WindowOptions = WindowOptions(
trigger = Repeatedly.forever(AfterWatermark.pastEndOfWindow()),
allowedLateness = Duration.ZERO,
accumulationMode = AccumulationMode.DISCARDING_FIRED_PANES,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.mkuthan.streamprocessing.toll.domain.vehicle

import org.apache.beam.sdk.transforms.windowing.AfterWatermark
import org.apache.beam.sdk.transforms.windowing.Repeatedly
import org.apache.beam.sdk.transforms.windowing.Window
import org.apache.beam.sdk.values.WindowingStrategy.AccumulationMode

import com.spotify.scio.bigquery.types.BigQueryType
import com.spotify.scio.values.SCollection
import com.spotify.scio.values.WindowOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.mkuthan.streamprocessing.toll.domain.common.LicensePlate

trait TollBoothEntryFixture {

final val anyTollBoothEntryPayload = TollBoothEntry.Payload(
final val anyTollBoothEntryPayload: TollBoothEntry.Payload = TollBoothEntry.Payload(
id = "1",
entry_time = "2014-09-10T12:01:00Z",
license_plate = "JNB 7001",
Expand All @@ -20,16 +20,17 @@ trait TollBoothEntryFixture {
tag = "String"
)

final val tollBoothEntryPayloadInvalid = anyTollBoothEntryPayload.copy(id = "")
final val tollBoothEntryPayloadInvalid: TollBoothEntry.Payload = anyTollBoothEntryPayload.copy(id = "")

final val tollBoothEntryDecodingError = DeadLetter[TollBoothEntry.Payload](
final val tollBoothEntryDecodingError: DeadLetter[TollBoothEntry.Payload] = DeadLetter[TollBoothEntry.Payload](
data = tollBoothEntryPayloadInvalid,
error = "requirement failed: Toll booth id is empty"
)

final val tollBoothEntryPayloadWithoutExit = anyTollBoothEntryPayload.copy(license_plate = "other license plate")
final val tollBoothEntryPayloadWithoutExit: TollBoothEntry.Payload =
anyTollBoothEntryPayload.copy(license_plate = "other license plate")

final val anyTollBoothEntryRecord = TollBoothEntry.Record(
final val anyTollBoothEntryRecord: TollBoothEntry.Record = TollBoothEntry.Record(
id = anyTollBoothEntryPayload.id,
entry_time = Instant.parse(anyTollBoothEntryPayload.entry_time),
license_plate = anyTollBoothEntryPayload.license_plate,
Expand All @@ -42,7 +43,7 @@ trait TollBoothEntryFixture {
tag = anyTollBoothEntryPayload.tag
)

final val anyTollBoothEntry = TollBoothEntry(
final val anyTollBoothEntry: TollBoothEntry = TollBoothEntry(
id = TollBoothId(anyTollBoothEntryPayload.id),
entryTime = Instant.parse(anyTollBoothEntryPayload.entry_time),
toll = BigDecimal(anyTollBoothEntryPayload.toll),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import org.mkuthan.streamprocessing.toll.domain.common.LicensePlate

trait TollBoothExitFixture {

final val anyTollBoothExitPayload = TollBoothExit.Payload(
final val anyTollBoothExitPayload: TollBoothExit.Payload = TollBoothExit.Payload(
id = "1",
exit_time = "2014-09-10T12:03:00Z",
license_plate = "JNB 7001"
)

final val tollBoothExitPayloadInvalid = anyTollBoothExitPayload.copy(id = "")
final val tollBoothExitPayloadInvalid: TollBoothExit.Payload = anyTollBoothExitPayload.copy(id = "")

final val tollBoothExitDecodingError = DeadLetter[TollBoothExit.Payload](
final val tollBoothExitDecodingError: DeadLetter[TollBoothExit.Payload] = DeadLetter[TollBoothExit.Payload](
data = tollBoothExitPayloadInvalid,
error = "requirement failed: Toll booth id is empty"
)

final val anyTollBoothExitRecord = TollBoothExit.Record(
final val anyTollBoothExitRecord: TollBoothExit.Record = TollBoothExit.Record(
id = anyTollBoothExitPayload.id,
exit_time = Instant.parse(anyTollBoothExitPayload.exit_time),
license_plate = anyTollBoothExitPayload.license_plate
)

final val anyTollBoothExit = TollBoothExit(
final val anyTollBoothExit: TollBoothExit = TollBoothExit(
id = TollBoothId(anyTollBoothExitPayload.id),
exitTime = Instant.parse(anyTollBoothExitPayload.exit_time),
licensePlate = LicensePlate(anyTollBoothExitPayload.license_plate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import org.joda.time.Instant

trait TollBoothStatsFixture {

final val anyTollBoothStats = TollBoothStats(
final val anyTollBoothStats: TollBoothStats = TollBoothStats(
id = TollBoothId("1"),
totalToll = BigDecimal(7),
count = 1,
firstEntryTime = Instant.parse("2014-09-10T12:01:00.000Z"),
lastEntryTime = Instant.parse("2014-09-10T12:01:00.000Z")
)

final val anyTollBoothStatsRecord = TollBoothStats.Record(
final val anyTollBoothStatsRecord: TollBoothStats.Record = TollBoothStats.Record(
created_at = Instant.EPOCH,
id = anyTollBoothStats.id.id,
total_toll = anyTollBoothStats.totalToll,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait VehicleRegistrationFixture {
private final val defaultLicensePlate = "JNB 7001"
private final val defaultExpired = "1"

final val anyVehicleRegistrationMessage = Message(
final val anyVehicleRegistrationMessage: Message[VehicleRegistration.Payload] = Message(
VehicleRegistration.Payload(
id = "1",
license_plate = defaultLicensePlate,
Expand All @@ -21,30 +21,31 @@ trait VehicleRegistrationFixture {
Map(VehicleRegistration.TimestampAttribute -> messageTimestamp)
)

final val vehicleRegistrationMessageInvalid = Message(
final val vehicleRegistrationMessageInvalid: Message[VehicleRegistration.Payload] = Message(
anyVehicleRegistrationMessage.payload.copy(license_plate = ""),
anyVehicleRegistrationMessage.attributes
)

final val vehicleRegistrationDecodingError = DeadLetter[VehicleRegistration.Payload](
data = vehicleRegistrationMessageInvalid.payload,
error = "requirement failed: Licence plate number is empty"
)
final val vehicleRegistrationDecodingError: DeadLetter[VehicleRegistration.Payload] =
DeadLetter[VehicleRegistration.Payload](
data = vehicleRegistrationMessageInvalid.payload,
error = "requirement failed: Licence plate number is empty"
)

final val anyVehicleRegistrationRecord = VehicleRegistration.Record(
final val anyVehicleRegistrationRecord: VehicleRegistration.Record = VehicleRegistration.Record(
id = "2",
license_plate = defaultLicensePlate,
expired = defaultExpired.toInt
)

final val anyVehicleRegistrationUpdate = VehicleRegistration(
final val anyVehicleRegistrationUpdate: VehicleRegistration = VehicleRegistration(
id = VehicleRegistrationId(anyVehicleRegistrationMessage.payload.id),
registrationTime = Instant.parse("2014-09-10T11:59:00Z"), // before toll booth entry
licensePlate = LicensePlate(defaultLicensePlate),
expired = defaultExpired == "1"
)

final val anyVehicleRegistrationHistory = VehicleRegistration(
final val anyVehicleRegistrationHistory: VehicleRegistration = VehicleRegistration(
id = VehicleRegistrationId(anyVehicleRegistrationRecord.id),
registrationTime = Instant.parse("2014-09-09T00:00:00.000Z"), // the previous day
licensePlate = LicensePlate(defaultLicensePlate),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import org.mkuthan.streamprocessing.toll.domain.common.LicensePlate

trait TotalVehicleTimeFixture {

final val anyTotalVehicleTime = TotalVehicleTime(
final val anyTotalVehicleTime: TotalVehicleTime = TotalVehicleTime(
tollBoothId = TollBoothId("1"),
licensePlate = LicensePlate("JNB 7001"),
entryTime = Instant.parse("2014-09-10T12:01:00.000Z"),
exitTime = Instant.parse("2014-09-10T12:03:00.000Z"),
duration = Duration.standardSeconds(120)
)

final val anyTotalVehicleTimeRecord = TotalVehicleTime.Record(
final val anyTotalVehicleTimeRecord: TotalVehicleTime.Record = TotalVehicleTime.Record(
created_at = Instant.EPOCH,
toll_booth_id = anyTotalVehicleTime.tollBoothId.id,
license_plate = anyTotalVehicleTime.licensePlate.number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ trait VehiclesWithExpiredRegistrationFixture {
private val defaultEntryTime = "2014-09-10T12:01:00.000Z"

final def anyVehicleWithExpiredRegistration(id: VehicleRegistrationId =
VehicleRegistrationId(defaultRegistrationId)) = VehiclesWithExpiredRegistration(
VehicleRegistrationId(defaultRegistrationId)): VehiclesWithExpiredRegistration = VehiclesWithExpiredRegistration(
vehicleRegistrationId = id,
licensePlate = LicensePlate(defaultLicencePlate),
tollBoothId = TollBoothId(defaultTollBoothId),
entryTime = Instant.parse(defaultEntryTime)
)

final def anyVehicleWithExpiredRegistrationRecord(createdAt: Instant, id: String = defaultRegistrationId) =
final def anyVehicleWithExpiredRegistrationRecord(
createdAt: Instant,
id: String = defaultRegistrationId
): VehiclesWithExpiredRegistration.Record =
VehiclesWithExpiredRegistration.Record(
created_at = createdAt,
vehicle_registration_id = id,
Expand All @@ -30,7 +33,10 @@ trait VehiclesWithExpiredRegistrationFixture {
entry_time = Instant.parse(defaultEntryTime)
)

final def anyVehicleWithExpiredRegistrationMessage(createdAt: Instant, id: String = defaultRegistrationId) =
final def anyVehicleWithExpiredRegistrationMessage(
createdAt: Instant,
id: String = defaultRegistrationId
): Message[VehiclesWithExpiredRegistration.Payload] =
Message(
VehiclesWithExpiredRegistration.Payload(
created_at = createdAt.toString,
Expand All @@ -42,13 +48,15 @@ trait VehiclesWithExpiredRegistrationFixture {
Map(VehiclesWithExpiredRegistration.TimestampAttribute -> createdAt.toString)
)

final val vehicleWithNotExpiredRegistrationDiagnostic = VehiclesWithExpiredRegistrationDiagnostic(
tollBothId = TollBoothId("1"),
reason = VehiclesWithExpiredRegistrationDiagnostic.NotExpired
)
final val vehicleWithNotExpiredRegistrationDiagnostic: VehiclesWithExpiredRegistrationDiagnostic =
VehiclesWithExpiredRegistrationDiagnostic(
tollBothId = TollBoothId("1"),
reason = VehiclesWithExpiredRegistrationDiagnostic.NotExpired
)

final val vehicleWithMissingRegistrationDiagnostic = VehiclesWithExpiredRegistrationDiagnostic(
tollBothId = TollBoothId("1"),
reason = VehiclesWithExpiredRegistrationDiagnostic.MissingRegistration
)
final val vehicleWithMissingRegistrationDiagnostic: VehiclesWithExpiredRegistrationDiagnostic =
VehiclesWithExpiredRegistrationDiagnostic(
tollBothId = TollBoothId("1"),
reason = VehiclesWithExpiredRegistrationDiagnostic.MissingRegistration
)
}

0 comments on commit 97c9f50

Please sign in to comment.