Skip to content

Commit

Permalink
Merge pull request #463 from Javakky-pxv/javakky/scalafix-install
Browse files Browse the repository at this point in the history
Refactor: Introduce Scalafix and Lint / Refactor the code
  • Loading branch information
Javakky-pxv authored Jul 9, 2022
2 parents 7aa3eb6 + 4cb7b19 commit cc2aff0
Show file tree
Hide file tree
Showing 32 changed files with 141 additions and 83 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Check Format by scalafmt
run:
sbt scalafmtSbtCheck scalafmtCheckAll
- name: Check Lint by scalafix
run:
sbt scalafixEnable "scalafixAll --check"
- name: Sbt Plugin tests
run:
sbt clean sbtPlaySwagger/scripted
Expand All @@ -29,4 +32,4 @@ jobs:
sbt ';project playSwagger;clean;+test'
- name: Example compile
run:
cd example && sbt clean scalafmtSbtCheck scalafmtCheckAll compile
cd example && sbt clean scalafmtSbtCheck scalafmtCheckAll scalafixEnable "scalafixAll --check" compile
27 changes: 27 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rules = [
RemoveUnused
NoAutoTupling
NoValInForComprehension
ProcedureSyntax
ExplicitResultTypes
OrganizeImports
]

RemoveUnused {
imports = true
privates = false
locals = true
patternvars = true
params = false
}

OrganizeImports {
coalesceToWildcardImportThreshold = 6
groupedImports = Merge
groups = [
"re:javax?\\."
"scala.",
"*",
"com.sun."
]
}
16 changes: 14 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
organization in ThisBuild := "com.iheart"

scalafixDependencies in ThisBuild ++= Seq(
"com.github.liancheng" %% "organize-imports" % "0.6.0"
)

lazy val noPublishSettings = Seq(
skip in publish := true,
publish := (),
Expand Down Expand Up @@ -32,7 +36,11 @@ lazy val playSwagger = project.in(file("core"))
Dependencies.test ++
Dependencies.yaml,
scalaVersion := scalaV,
crossScalaVersions := Seq(scalaVersion.value, "2.13.6")
crossScalaVersions := Seq(scalaVersion.value, "2.13.6"),
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 13)) => Seq("-Wunused")
case _ => Seq("-Xlint:unused")
})
)

lazy val sbtPlaySwagger = project.in(file("sbtPlugin"))
Expand All @@ -55,5 +63,9 @@ lazy val sbtPlaySwagger = project.in(file("sbtPlugin"))
scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
},
scriptedBufferLog := false
scriptedBufferLog := false,
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 13)) => Seq("-Wunused")
case _ => Seq("-Xlint:unused")
})
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.iheart.playSwagger

import scala.collection.JavaConverters
import scala.reflect.runtime.universe._

import com.fasterxml.jackson.databind.{BeanDescription, ObjectMapper}
import com.iheart.playSwagger.Domain.{CustomMappings, Definition, GenSwaggerParameter, SwaggerParameter}
import com.iheart.playSwagger.SwaggerParameterMapper.mapParam
import play.routes.compiler.Parameter

import scala.collection.JavaConverters
import scala.reflect.runtime.universe._

