Skip to content

Commit

Permalink
support scala 3 (#195)
Browse files Browse the repository at this point in the history
* support scala 3

* add common Client trait

* fix getting method return type

* Update Macros.scala

* refactoring and fix wrong overloaded method handling

* refactor

* fix warnings

* println

* update to scala 3.3.0

* Update build.yml
  • Loading branch information
cornerman committed Jun 4, 2023
1 parent 2562f7f commit 77bc8cb
Show file tree
Hide file tree
Showing 20 changed files with 434 additions and 59 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
build:
strategy:
matrix:
scalaVersion: ["2.12.17", "2.13.10"]
scalaVersion: ["2.13.10", "3.3.0"]
runs-on: ubuntu-latest

steps:
Expand All @@ -33,8 +33,6 @@ jobs:
strategy:
matrix:
os: [ubuntu-20.04]
scala: [2.13.10]
java: [adopt@1.8]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![sloth Scala version support](https://index.scala-lang.org/cornerman/sloth/sloth/latest-by-scala-version.svg?platform=sjs1)](https://index.scala-lang.org/cornerman/sloth/sloth)
[![sloth Scala version support](https://index.scala-lang.org/cornerman/sloth/sloth/latest-by-scala-version.svg?platform=jvm)](https://index.scala-lang.org/cornerman/sloth/sloth)

Type safe RPC in scala
Type safe RPC in scala (scala 2 and scala 3)

Sloth is essentially a pair of macros (server and client) which takes an API definition in the form of a scala trait and then generates code for routing in the server as well as generating an API implementation in the client.

Expand Down Expand Up @@ -266,6 +266,8 @@ new Api {

## Experimental: Checksum for Apis

Currently scala-2 only.

In order to check the compatability of the client and server Api trait, you can calculate a checksum of your Api:

```scala
Expand Down
17 changes: 11 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// shadow sbt-scalajs' crossProject and CrossType from Scala.js 0.6.x
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
Global / onChangedBuildSource := IgnoreSourceChanges

inThisBuild(Seq(
organization := "com.github.cornerman",

scalaVersion := "2.12.17",
crossScalaVersions := Seq("2.12.17", "2.13.10"),
crossScalaVersions := Seq("2.13.10", "3.3.0"),
scalaVersion := crossScalaVersions.value.head,

licenses := Seq("MIT License" -> url("https://opensource.org/licenses/MIT")),

Expand Down Expand Up @@ -35,6 +34,10 @@ lazy val commonSettings = Seq(
)

lazy val jsSettings = Seq(
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) => Seq("-scalajs")
case _ => Seq.empty
}),
)

enablePlugins(ScalaJSPlugin)
Expand All @@ -56,12 +59,14 @@ lazy val sloth = crossProject(JSPlatform, JVMPlatform)
.settings(commonSettings)
.settings(
name := "sloth",
libraryDependencies ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) => Seq.empty
case _ => Seq(Deps.scalaReflect.value % scalaVersion.value % Provided)
}),
libraryDependencies ++=
Deps.scalaReflect.value % scalaVersion.value % Provided ::
Deps.cats.value ::
Deps.chameleon.value ::

Deps.kittens.value % Test ::
Deps.circe.core.value % Test ::
Deps.circe.generic.value % Test ::
Deps.circe.parser.value % Test ::
Expand Down
4 changes: 2 additions & 2 deletions project/Deps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
object Deps {
import Def.{setting => dep}

val scalaTest = dep("org.scalatest" %%% "scalatest" % "3.2.16")

val scalaReflect = dep("org.scala-lang" % "scala-reflect")
val cats = dep("org.typelevel" %%% "cats-core" % "2.9.0")
val chameleon = dep("com.github.cornerman" %%% "chameleon" % "0.3.5")

val scalaTest = dep("org.scalatest" %%% "scalatest" % "3.2.16")
val kittens = dep("org.typelevel" %%% "kittens" % "3.0.0")
val circe = new {
private val version = "0.14.1"
val core = dep("io.circe" %%% "circe-core" % version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Translator[C <: Context](val c: C) {
case Apply(Select(New(annotation), _), Literal(Constant(name)) :: Nil) if annotation.tpe =:= typeOf[sloth.PathName] => name.toString
}

private def eitherSeq[A, B](list: List[Either[A, B]]): Either[List[A], List[B]] = list.partition(_.isLeft) match {
case (Nil, rights) => Right(for (Right(i) <- rights) yield i)
case (lefts, _) => Left(for (Left(s) <- lefts) yield s)
}

def definedMethodsInType(tpe: Type): List[(MethodSymbol, Type)] = for {
member <- tpe.members.toList
if member.isAbstract
Expand Down
9 changes: 9 additions & 0 deletions sloth/src/main/scala-2/internal/PlatformSpecificClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sloth.internal

trait PlatformSpecificClientCo[PickleType, Result[_]] {
def wire[T]: T = macro TraitMacro.impl[T, PickleType, Result]
}

trait PlatformSpecificClientContra[PickleType, Result[_]] {
def wire[T]: T = macro TraitMacro.implContra[T, PickleType, Result]
}
11 changes: 11 additions & 0 deletions sloth/src/main/scala-2/internal/PlatformSpecificRouter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sloth.internal

import sloth.{RouterCo, RouterContra}

trait PlatformSpecificRouterCo[PickleType, Result[_]] {
def route[T](value: T): RouterCo[PickleType, Result] = macro RouterMacro.impl[T, PickleType, Result]
}

trait PlatformSpecificRouterContra[PickleType, Result[_]] {
def route[T](value: T): RouterContra[PickleType, Result] = macro RouterMacro.implContra[T, PickleType, Result]
}

0 comments on commit 77bc8cb

Please sign in to comment.