Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the core module and tests to scala 3 #883

Merged
merged 6 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/ci-scala3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This file was based on ci.yml file, but is unmanaged by the sbt plugin
# and was edited manually. Should be removed when sangria-derivation will
# be merged for Scala 3

name: Continuous Integration (Scala 3)

on:
pull_request:
branches: ['**']
push:
branches: ['**']
tags: [v*]

jobs:
build:
name: Build and Test on Scala 3
strategy:
matrix:
os: [ubuntu-latest]
scala: [3.1.3]
java: [temurin@11]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
uses: actions/setup-java@v2
with:
distribution: temurin
java-version: 11

- name: Cache sbt
uses: actions/cache@v2
with:
path: |
~/.sbt
~/.ivy2/cache
~/.coursier/cache/v1
~/.cache/coursier/v1
~/AppData/Local/Coursier/Cache/v1
~/Library/Caches/Coursier/v1
key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

- name: Check binary compatibility
run: sbt ++${{ matrix.scala }} "sangria-ast/mimaReportBinaryIssues;sangria-parser/mimaReportBinaryIssues;sangria-core/mimaReportBinaryIssues"

- name: Check formatting
run: sbt ++${{ matrix.scala }} scalafmtCheckAll

- name: Build and test project
run: sbt ++${{ matrix.scala }} "sangria-parser/test;sangria-core/test"
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ version = 3.5.8

runner.dialect = scala213

fileOverride {
"glob:**/src/main/scala-3/**" {
runner.dialect = scala3
}
}

maxColumn = 100

// Vertical alignment is pretty, but leads to bigger diffs
Expand Down
56 changes: 38 additions & 18 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import sbt.Keys._

import com.typesafe.tools.mima.core._

val isScala3 = Def.setting(
CrossVersion.partialVersion(scalaVersion.value).exists(_._1 == 3)
)

// sbt-github-actions needs configuration in `ThisBuild`
ThisBuild / crossScalaVersions := Seq("2.12.16", "2.13.8")
ThisBuild / scalaVersion := crossScalaVersions.value.last
ThisBuild / crossScalaVersions := Seq("2.12.16", "2.13.8", "3.1.3")
ThisBuild / scalaVersion := "2.13.8"
ThisBuild / githubWorkflowBuildPreamble ++= List(
WorkflowStep.Sbt(List("mimaReportBinaryIssues"), name = Some("Check binary compatibility")),
WorkflowStep.Sbt(List("scalafmtCheckAll"), name = Some("Check formatting"))
)

// For now we set up GH Actions manually for Scala 3
ThisBuild / githubWorkflowScalaVersions := crossScalaVersions.value.filterNot(_.startsWith("3."))

// Release
ThisBuild / githubWorkflowTargetTags ++= Seq("v*")
ThisBuild / githubWorkflowPublishTargetBranches :=
Expand All @@ -28,6 +35,10 @@ ThisBuild / githubWorkflowPublish := Seq(
)
)

def emptyForScala3(isScala3: Boolean, module: ModuleID): Set[ModuleID] =
if (isScala3) Set.empty
else Set(module)

