Skip to content

Commit

Permalink
Scala3 migration (#15)
Browse files Browse the repository at this point in the history
* Add basic config to proyect, fix DataExplorerSettings issue and basic SpraConfig

* TableSettings config

* Implement SpraConfig into DataExplorerConfig

* CORS

* Fix filter and add more examples

* Minor changes

* Remove space

* .sbt config

* Fix minor errors

* Optimize imports

* Fix web compilation

* Minor changes
  • Loading branch information
KapStorm authored Aug 12, 2023
1 parent 41a4542 commit 662abd6
Show file tree
Hide file tree
Showing 26 changed files with 65 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project.git = true
project.excludeFilters = [
]

runner.dialect=scala213
runner.dialect=scala3

maxColumn = 120
assumeStandardLibraryStripMargin = false
Expand Down
48 changes: 17 additions & 31 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import java.nio.file.StandardCopyOption.REPLACE_EXISTING
ThisBuild / versionScheme := Some("early-semver")
// For all Sonatype accounts created on or after February 2021
ThisBuild / sonatypeCredentialHost := "s01.oss.sonatype.org"
ThisBuild / scalaVersion := "3.3.0"

inThisBuild(
List(
organization := "net.wiringbits",
name := "scala-postgres-react-admin",
scalaVersion := "3.3.0",
homepage := Some(url("https://github.com/wiringbits/scala-postgres-react-admin")),
licenses := List("MIT" -> url("https://www.opensource.org/licenses/mit-license.html")),
developers := List(
Expand All @@ -24,8 +26,12 @@ inThisBuild(

resolvers += Resolver.sonatypeRepo("releases")

val playJson = "2.10.0-RC5"
val play = "2.9.0-M6"
val playJson = "2.10.0-RC9"
val sttp = "3.5.0"
val anorm = "2.7.0"
val scalaTestPlusPlay = "6.0.0-M6"
val scalaTestPlusMockito = "3.2.15.0"

val consoleDisabledOptions = Seq("-Xfatal-warnings", "-Ywarn-unused", "-Ywarn-unused-import")

Expand All @@ -35,8 +41,7 @@ lazy val build = TaskKey[File]("build")
lazy val baseServerSettings: Project => Project = {
_.settings(
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-Werror",
"-feature"
),
Compile / doc / scalacOptions ++= Seq("-no-link-warnings"),
Expand All @@ -55,18 +60,15 @@ lazy val playSettings: Project => Project = {
Compile / doc / scalacOptions ++= Seq(
"-no-link-warnings"
),
// remove play noisy warnings
play.sbt.routes.RoutesKeys.routesImport := Seq.empty,
libraryDependencies ++= Seq(
evolutions,
"com.typesafe.play" %% "play-jdbc" % "2.8.13",
"com.typesafe.play" %% "play-jdbc" % "2.9.0-M6",
"com.google.inject" % "guice" % "5.1.0"
),
// test
libraryDependencies ++= Seq(
"org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test,
"org.mockito" %% "mockito-scala" % "1.17.5" % Test,
"org.mockito" %% "mockito-scala-scalatest" % "1.17.5" % Test
"org.scalatestplus.play" %% "scalatestplus-play" % "6.0.0-M6" % Test,
"org.scalatestplus" %% "mockito-4-6" % scalaTestPlusMockito % Test
)
)
}
Expand Down Expand Up @@ -105,11 +107,6 @@ lazy val baseLibSettings: Project => Project = _.settings(
// The common stuff for the server/client modules
lazy val spraCommon = (crossProject(JSPlatform, JVMPlatform) in file("spra-common"))
.configure(baseLibSettings)
.settings(
scalaVersion := "2.13.8",
crossScalaVersions := Seq("2.13.8", "3.1.2"),
name := "spra-common"
)
.jsConfigure(_.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin))
.jvmSettings(
libraryDependencies ++= Seq(
Expand All @@ -131,11 +128,6 @@ lazy val spraCommon = (crossProject(JSPlatform, JVMPlatform) in file("spra-commo
lazy val spraApi = (crossProject(JSPlatform, JVMPlatform) in file("spra-api"))
.configure(baseLibSettings)
.dependsOn(spraCommon)
.settings(
scalaVersion := "2.13.8",
crossScalaVersions := Seq("2.13.8", "3.1.2"),
name := "spra-api"
)
.jsConfigure(_.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin))
.jvmSettings(
libraryDependencies ++= Seq(
Expand All @@ -156,19 +148,16 @@ lazy val spraApi = (crossProject(JSPlatform, JVMPlatform) in file("spra-api"))
/** Includes the specific stuff to run the SPRA server side (play-specific)
*/
lazy val spraPlayServer = (project in file("spra-play-server"))
.enablePlugins(PlayScala)
.dependsOn(spraApi.jvm, spraCommon.jvm)
.configure(baseServerSettings, playSettings)
.settings(
scalaVersion := "2.13.8",
crossScalaVersions := Seq("2.13.8"),
name := "spra-play-server",
fork := true,
Test / fork := true, // allows for graceful shutdown of containers once the tests have finished running
libraryDependencies ++= Seq(
guice,
"org.playframework.anorm" %% "anorm" % "2.6.10",
"com.typesafe.play" %% "play" % "2.8.13",
"com.typesafe.play" %% "play-json" % "2.9.2",
"org.playframework.anorm" %% "anorm" % anorm,
"com.typesafe.play" %% "play-json" % playJson,
"org.postgresql" % "postgresql" % "42.3.6",
"com.github.jwt-scala" %% "jwt-core" % "9.0.5",
"de.svenkubiak" % "jBCrypt" % "0.4.3",
Expand Down Expand Up @@ -254,22 +243,19 @@ lazy val browserProject: Project => Project =
)

lazy val spraWeb = (project in file("spra-web"))
.dependsOn(spraApi.js, spraPlayServer)
.dependsOn(spraApi.js)
.configure(bundlerSettings, baseLibSettings, browserProject, spraWebBuildInfoSettings)
.configure(_.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin))
.settings(
scalaVersion := "2.13.8",
crossScalaVersions := Seq("2.13.8", "3.1.2"),
name := "spra-web",
Test / fork := false, // sjs needs this to run tests
scalaJSUseMainModuleInitializer := true,
scalaJSLinkerConfig := scalaJSLinkerConfig.value.withSourceMap(false),
webpackDevServerPort := 8081,
webpackBundlingMode := BundlingMode.LibraryOnly(),
libraryDependencies ++= Seq(
"org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0",
"me.shadaj" %%% "slinky-core" % "0.7.3",
"me.shadaj" %%% "slinky-web" % "0.7.3"
"me.shadaj" %%% "slinky-core" % "0.7.4",
"me.shadaj" %%% "slinky-web" % "0.7.4"
),
Compile / npmDependencies ++= Seq(
"react" -> "17.0.0",
Expand Down
9 changes: 6 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.1.0")
// while there are some eviction errors, plugins seem to be compatible so far
evictionErrorLevel := sbt.util.Level.Warn

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.13")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.1")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.8.0")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.9.0-M6")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.1")

addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.20.0")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package net.wiringbits.spra.api

import net.wiringbits.spra.api.models.{
AdminCreateTable,
AdminDeleteTable,
AdminGetTables,
AdminUpdateTable,
PlayErrorResponse
}
import net.wiringbits.spra.api.models._
import play.api.libs.json._
import sttp.client3._
import sttp.model._
import net.wiringbits.spra.api.models.*
import play.api.libs.json.*
import sttp.client3.*
import sttp.model.*

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.wiringbits.spra.api

import play.api.libs.json._
import play.api.libs.json.*

import java.time.Instant

Expand Down
2 changes: 1 addition & 1 deletion spra-play-server/src/main/resources/routes
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# https://www.playframework.com/documentation/latest/ScalaRouting
# ~~~~

-> / net.wiringbits.spra.admin.AppRouter
-> / net.wiringbits.spra.admin.AppRouter
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.wiringbits.spra.admin.utils.StringToDataTypesExt
import net.wiringbits.spra.admin.utils.models.{FilterParameter, PaginationParameter, QueryParameters, SortParameter}
import play.api.routing.Router.Routes
import play.api.routing.SimpleRouter
import play.api.routing.sird._
import play.api.routing.sird.*

import javax.inject.Inject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ case class TableSettings(

object TableSettings {
implicit val configLoader: ConfigLoader[TableSettings] = (config: Config, path: String) => {
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
val newConfig = config.getConfig(path)

def get[A](path: String): A = {
Expand Down Expand Up @@ -97,16 +97,11 @@ object TableSettings {
}
}

sealed trait PrimaryKeyDataType extends Product with Serializable
object PrimaryKeyDataType {
final case object UUID extends PrimaryKeyDataType
final case object Serial extends PrimaryKeyDataType
final case object BigSerial extends PrimaryKeyDataType
enum PrimaryKeyDataType {
case UUID, Serial, BigSerial
}

sealed trait CustomDataType extends Product with Serializable
object CustomDataType {
final case object BinaryImage extends CustomDataType
enum CustomDataType {
// TODO: add support to binary files
final case object Binary extends CustomDataType
case BinaryImage, Binary
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.wiringbits.spra.admin.controllers
import net.wiringbits.spra.admin.config.DataExplorerConfig
import net.wiringbits.spra.admin.services.AdminService
import net.wiringbits.spra.admin.utils.models.QueryParameters
import net.wiringbits.spra.api.models._
import net.wiringbits.spra.api.models.*
import org.slf4j.LoggerFactory
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
Expand Down Expand Up @@ -32,7 +32,8 @@ class AdminController @Inject() (
_ <- adminUser(request)
_ = logger.info(s"Get metadata for $tableName, parameters: $queryParams")
(response, contentRange) <- adminService.tableMetadata(tableName, queryParams)
} yield Ok(Json.toJson(response))
// Json.toJson doesn't support a List[Map[_, _]] so we need to map to convert it to List[JsonValue]
} yield Ok(Json.toJson(response.map(Json.toJson(_))))
.withHeaders(("Access-Control-Expose-Headers", "Content-Range"), ("Content-Range", contentRange))
}

Expand All @@ -49,7 +50,7 @@ class AdminController @Inject() (
_ <- adminUser(request)
_ = logger.info(s"Get data from $tableName where primaryKeys = ${primaryKeyValues.mkString(",")}")
response <- adminService.find(tableName, primaryKeyValues)
} yield Ok(Json.toJson(response))
} yield Ok(Json.toJson(response.map(Json.toJson(_))))
}

def create(tableName: String) = handleJsonBody[AdminCreateTable.Request] { request =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package net.wiringbits.spra.admin

import net.wiringbits.spra.api.models.ErrorResponse
import net.wiringbits.spra.api.models.{ErrorResponse, errorResponseFormat}
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsValue, Json, Reads}
import play.api.mvc.*
import play.api.mvc.Results.InternalServerError
import play.api.mvc._

import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package net.wiringbits.spra.admin.repositories.daos

import anorm.{SqlParser, SqlStringInterpolation}
import net.wiringbits.spra.admin.config.{CustomDataType, PrimaryKeyDataType, TableSettings}
import net.wiringbits.spra.admin.repositories.models._
import net.wiringbits.spra.admin.repositories.models.*
import net.wiringbits.spra.admin.utils.models.{FilterParameter, QueryParameters}
import net.wiringbits.spra.admin.utils.{QueryBuilder, StringRegex}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.wiringbits.spra.admin.repositories

import anorm._
import anorm.*
import net.wiringbits.spra.admin.repositories.models.{DatabaseTable, ForeignKey, TableColumn}

package object daos {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.wiringbits.spra.admin.repositories.DatabaseTablesRepository
import net.wiringbits.spra.admin.repositories.models.{ForeignKey, TableData}
import net.wiringbits.spra.admin.utils.models.QueryParameters
import net.wiringbits.spra.admin.utils.{MapStringHideExt, contentRangeHeader}
import net.wiringbits.spra.api.models._
import net.wiringbits.spra.api.models.*

import java.awt.image.BufferedImage
import java.io.{ByteArrayInputStream, File}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class AdminControllerSpec extends PlayPostgresSpec {
val nameLength = 7
Range.apply(0, createdUsers).foreach { i =>
val letter = Character.valueOf(('A' + i).toChar)
val name = StringUtils.repeat(letter, nameLength);
val name = StringUtils.repeat(letter, nameLength)
val data = Map("name" -> name, "email" -> s"test@wiringbits$i.net", "password" -> "wiringbits")
val request = AdminCreateTable.Request(data)
client.createItem(usersSettings.tableName, request).futureValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc.Result
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.test.Helpers.*
import play.api.{Application, Mode}

import java.net.URLEncoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.scalatestplus.play.guice.GuiceOneServerPerTest
import org.testcontainers.utility.DockerImageName
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.{Application, Configuration, Environment, Mode}
import sttp.client3.SttpBackend

import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal
Expand Down Expand Up @@ -47,7 +48,7 @@ trait PlayPostgresSpec extends PlayAPISpec with TestContainerForEach with GuiceO
def withApiClient[A](runTest: AdminDataExplorerApiClient => A): A = {
import sttp.client3.asynchttpclient.future.AsyncHttpClientFutureBackend

implicit val sttpBackend = AsyncHttpClientFutureBackend()
implicit val sttpBackend: SttpBackend[Future, Any] = AsyncHttpClientFutureBackend()

val config = AdminDataExplorerApiClient.Config(s"http://localhost:$port")
val client = new AdminDataExplorerApiClient.DefaultImpl(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package net.wiringbits.spra.admin

import net.wiringbits.spra.admin.repositories.models.TableColumn
import net.wiringbits.spra.admin.utils.QueryBuilder
import org.scalatest.matchers.must.Matchers.{be, convertToAnyMustWrapper}
import org.scalatest.matchers.must.Matchers.{be, must}
import org.scalatest.wordspec.AnyWordSpec

class QueryBuilderSpec extends AnyWordSpec {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.wiringbits.spra.admin

import net.wiringbits.spra.admin.utils.StringRegex
import org.scalatest.matchers.must.Matchers.{be, convertToAnyMustWrapper}
import org.scalatest.matchers.must.Matchers.{be, must}
import org.scalatest.wordspec.AnyWordSpec

class StringRegexSpec extends AnyWordSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package net.wiringbits.spra.admin

import net.wiringbits.spra.admin.utils.StringToDataTypesExt
import org.scalatest.OptionValues.convertOptionToValuable
import org.scalatest.matchers.must.Matchers.{be, convertToAnyMustWrapper}
import org.scalatest.matchers.must.Matchers.{be, must}
import org.scalatest.wordspec.AnyWordSpec

class UtilsSpec extends AnyWordSpec {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.wiringbits.spra.admin.models

import net.wiringbits.spra.admin.utils.models.FilterParameter
import org.scalatest.matchers.must.Matchers.{be, convertToAnyMustWrapper}
import org.scalatest.matchers.must.Matchers.{be, must}
import org.scalatest.wordspec.AnyWordSpec

class FilterParameterSpec extends AnyWordSpec {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.wiringbits.spra.admin.models

import net.wiringbits.spra.admin.utils.models.PaginationParameter
import org.scalatest.matchers.must.Matchers.{be, convertToAnyMustWrapper}
import org.scalatest.matchers.must.Matchers.{be, must}
import org.scalatest.wordspec.AnyWordSpec

import scala.util.Try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.wiringbits.spra.admin.models

import net.wiringbits.spra.admin.utils.models.SortParameter
import org.scalatest.matchers.must.Matchers.{be, convertToAnyMustWrapper}
import org.scalatest.matchers.must.Matchers.{be, must}
import org.scalatest.wordspec.AnyWordSpec

class SortParameterSpec extends AnyWordSpec {
Expand Down
6 changes: 3 additions & 3 deletions spra-web/src/main/js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script type="text/javascript" src="spra-web-fastopt-library.js"></script>
<script type="text/javascript" src="spra-web-fastopt-loader.js"></script>
<script type="text/javascript" src="spra-web-fastopt.js"></script>
<script type="text/javascript" src="spraweb-fastopt-library.js"></script>
<script type="text/javascript" src="spraweb-fastopt-loader.js"></script>
<script type="text/javascript" src="spraweb-fastopt.js"></script>
</body>

</html>
Loading

0 comments on commit 662abd6

Please sign in to comment.