final case class DefinitionGenerator(
modelQualifier: DomainModelQualifier = PrefixDomainModelQualifier(),
mappings: CustomMappings = Nil,
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/scala/com/iheart/playSwagger/Domain.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.iheart.playSwagger

import play.api.libs.json.{JsObject, JsPath, JsValue, Reads}
import play.twirl.api.TemplateMagic.Default

object Domain {
type Path = String
Expand Down Expand Up @@ -34,7 +33,7 @@ object Domain {
items: Option[SwaggerParameter] = None,
enum: Option[Seq[String]] = None
) extends SwaggerParameter {
def update(_required: Boolean, _nullable: Boolean, _default: Option[JsValue]) =
def update(_required: Boolean, _nullable: Boolean, _default: Option[JsValue]): GenSwaggerParameter =
copy(required = _required, nullable = Some(_nullable), default = _default)
}

Expand All @@ -46,7 +45,7 @@ object Domain {
nullable: Option[Boolean] = None,
default: Option[JsValue] = None
) extends SwaggerParameter {
def update(_required: Boolean, _nullable: Boolean, _default: Option[JsValue]) =
def update(_required: Boolean, _nullable: Boolean, _default: Option[JsValue]): CustomSwaggerParameter =
copy(required = _required, nullable = Some(_nullable), default = _default)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.iheart.playSwagger

import com.iheart.playSwagger.OutputTransformer.SimpleOutputTransformer
import play.api.libs.json.{JsArray, JsString, JsValue, JsObject}

import scala.util.matching.Regex
import scala.util.{Success, Failure, Try}
import scala.util.{Failure, Success, Try}

import com.iheart.playSwagger.OutputTransformer.SimpleOutputTransformer
import play.api.libs.json.{JsArray, JsObject, JsString, JsValue}

/** Specialization of a Kleisli function (A => M[B]) */
trait OutputTransformer extends (JsObject Try[JsObject]) {
Expand Down Expand Up @@ -57,7 +57,7 @@ object OutputTransformer {

class PlaceholderVariablesTransformer(map: String Option[String], pattern: Regex = "^\\$\\{(.*)\\}$".r)
extends OutputTransformer {
def apply(value: JsObject) = OutputTransformer.traverseTransformer(value) {
def apply(value: JsObject): Try[JsObject] = OutputTransformer.traverseTransformer(value) {
case JsString(pattern(key)) map(key) match {
case Some(result) Success(JsString(result))
case None Failure(new IllegalStateException(s"Unable to find variable $key"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.iheart.playSwagger

import scala.collection.immutable.SortedMap
import scala.reflect.runtime.universe._
import ParametricType._
import scala.util.matching.Regex

import scala.collection.immutable.SortedMap
import ParametricType._

case class ParametricType private (
tpe: Type,
Expand All @@ -24,7 +25,7 @@ case class ParametricType private (
}

object ParametricType {
final val ParametricTypeClassName = "^(.*?)\\[(.*)\\]$".r
final val ParametricTypeClassName: Regex = "^(.*?)\\[(.*)\\]$".r

def apply(reifiedTypeName: String)(implicit cl: ClassLoader): ParametricType = {
val mirror = runtimeMirror(cl)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.iheart.playSwagger

import java.io.{IOException, InputStream}
import scala.util.{Failure, Try}

import scala.io.Source
import scala.util.{Failure, Try}

object ResourceReader {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.iheart.playSwagger

import scala.reflect.runtime.universe
import scala.util.Try
import scala.util.matching.Regex

import com.iheart.playSwagger.Domain.{CustomMappings, CustomSwaggerParameter, GenSwaggerParameter, SwaggerParameter}
import play.api.libs.json._
import play.routes.compiler.Parameter

import scala.reflect.runtime.universe
import scala.util.Try

object SwaggerParameterMapper {

type MappingFunction = PartialFunction[String, SwaggerParameter]
Expand Down Expand Up @@ -164,7 +165,7 @@ object SwaggerParameterMapper {
}

implicit class CaseInsensitiveRegex(sc: StringContext) {
def ci = ("(?i)" + sc.parts.mkString).r
def ci: Regex = ("(?i)" + sc.parts.mkString).r
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.iheart.playSwagger

import java.io.File

import scala.collection.immutable.ListMap
import scala.collection.mutable
import scala.util.{Failure, Success, Try}

import com.fasterxml.jackson.databind.ObjectMapper
import com.iheart.playSwagger.Domain._
import com.iheart.playSwagger.OutputTransformer.SimpleOutputTransformer
Expand All @@ -11,10 +15,6 @@ import org.yaml.snakeyaml.Yaml
import play.api.libs.json._
import play.routes.compiler._

import scala.collection.mutable
import scala.collection.immutable.ListMap
import scala.util.{Failure, Success, Try}

object SwaggerSpecGenerator {
private val marker = "##"
val customMappingsFileName = "swagger-custom-mappings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.iheart.playSwagger

import java.nio.file.{Files, Paths, StandardOpenOption}

import play.api.libs.json.{JsValue, Json}

import scala.util.{Failure, Success, Try}

import play.api.libs.json.{JsValue, Json}

object SwaggerSpecRunner extends App {
implicit def cl: ClassLoader = getClass.getClassLoader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ object MyObject {
}

object ExcludingDomainQualifier extends DomainModelQualifier {
val parent = PrefixDomainModelQualifier("com.iheart.playSwagger")
val exclusions = Seq("com.iheart.playSwagger.DictType")
val parent: PrefixDomainModelQualifier = PrefixDomainModelQualifier("com.iheart.playSwagger")
val exclusions: Seq[String] = Seq("com.iheart.playSwagger.DictType")
override def isModel(className: String): Boolean = parent.isModel(className) && !(exclusions contains className)
}

class DefinitionGeneratorSpec extends Specification {
implicit val cl = getClass.getClassLoader
implicit val cl: ClassLoader = getClass.getClassLoader

"definition" >> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.iheart.playSwagger

import scala.util.{Failure, Success}

import com.iheart.playSwagger.OutputTransformer.SimpleOutputTransformer
import org.specs2.mutable.Specification
import play.api.libs.json._

import scala.util.{Failure, Success}

class OutputTransformerSpec extends Specification {
"OutputTransformer.traverseTransformer" >> {

Expand Down Expand Up @@ -124,7 +124,7 @@ class EnvironmentVariablesSpec extends Specification {
}

class EnvironmentVariablesIntegrationSpec extends Specification {
implicit val cl = getClass.getClassLoader
implicit val cl: ClassLoader = getClass.getClassLoader

"integration" >> {
"generate api with placeholders in place" >> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.iheart.playSwagger

import scala.util.Success

import org.specs2.mutable.Specification
import play.api.libs.json.Json

import scala.util.Success

class ParametricTypeNamesTransformerSpec extends Specification {
private val transformer = new ParametricTypeNamesTransformer

Expand Down
4 changes: 2 additions & 2 deletions core/src/test/scala/com/iheart/playSwagger/RefinedTypes.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.iheart.playSwagger

import scala.collection.immutable

import eu.timepit.refined._
import eu.timepit.refined.api._
import eu.timepit.refined.boolean.And
import eu.timepit.refined.collection.{MinSize, NonEmpty}
import eu.timepit.refined.numeric._
import eu.timepit.refined.string._

import scala.collection.immutable

object RefinedTypes {
type SpotifyAccount = String Refined And[MinSize[W.`6`.T], MatchesRegex[W.`"""@?(\\w){1,15}"""`.T]]
type Age = Int Refined Positive
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.iheart.playSwagger

import org.specs2.mutable.Specification
import play.api.libs.json.{Json, JsString}
import com.iheart.playSwagger.Domain._
import org.specs2.mutable.Specification
import play.api.libs.json.{JsString, Json}
import play.routes.compiler.Parameter

class SwaggerParameterMapperSpec extends Specification {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.iheart.playSwagger

import com.iheart.playSwagger.Domain.{CustomMappings, CustomTypeMapping}
import com.iheart.playSwagger.RefinedTypes.{Age, Albums, SpotifyAccount}

import java.time.LocalDate

import com.iheart.playSwagger.Domain.CustomMappings
import com.iheart.playSwagger.RefinedTypes.{Age, Albums, SpotifyAccount}
import org.specs2.mutable.Specification
import play.api.libs.json._

Expand Down Expand Up @@ -40,8 +40,8 @@ case class Cat(catName: String)
case class TypeParametricWrapper[T, O, S](simplePayload: T, maybeOtherPayload: Option[O], seq: Seq[S])

class SwaggerSpecGeneratorSpec extends Specification {
implicit val cl = getClass.getClassLoader
val gen = SwaggerSpecGenerator()
implicit val cl: ClassLoader = getClass.getClassLoader
val gen: SwaggerSpecGenerator = SwaggerSpecGenerator()

"full path" >> {
"combine routePath with prefix" >> {
Expand Down Expand Up @@ -98,7 +98,7 @@ class SwaggerSpecGeneratorSpec extends Specification {
}

class SwaggerSpecGeneratorIntegrationSpec extends Specification {
implicit val cl = getClass.getClassLoader
implicit val cl: ClassLoader = getClass.getClassLoader

"integration" >> {

Expand Down
1 change: 1 addition & 0 deletions example/.scalafix.conf
7 changes: 3 additions & 4 deletions example/app/Filters.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.api.mvc._

import filters.ExampleFilter
import play.api._
import play.api.http.HttpFilters

/**
* This class configures filters that run on every request. This
Expand All @@ -24,7 +23,7 @@ class Filters @Inject() (
exampleFilter: ExampleFilter
) extends HttpFilters {

override val filters = {
override val filters: Seq[ExampleFilter] = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
Expand Down
4 changes: 2 additions & 2 deletions example/app/Module.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.google.inject.AbstractModule
import java.time.Clock

import com.google.inject.AbstractModule
import services.{ApplicationTimer, AtomicCounter, Counter}

/**
Expand All @@ -15,7 +15,7 @@ import services.{ApplicationTimer, AtomicCounter, Counter}
*/
class Module extends AbstractModule {

override def configure() = {
override def configure(): Unit = {
// Use the system clock as the default implementation of Clock
bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
// Ask Guice to create an instance of ApplicationTimer when the
Expand Down
Loading

0 comments on commit cc2aff0

Please sign in to comment.