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

Cross-build guava module on scala 3 #860

Draft
wants to merge 21 commits into
base: scala3-scalacheck
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:

- name: Test
if: matrix.scala == '3'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' shared/test test/test
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' shared/test test/test scalacheck/test guava/test

- name: Check binary compatibility
if: '!(matrix.scala == ''3'')'
Expand Down
12 changes: 9 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ val tensorflowMetadataVersion = "1.10.0"
val tensorflowVersion = "0.5.0"

// project
ThisBuild / tlBaseVersion := "0.7"
ThisBuild / tlBaseVersion := "0.8"
ThisBuild / tlSonatypeUseLegacyHost := true
ThisBuild / organization := "com.spotify"
ThisBuild / organizationName := "Spotify AB"
Expand Down Expand Up @@ -110,7 +110,9 @@ val scala212 = "2.12.19"
val scalaDefault = scala213
val scala3Projects = List(
"shared",
"test"
"test",
"scalacheck",
"guava"
)

// github actions
Expand Down Expand Up @@ -267,7 +269,9 @@ val commonSettings = Seq(
"-Yretain-trees",
// tolerate some nested macro expansion
"-Xmax-inlines",
"64"
"64",
// silence warnings. dotty doesn't have unused-imports category nor origin support yet
"-Wconf:msg=unused import:s"
)
case Some((2, 13)) =>
Seq(
Expand Down Expand Up @@ -373,6 +377,7 @@ lazy val scalacheck = project
commonSettings,
moduleName := "magnolify-scalacheck",
description := "Magnolia add-on for ScalaCheck",
crossScalaVersions := Seq(scala3, scala213, scala212),
libraryDependencies += "org.scalacheck" %% "scalacheck" % scalacheckVersion % Provided
)

Expand Down Expand Up @@ -405,6 +410,7 @@ lazy val guava = project
commonSettings,
moduleName := "magnolify-guava",
description := "Magnolia add-on for Guava",
crossScalaVersions := Seq(scala3, scala213, scala212),
libraryDependencies ++= Seq(
"com.google.guava" % "guava" % guavaVersion % Provided
)
Expand Down
4 changes: 2 additions & 2 deletions docs/scalacheck.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import org.scalacheck._
case class Inner(int: Int, str: String)
case class Outer(inner: Inner)

