Skip to content

Commit

Permalink
Use scala 3 syntax for imports and type constuctors
Browse files Browse the repository at this point in the history
This is supported scala 2 synatax as well now with -Xsource:3 which
sbt-typelevel automatically adds.

Now we keep the Scala 2 and Scala 3 versions consistent and
do not confuse intellij which actually respects the -Xsource:3 flag.

Extracted from #255
  • Loading branch information
hamnis committed Feb 23, 2024
1 parent 7e7687b commit 9e32509
Show file tree
Hide file tree
Showing 53 changed files with 167 additions and 156 deletions.
8 changes: 7 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version = 3.7.17
runner.dialect = scala213
runner.dialect = Scala213Source3
runner.dialectOverride.withAllowStarWildcardImport = true
runner.dialectOverride.withAllowPostfixStarVarargSplices = true
runner.dialectOverride.withAllowAsForImportRename = true
runner.dialectOverride.withAllowQuestionMarkAsTypeWildcard = true
rewrite.scala3.convertToNewSyntax = true
style = default

maxColumn = 120
Expand All @@ -20,6 +25,7 @@ rewrite.rules = [
fileOverride {
"glob:**/scala-3/**/*.scala" {
runner.dialect = scala3
runner.dialectOverride.allowSignificantIndentation = false
}
}
project.excludePaths = ["glob:**/codegen-tests/**/*.scala"]
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ val commonSettings = Seq(
scalacOptions -= "-source:3.0-migration",
scalacOptions ++= {
if (scalaVersion.value.startsWith("3")) {
Seq("-source:3.2-migration")
Seq("-source:3.2-migration", "-old-syntax", "-no-indent")
} else {
Seq("-feature", "-language:implicitConversions", "-language:experimental.macros")
Nil
}
},
sonatypeProfileName := "no.nrk"
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/main/scala/no/nrk/bigquery/codegen/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.apache.commons.text.translate.LookupTranslator
import java.nio.charset.StandardCharsets
import java.nio.file.*
import scala.collection.immutable
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*

object Generators {
private val escaper = new LookupTranslator(
Expand Down Expand Up @@ -125,9 +125,9 @@ object Generators {
object CodeGen {
def generate(tables: immutable.Seq[BQTableDef[Any]], basePackage: List[String]) =
tables.collect {
case table: BQTableDef.Table[_] =>
case table: BQTableDef.Table[?] =>
Source(SourceLocation(table.tableId, basePackage, "Table"), Generators.genTableDef(table))
case view: BQTableDef.View[_] =>
case view: BQTableDef.View[?] =>
Source(SourceLocation(view.tableId, basePackage, "View"), Generators.genViewDef(view))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package no.nrk.bigquery
package codegen

import no.nrk.bigquery.syntax._
import no.nrk.bigquery.syntax.*

class CodeGenTest extends munit.FunSuite with testing.GeneratedTest {
val table = BQTableDef.Table(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object BQLiteralOps {
Ident.fromString(s).map(_ => c.Expr(q"Ident.unsafeFromString($s)"))
}

def make(c: Context)(args: c.Expr[Any]*): c.Expr[Ident] = apply(c)(args: _*)
def make(c: Context)(args: c.Expr[Any]*): c.Expr[Ident] = apply(c)(args*)
}

object LabelKeyLiteral extends Literally[Labels.Key] {
Expand All @@ -38,7 +38,7 @@ object BQLiteralOps {
Labels.Key.apply(s).toEither.left.map(_.toList.mkString("\n")).map(_ => c.Expr(q"Labels.Key.make($s)"))
}

def make(c: Context)(args: c.Expr[Any]*): c.Expr[Labels.Key] = apply(c)(args: _*)
def make(c: Context)(args: c.Expr[Any]*): c.Expr[Labels.Key] = apply(c)(args*)
}

object LabelValueLiteral extends Literally[Labels.Value] {
Expand All @@ -48,7 +48,7 @@ object BQLiteralOps {
Labels.Value.apply(s).toEither.left.map(_.toList.mkString("\n")).map(_ => c.Expr(q"Labels.Value.make($s)"))
}

def make(c: Context)(args: c.Expr[Any]*): c.Expr[Labels.Value] = apply(c)(args: _*)
def make(c: Context)(args: c.Expr[Any]*): c.Expr[Labels.Value] = apply(c)(args*)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ import no.nrk.bigquery.{Ident, Labels}
import org.typelevel.literally.Literally

trait BQLiteralSyntax {
extension (inline ctx: StringContext)
extension (inline ctx: StringContext) {
inline def ident(args: Any*): Ident =
${ IdentLiteral('ctx, 'args) }
inline def labelkey(args: Any*): Labels.Key =
${ LabelKeyLiteral('ctx, 'args) }
inline def labelvalue(args: Any*): Labels.Value =
${ LabelValueLiteral('ctx, 'args) }
}
}

object IdentLiteral extends Literally[Ident]:
object IdentLiteral extends Literally[Ident] {
def validate(s: String)(using Quotes) =
Ident.fromString(s).map(_ => '{ Ident.unsafeFromString(${ Expr(s) }) })
}

object LabelKeyLiteral extends Literally[Labels.Key]:
object LabelKeyLiteral extends Literally[Labels.Key] {
def validate(s: String)(using Quotes) =
Labels.Key(s).toEither.left.map(_.toList.mkString("\n")).map(_ => '{ Labels.Key.make(${ Expr(s) }) })
}

object LabelValueLiteral extends Literally[Labels.Value]:
object LabelValueLiteral extends Literally[Labels.Value] {
def validate(s: String)(using Quotes) =
Labels.Value(s).toEither.left.map(_.toList.mkString("\n")).map(_ => '{ Labels.Value.make(${ Expr(s) }) })
}
5 changes: 3 additions & 2 deletions core/src/main/scala-3/no/nrk/bigquery/util/Sized.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Sized[+Repr, N <: Nat](val unsized: Repr) {

override def equals(other: Any): Boolean =
other match {
case o: Sized[_, _] => unsized == o.unsized
case o: Sized[?, ?] => unsized == o.unsized
case _ => false
}

Expand All @@ -25,8 +25,9 @@ object Sized {
def wrap[Repr, L <: Nat](r: Repr): Sized[Repr, L] =
new Sized[Repr, L](r)

extension [A, N <: Nat](sized: Sized[IndexedSeq[A], N])
extension [A, N <: Nat](sized: Sized[IndexedSeq[A], N]) {
def map[B](f: A => B): Sized[IndexedSeq[B], N] =
new Sized[IndexedSeq[B], N](sized.unsized.map(f(_)))
def length: Int = sized.unsized.length
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package no.nrk.bigquery

import cats.Show
import cats.syntax.show._
import cats.syntax.show.*
import com.google.cloud.bigquery.{BigQueryError, JobId, Jsonify}
import BQExecutionExceptionInstances._
import BQExecutionExceptionInstances.*

case class BQExecutionException(
jobId: JobId,
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/no/nrk/bigquery/BQPoll.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
package no.nrk.bigquery

import cats.effect.Async
import cats.effect.implicits._
import cats.syntax.all._
import cats.effect.implicits.*
import cats.syntax.all.*
import com.google.cloud.bigquery.{BigQueryError, Job, JobStatus}
import org.typelevel.log4cats.LoggerFactory

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
import scala.util.Random

sealed trait BQPoll
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/scala/no/nrk/bigquery/BQRead.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import io.circe.Json
import org.apache.avro.util.Utf8
import org.apache.avro

import java.time._
import scala.collection.compat._
import scala.jdk.CollectionConverters._
import java.time.*
import scala.collection.compat.*
import scala.jdk.CollectionConverters.*

import scala.reflect.ClassTag

Expand Down Expand Up @@ -72,9 +72,9 @@ object BQRead extends BQReadCompat {
override def read(transportSchema: avro.Schema, value: Any): I[A] = {
val b = cb.newBuilder
value match {
case coll: scala.Array[_] =>
case coll: scala.Array[?] =>
coll.foreach(elem => b += BQRead[A].read(transportSchema.getElementType, elem))
case coll: java.util.Collection[_] =>
case coll: java.util.Collection[?] =>
coll.forEach(elem => b += BQRead[A].read(transportSchema.getElementType, elem))
case other =>
sys.error(
Expand All @@ -93,9 +93,9 @@ object BQRead extends BQReadCompat {
override def read(transportSchema: avro.Schema, value: Any): Array[A] = {
val b = Array.newBuilder[A]
value match {
case coll: scala.Array[_] =>
case coll: scala.Array[?] =>
coll.foreach(elem => b += BQRead[A].read(transportSchema.getElementType, elem))
case coll: java.util.Collection[_] =>
case coll: java.util.Collection[?] =>
coll.forEach(elem => b += BQRead[A].read(transportSchema.getElementType, elem))
case other =>
sys.error(
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/no/nrk/bigquery/BQShow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object BQShow extends BQShowInstances {
}
}

def bqfr(args: BQSqlFrag.Magnet*): BQSqlFrag = bqsql(args: _*)
def bqfr(args: BQSqlFrag.Magnet*): BQSqlFrag = bqsql(args*)
}

}
Expand Down
18 changes: 9 additions & 9 deletions core/src/main/scala/no/nrk/bigquery/BQSqlFrag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

package no.nrk.bigquery

import cats.syntax.all._
import no.nrk.bigquery.syntax._
import cats.syntax.all.*
import no.nrk.bigquery.syntax.*
import no.nrk.bigquery.BQSqlFrag.asSubQuery

import scala.annotation.tailrec
Expand Down Expand Up @@ -78,7 +78,7 @@ sealed trait BQSqlFrag {
}

final lazy val asStringWithUDFs: String = {
val udfs = allReferencedUDFs.collect { case udf: UDF.Temporary[_] => udf.definition.asString }
val udfs = allReferencedUDFs.collect { case udf: UDF.Temporary[?] => udf.definition.asString }
val udfsAsString = udfs.mkString("\n\n") + (if (udfs.nonEmpty) "\n\n" else "")
udfsAsString + asString
}
Expand Down Expand Up @@ -130,9 +130,9 @@ sealed trait BQSqlFrag {
def pf(outerRef: Option[BQPartitionId[Any]]): PartialFunction[BQSqlFrag, List[BQPartitionId[Any]]] = {
case BQSqlFrag.PartitionRef(partitionRef) =>
partitionRef.wholeTable match {
case tableDef: BQTableDef.View[_] if expandAndExcludeViews =>
case tableDef: BQTableDef.View[?] if expandAndExcludeViews =>
tableDef.query.collect(pf(Some(partitionRef))).flatten
case tvf: BQAppliedTableValuedFunction[_] if expandAndExcludeViews =>
case tvf: BQAppliedTableValuedFunction[?] if expandAndExcludeViews =>
tvf.query.collect(pf(Some(partitionRef))).flatten
case _ => List(partitionRef)
}
Expand Down Expand Up @@ -163,13 +163,13 @@ sealed trait BQSqlFrag {
final def allReferencedTables: Seq[BQTableLike[Any]] =
allReferencedAsPartitions
.map(_.wholeTable)
.filterNot(tableLike => tableLike.isInstanceOf[BQTableDef.View[_]])
.filterNot(tableLike => tableLike.isInstanceOf[BQTableDef.View[?]])

final def allReferencedTablesAsPartitions: Seq[BQPartitionId[Any]] =
allReferencedAsPartitions(expandAndExcludeViews = true)
.filterNot(pid => pid.wholeTable.isInstanceOf[BQTableDef.View[_]])
.filterNot(pid => pid.wholeTable.isInstanceOf[BQTableDef.View[?]])

final def allReferencedUDFs: Seq[UDF[UDF.UDFId, _]] =
final def allReferencedUDFs: Seq[UDF[UDF.UDFId, ?]] =
this.collect { case BQSqlFrag.Call(udf, _) => udf }.distinct

override def toString: String = asString
Expand All @@ -180,7 +180,7 @@ object BQSqlFrag {
def backticks(string: String): BQSqlFrag = Frag("`" + string + "`")

case class Frag(string: String) extends BQSqlFrag
case class Call(udf: UDF[UDF.UDFId, _], args: List[BQSqlFrag]) extends BQSqlFrag {
case class Call(udf: UDF[UDF.UDFId, ?], args: List[BQSqlFrag]) extends BQSqlFrag {
require(
udf.params.length == args.length,
show"UDF ${udf.name}: Expected ${udf.params.length} arguments, got ${args.length}"
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/no/nrk/bigquery/BQTableLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

package no.nrk.bigquery

import no.nrk.bigquery.syntax._
import no.nrk.bigquery.syntax.*
import cats.effect.Concurrent
import cats.syntax.all._
import cats.syntax.all.*
import no.nrk.bigquery.util.{Nat, Sized}

/** @tparam P
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/scala/no/nrk/bigquery/BigQueryClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ package no.nrk.bigquery

import cats.Show
import cats.data.OptionT
import cats.effect.implicits._
import cats.effect.implicits.*
import cats.effect.kernel.Outcome
import cats.effect.{Async, Resource}
import cats.syntax.all._
import cats.syntax.all.*
import com.google.api.gax.core.FixedCredentialsProvider
import com.google.api.gax.retrying.RetrySettings
import com.google.api.gax.rpc.ServerStream
import com.google.auth.Credentials
import com.google.cloud.bigquery.BigQuery.{JobOption, TableOption}
import com.google.cloud.bigquery.JobInfo.WriteDisposition
import com.google.cloud.bigquery.JobStatistics.LoadStatistics
import com.google.cloud.bigquery.storage.v1._
import com.google.cloud.bigquery.{Option => _, _}
import com.google.cloud.bigquery.storage.v1.*
import com.google.cloud.bigquery.{Option as _, *}
import com.google.cloud.http.HttpTransportOptions
import fs2.{Chunk, Stream}
import io.circe.Encoder
import no.nrk.bigquery.internal.{PartitionTypeHelper, SchemaHelper, TableUpdateOperation}
import no.nrk.bigquery.internal.GoogleTypeHelper._
import no.nrk.bigquery.internal.GoogleTypeHelper.*
import no.nrk.bigquery.metrics.{BQMetrics, MetricsOps}
import no.nrk.bigquery.util.StreamUtils
import org.apache.avro
Expand All @@ -37,8 +37,8 @@ import org.typelevel.log4cats.LoggerFactory
import java.time.Instant
import java.util.UUID
import java.util.concurrent.TimeUnit
import scala.concurrent.duration._
import scala.jdk.CollectionConverters._
import scala.concurrent.duration.*
import scala.jdk.CollectionConverters.*

class BigQueryClient[F[_]](
bigQuery: BigQuery,
Expand Down Expand Up @@ -130,7 +130,7 @@ class BigQueryClient[F[_]](
submitJob(jobId)(jobId =>
F.blocking(
Option(
bigQuery.create(JobInfo.of(jobId, queryRequest), jobOptions: _*)
bigQuery.create(JobInfo.of(jobId, queryRequest), jobOptions*)
)
)).flatMap {
case Some(job) => F.pure(job)
Expand Down Expand Up @@ -448,7 +448,7 @@ class BigQueryClient[F[_]](

F.interruptible(
Option(
bigQuery.create(JobInfo.of(jobId, jobConfiguration), jobOptions: _*)
bigQuery.create(JobInfo.of(jobId, jobConfiguration), jobOptions*)
)
)
}.flatMap {
Expand Down Expand Up @@ -505,12 +505,12 @@ class BigQueryClient[F[_]](
tableOptions: TableOption*
): F[Option[Table]] =
F.interruptible(
Option(bigQuery.getTable(tableId.underlying, tableOptions: _*))
Option(bigQuery.getTable(tableId.underlying, tableOptions*))
.filter(_.exists())
)

def getTableLike(tableId: BQTableId, tableOptions: TableOption*): F[Option[BQTableDef[Any]]] =
OptionT(getTable(tableId, tableOptions: _*)).mapFilter(t => SchemaHelper.fromTable(t).toOption).value
OptionT(getTable(tableId, tableOptions*)).mapFilter(t => SchemaHelper.fromTable(t).toOption).value

def tableExists(tableId: BQTableId): F[Table] =
getTable(tableId).flatMap {
Expand Down Expand Up @@ -561,7 +561,7 @@ class BigQueryClient[F[_]](
dataset: BQDataset.Ref,
datasetOptions: BigQuery.TableListOption*
): F[Vector[BQTableRef[Any]]] =
F.interruptible(bigQuery.listTables(dataset.underlying, datasetOptions: _*)).flatMap { tables =>
F.interruptible(bigQuery.listTables(dataset.underlying, datasetOptions*)).flatMap { tables =>
tables
.iterateAll()
.asScala
Expand Down
Loading

0 comments on commit 9e32509

Please sign in to comment.