lazy val root = project
.in(file("."))
.withId("sangria-root")
Expand All @@ -45,7 +56,9 @@ lazy val ast = project
.settings(
name := "sangria-ast",
description := "Scala GraphQL AST representation",
mimaPreviousArtifacts := Set("org.sangria-graphql" %% "sangria-ast" % "3.0.0"),
mimaPreviousArtifacts := emptyForScala3(
isScala3.value,
"org.sangria-graphql" %% "sangria-ast" % "3.0.0"),
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[IncompatibleResultTypeProblem]("sangria.ast.DirectiveDefinition.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.ast.DirectiveDefinition.apply"),
Expand All @@ -67,7 +80,9 @@ lazy val parser = project
.settings(
name := "sangria-parser",
description := "Scala GraphQL parser",
mimaPreviousArtifacts := Set("org.sangria-graphql" %% "sangria-parser" % "3.0.0"),
mimaPreviousArtifacts := emptyForScala3(
isScala3.value,
"org.sangria-graphql" %% "sangria-parser" % "3.0.0"),
libraryDependencies ++= Seq(
// AST Parser
"org.parboiled" %% "parboiled" % "2.4.0",
Expand All @@ -87,7 +102,9 @@ lazy val core = project
.settings(
name := "sangria-core",
description := "Scala GraphQL implementation",
mimaPreviousArtifacts := Set("org.sangria-graphql" %% "sangria-core" % "3.0.0"),
mimaPreviousArtifacts := emptyForScala3(
isScala3.value,
"org.sangria-graphql" %% "sangria-core" % "3.0.0"),
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.introspection.IntrospectionDirective.apply"),
Expand All @@ -109,18 +126,17 @@ lazy val core = project
"sangria.schema.Directive.copy$default$*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.schema.Directive.apply"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.schema.Directive.this"),
ProblemFilters.exclude[MissingTypesProblem]("sangria.schema.Directive$")
ProblemFilters.exclude[MissingTypesProblem]("sangria.schema.Directive$"),
ProblemFilters.exclude[MissingTypesProblem]("sangria.schema.MappedAbstractType")
),
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oF"),
libraryDependencies ++= Seq(
// AST Visitor
"org.sangria-graphql" %% "macro-visit" % "0.1.3",
"org.sangria-graphql" %% "macro-visit" % "0.2.0-RC1",
// Marshalling
"org.sangria-graphql" %% "sangria-marshalling-api" % "1.0.8",
// Streaming
"org.sangria-graphql" %% "sangria-streaming-api" % "1.0.3",
// Macros
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
// Testing
"co.fs2" %% "fs2-core" % "2.5.11" % Test,
"org.scalatest" %% "scalatest" % "3.2.13" % Test,
Expand All @@ -131,9 +147,11 @@ lazy val core = project
"org.sangria-graphql" %% "sangria-monix" % "2.0.1" % Test,
"eu.timepit" %% "refined" % "0.10.1" % Test,
// CATs
"net.jcazevedo" %% "moultingyaml" % "0.4.2" % Test,
("net.jcazevedo" %% "moultingyaml" % "0.4.2" % Test).cross(CrossVersion.for3Use2_13),
"io.github.classgraph" % "classgraph" % "4.8.149" % Test
),
) ++ (if (isScala3.value) Seq.empty
else Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value)), // Macros

apiURL := {
val ver = CrossVersion.binaryScalaVersion(scalaVersion.value)
Some(url(s"https://www.javadoc.io/doc/org.sangria-graphql/sangria-core_$ver/latest/"))
Expand All @@ -148,11 +166,12 @@ lazy val derivation = project
.settings(
name := "sangria-derivation",
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oF"),
mimaPreviousArtifacts := Set("org.sangria-graphql" %% "sangria-derivation" % "3.0.0"),
libraryDependencies ++= Seq(
// Macros
"org.scala-lang" % "scala-reflect" % scalaVersion.value
),
mimaPreviousArtifacts := emptyForScala3(
isScala3.value,
"org.sangria-graphql" %% "sangria-derivation" % "3.0.0"),
// Macros
libraryDependencies ++= (if (isScala3.value) Seq.empty
else Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value)),
apiURL := {
val ver = CrossVersion.binaryScalaVersion(scalaVersion.value)
Some(url(s"https://www.javadoc.io/doc/org.sangria-graphql/sangria-derivation_$ver/latest/"))
Expand Down Expand Up @@ -210,11 +229,12 @@ lazy val projectInfo = Seq(
)

lazy val scalacSettings = Seq(
scalacOptions ++= Seq("-deprecation", "-feature", "-Xlint:-missing-interpolator,_"),
scalacOptions ++= Seq("-deprecation", "-feature"),
scalacOptions ++= { if (!isScala3.value) Seq("-Xlint:-missing-interpolator,_") else Seq.empty },
scalacOptions ++= {
if (scalaVersion.value.startsWith("2.12")) Seq("-language:higherKinds") else List.empty[String]
},
scalacOptions += "-target:jvm-1.8",
scalacOptions += { if (isScala3.value) "-Xtarget:8" else "-target:jvm-1.8" },
autoAPIMappings := true,
Compile / doc / scalacOptions ++= // scaladoc options
Opts.doc.title("Sangria") ++ Seq(
Expand Down
36 changes: 36 additions & 0 deletions modules/core/src/main/scala-3/sangria/macros/ParseMacro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sangria.macros

import scala.quoted._
import sangria.parser.{QueryParser, SyntaxError}

object ParseMacro extends ToExprGivens {
def impl(using Quotes)(strCtxExpr: Expr[StringContext]): Expr[sangria.ast.Document] =
val parts = strCtxExpr.valueOrAbort.parts
if parts.length > 1 then
throw new Exception(
"String interpolation is not supported for `graphql`/`gql` macro at the moment.")
parts.headOption match
case Some(str) =>
Expr(QueryParser.parse(str).get)
case None => throw new Exception("Invalid `graphql` invocation syntax.")

def implInput(using Quotes)(strCtxExpr: Expr[StringContext]): Expr[sangria.ast.Value] =
val parts = strCtxExpr.valueOrAbort.parts
if parts.length > 1 then
throw new Exception(
"String interpolation is not supported for `graphqlInput`/`gqlInp` macro at the moment.")
parts.headOption match
case Some(str) =>
Expr(QueryParser.parseInput(str).get)
case None => throw new Exception("Invalid `graphql` invocation syntax.")

def implInputDoc(using Quotes)(strCtxExpr: Expr[StringContext]): Expr[sangria.ast.InputDocument] =
val parts = strCtxExpr.valueOrAbort.parts
if parts.length > 1 then
throw new Exception(
"String interpolation is not supported for `gqlInpDoc` macro at the moment.")
parts.headOption match
case Some(str) =>
Expr(QueryParser.parseInputDocument(str).get)
case None => throw new Exception("Invalid `graphql` invocation syntax.")
}
Loading