val arb: Arbitrary[Outer] = ArbitraryDerivation[Outer]
val cogen: Cogen[Outer] = CogenDerivation[Outer]
val arb: Arbitrary[Outer] = Arbitrary.gen[Outer]
val cogen: Cogen[Outer] = Cogen.gen[Outer]
```
59 changes: 59 additions & 0 deletions guava/src/main/scala-2/magnolify/guava/FunnelDerivation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.guava

import com.google.common.base.Charsets
import com.google.common.hash.{Funnel, PrimitiveSink}
import magnolia1.*

import scala.annotation.nowarn

object FunnelDerivation {
type Typeclass[T] = Funnel[T]

def join[T](caseClass: CaseClass[Funnel, T]): Funnel[T] = new Funnel[T] {
override def funnel(from: T, into: PrimitiveSink): Unit =
if (caseClass.isValueClass) {
val p = caseClass.parameters.head
p.typeclass.funnel(p.dereference(from), into)
} else if (caseClass.parameters.isEmpty) {
into.putString(caseClass.typeName.short, Charsets.UTF_8): @nowarn
} else {
caseClass.parameters.foreach { p =>
// inject index to distinguish cases like `(Some(1), None)` and `(None, Some(1))`
into.putInt(p.index)
p.typeclass.funnel(p.dereference(from), into)
}
}
}

def split[T](sealedTrait: SealedTrait[Funnel, T]): Funnel[T] = new Funnel[T] {
override def funnel(from: T, into: PrimitiveSink): Unit =
sealedTrait.split(from)(sub => sub.typeclass.funnel(sub.cast(from), into))
}

implicit def gen[T]: Funnel[T] = macro Magnolia.gen[T]

@deprecated("Use gen instead", "0.7.0")
implicit def apply[T]: Funnel[T] = macro Magnolia.gen[T]

@deprecated("Use funnel.contramap instead", "0.7.0")
def by[T, S](f: T => S)(implicit fnl: Funnel[S]): Funnel[T] = {
import FunnelImplicits.*
fnl.contramap(f)
}
}
51 changes: 51 additions & 0 deletions guava/src/main/scala-2/magnolify/guava/FunnelImplicits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.guava

import com.google.common.hash.{Funnel, PrimitiveSink}

trait FunnelImplicits {
import FunnelImplicits.*
def Funnel[T](implicit fnl: Funnel[T]): Funnel[T] = fnl

implicit val intFunnel: Funnel[Int] = FunnelInstances.intFunnel()
implicit val longFunnel: Funnel[Long] = FunnelInstances.longFunnel()
implicit val bytesFunnel: Funnel[Array[Byte]] = FunnelInstances.bytesFunnel()
implicit val booleanFunnel: Funnel[Boolean] = FunnelInstances.booleanFunnel()
implicit val byteFunnel: Funnel[Byte] = FunnelInstances.byteFunnel()
implicit val charFunnel: Funnel[Char] = FunnelInstances.charFunnel()
implicit val shortFunnel: Funnel[Short] = FunnelInstances.shortFunnel()

implicit def charSequenceFunnel[T <: CharSequence]: Funnel[T] =
FunnelInstances.charSequenceFunnel[T]()

// There is an implicit Option[T] => Iterable[T]
implicit def iterableFunnel[T, C[_]](implicit
fnl: Funnel[T],
ti: C[T] => Iterable[T]
): Funnel[C[T]] = FunnelInstances.iterableFunnel(fnl)

implicit def funnelOps[T](fnl: Funnel[T]): FunnelOps[T] = new FunnelOps(fnl)
}

object FunnelImplicits extends FunnelImplicits {
final class FunnelOps[T](val fnl: Funnel[T]) extends AnyVal {
def contramap[U](f: U => T): Funnel[U] = new Funnel[U] {
override def funnel(from: U, into: PrimitiveSink): Unit = fnl.funnel(f(from), into)
}
}
}
39 changes: 39 additions & 0 deletions guava/src/main/scala-2/magnolify/guava/GuavaMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.guava

import com.google.common.hash.Funnel

import scala.reflect.macros.*

object GuavaMacros {

def genFunnelMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
import c.universe.*
val wtt = weakTypeTag[T]
q"""_root_.magnolify.guava.FunnelDerivation.gen[$wtt]"""
}

}

trait SemiAutoDerivations {
def genFunnel[T]: Funnel[T] = macro GuavaMacros.genFunnelMacro[T]
}

trait AutoDerivations {
implicit def genFunnel[T]: Funnel[T] = macro GuavaMacros.genFunnelMacro[T]
}
46 changes: 46 additions & 0 deletions guava/src/main/scala-3/magnolify/guava/FunnelDerivation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.guava

import com.google.common.base.Charsets
import com.google.common.hash.{Funnel, PrimitiveSink}
import magnolia1.*

import scala.annotation.nowarn
import scala.deriving.Mirror

object FunnelDerivation extends Derivation[Funnel]:

def join[T](caseClass: CaseClass[Funnel, T]): Funnel[T] = new Funnel[T]:
override def funnel(from: T, into: PrimitiveSink): Unit =
if (caseClass.isValueClass)
val p = caseClass.parameters.head
p.typeclass.funnel(p.deref(from), into)
else if (caseClass.parameters.isEmpty)
into.putString(caseClass.typeInfo.short, Charsets.UTF_8): @nowarn
else
caseClass.parameters.foreach { p =>
// inject index to distinguish cases like `(Some(1), None)` and `(None, Some(1))`
into.putInt(p.index)
p.typeclass.funnel(p.deref(from), into)
}

def split[T](sealedTrait: SealedTrait[Funnel, T]): Funnel[T] = new Funnel[T]:
override def funnel(from: T, into: PrimitiveSink): Unit =
sealedTrait.choose(from)(sub => sub.typeclass.funnel(sub.cast(from), into))

inline def gen[T](using Mirror.Of[T]): Funnel[T] = derivedMirror[T]
45 changes: 45 additions & 0 deletions guava/src/main/scala-3/magnolify/guava/FunnelImplicits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.guava

import com.google.common.hash.{Funnel, PrimitiveSink}

trait FunnelImplicits:
def Funnel[T](using fnl: Funnel[T]): Funnel[T] = fnl

given intFunnel: Funnel[Int] = FunnelInstances.intFunnel()
given longFunnel: Funnel[Long] = FunnelInstances.longFunnel()
given bytesFunnel: Funnel[Array[Byte]] = FunnelInstances.bytesFunnel()
given booleanFunnel: Funnel[Boolean] = FunnelInstances.booleanFunnel()
given byteFunnel: Funnel[Byte] = FunnelInstances.byteFunnel()
given charFunnel: Funnel[Char] = FunnelInstances.charFunnel()
given shortFunnel: Funnel[Short] = FunnelInstances.shortFunnel()

given charSequenceFunnel[T <: CharSequence]: Funnel[T] =
FunnelInstances.charSequenceFunnel[T]()

// There is an implicit Option[T] => Iterable[T]
given iterableFunnel[T, C[_]](using
fnl: Funnel[T],
ti: C[T] => Iterable[T]
): Funnel[C[T]] = FunnelInstances.iterableFunnel(fnl)

extension [T](fnl: Funnel[T])
def contramap[U](f: U => T): Funnel[U] = new Funnel[U]:
override def funnel(from: U, into: PrimitiveSink): Unit = fnl.funnel(f(from), into)

object FunnelImplicits extends FunnelImplicits
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Spotify AB
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,12 +16,12 @@

package magnolify.guava

import scala.reflect.macros.whitebox
import com.google.common.hash.Funnel

private object GuavaMacros {
def genFunnelMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
import c.universe._
val wtt = weakTypeTag[T]
q"""_root_.magnolify.guava.semiauto.FunnelDerivation.apply[$wtt]"""
}
}
import scala.deriving.Mirror

trait SemiAutoDerivations:
inline def genFunnel[T](using Mirror.Of[T]): Funnel[T] = FunnelDerivation.derivedMirror[T]

trait AutoDerivations:
inline given genFunnel[T](using Mirror.Of[T]): Funnel[T] = FunnelDerivation.derivedMirror[T]
52 changes: 52 additions & 0 deletions guava/src/main/scala/magnolify/guava/FunnelInstances.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.guava

import com.google.common.hash.{Funnel, Funnels, PrimitiveSink}

import scala.annotation.nowarn

object FunnelInstances {

private def funnel[T](f: (PrimitiveSink, T) => Any): Funnel[T] = new Funnel[T] {
override def funnel(from: T, into: PrimitiveSink): Unit = f(into, from): @nowarn
}

// respect naming convention from Funnels
def intFunnel(): Funnel[Int] = Funnels.integerFunnel().asInstanceOf[Funnel[Int]]
def longFunnel(): Funnel[Long] = Funnels.longFunnel().asInstanceOf[Funnel[Long]]
def bytesFunnel(): Funnel[Array[Byte]] = Funnels.byteArrayFunnel()
def booleanFunnel(): Funnel[Boolean] = funnel[Boolean](_.putBoolean(_))
def byteFunnel(): Funnel[Byte] = funnel[Byte](_.putByte(_))
def charFunnel(): Funnel[Char] = funnel[Char](_.putChar(_))
def shortFunnel(): Funnel[Short] = funnel[Short](_.putShort(_))

def charSequenceFunnel[T <: CharSequence](): Funnel[T] =
Funnels.unencodedCharsFunnel().asInstanceOf[Funnel[T]]

// There is an implicit Option[T] => Iterable[T]
def iterableFunnel[T, C[_]](fnl: Funnel[T])(implicit ti: C[T] => Iterable[T]): Funnel[C[T]] =
funnel { (sink, xs) =>
var i = 0
ti(xs).foreach { x =>
fnl.funnel(x, sink)
i += 1
}
// inject size to distinguish `None`, `Some("")`, and `List("", "", ...)`
sink.putInt(i)
}
}
Loading