From e1e5f693384143de813776d8d5f94617b0cf408f Mon Sep 17 00:00:00 2001 From: Michel Davit Date: Thu, 14 Sep 2023 11:38:45 +0200 Subject: [PATCH] Do not use package object for syntaxes --- .../cats/{semiauto => }/BandDerivation.scala | 2 +- ...package.scala => CatsAutoDerivation.scala} | 18 +++---- .../scala/magnolify/cats/CatsInstances.scala | 34 ++++++++++++ .../scala/magnolify/cats/CatsMacros.scala | 42 +++++++-------- .../CommutativeGroupDerivation.scala | 2 +- .../CommutativeMonoidDerivation.scala | 2 +- .../CommutativeSemigroupDerivation.scala | 2 +- .../cats/{semiauto => }/EqDerivation.scala | 2 +- .../cats/{semiauto => }/GroupDerivation.scala | 2 +- .../cats/{semiauto => }/HashDerivation.scala | 2 +- .../{semiauto => }/MonoidDerivation.scala | 2 +- .../{semiauto => }/SemigroupDerivation.scala | 2 +- .../cats/{semiauto => }/ShowDerivation.scala | 2 +- .../main/scala/magnolify/cats/syntaxes.scala | 7 +-- .../magnolify/guava/FunnelDerivation.scala | 54 +++++++++++++++++++ ...Derivation.scala => FunnelImplicits.scala} | 37 +------------ .../magnolify/guava/GuavaAutoDerivation.scala | 23 ++++++++ .../magnolify/guava/GuavaInstances.scala | 21 ++++++++ .../{auto/package.scala => GuavaMacros.scala} | 13 ++--- .../main/scala/magnolify/guava/syntaxes.scala | 20 +++++++ .../{semiauto => }/ArbitraryDerivation.scala | 2 +- .../{semiauto => }/CogenDerivation.scala | 2 +- .../scalacheck/ScalacheckAutoDerivation.scala | 24 +++++++++ .../scalacheck/ScalacheckInstances.scala | 24 +++++++++ .../package.scala => ScalacheckMacros.scala} | 17 +++--- .../scala/magnolify/scalacheck/syntaxes.scala | 20 +++++++ 26 files changed, 279 insertions(+), 99 deletions(-) rename cats/src/main/scala/magnolify/cats/{semiauto => }/BandDerivation.scala (97%) rename cats/src/main/scala/magnolify/cats/{auto/package.scala => CatsAutoDerivation.scala} (85%) create mode 100644 cats/src/main/scala/magnolify/cats/CatsInstances.scala rename cats/src/main/scala/magnolify/cats/{semiauto => }/CommutativeGroupDerivation.scala (98%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/CommutativeMonoidDerivation.scala (98%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/CommutativeSemigroupDerivation.scala (98%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/EqDerivation.scala (97%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/GroupDerivation.scala (98%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/HashDerivation.scala (98%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/MonoidDerivation.scala (99%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/SemigroupDerivation.scala (98%) rename cats/src/main/scala/magnolify/cats/{semiauto => }/ShowDerivation.scala (97%) rename guava/src/main/scala/magnolify/guava/semiauto/package.scala => cats/src/main/scala/magnolify/cats/syntaxes.scala (81%) create mode 100644 guava/src/main/scala/magnolify/guava/FunnelDerivation.scala rename guava/src/main/scala/magnolify/guava/{semiauto/FunnelDerivation.scala => FunnelImplicits.scala} (59%) create mode 100644 guava/src/main/scala/magnolify/guava/GuavaAutoDerivation.scala create mode 100644 guava/src/main/scala/magnolify/guava/GuavaInstances.scala rename guava/src/main/scala/magnolify/guava/{auto/package.scala => GuavaMacros.scala} (66%) create mode 100644 guava/src/main/scala/magnolify/guava/syntaxes.scala rename scalacheck/src/main/scala/magnolify/scalacheck/{semiauto => }/ArbitraryDerivation.scala (98%) rename scalacheck/src/main/scala/magnolify/scalacheck/{semiauto => }/CogenDerivation.scala (97%) create mode 100644 scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckAutoDerivation.scala create mode 100644 scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckInstances.scala rename scalacheck/src/main/scala/magnolify/scalacheck/{auto/package.scala => ScalacheckMacros.scala} (64%) create mode 100644 scalacheck/src/main/scala/magnolify/scalacheck/syntaxes.scala diff --git a/cats/src/main/scala/magnolify/cats/semiauto/BandDerivation.scala b/cats/src/main/scala/magnolify/cats/BandDerivation.scala similarity index 97% rename from cats/src/main/scala/magnolify/cats/semiauto/BandDerivation.scala rename to cats/src/main/scala/magnolify/cats/BandDerivation.scala index d46d470c3..4a4121eb1 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/BandDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/BandDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.kernel.Band import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/auto/package.scala b/cats/src/main/scala/magnolify/cats/CatsAutoDerivation.scala similarity index 85% rename from cats/src/main/scala/magnolify/cats/auto/package.scala rename to cats/src/main/scala/magnolify/cats/CatsAutoDerivation.scala index 904de08dc..3d72fa70d 100644 --- a/cats/src/main/scala/magnolify/cats/auto/package.scala +++ b/cats/src/main/scala/magnolify/cats/CatsAutoDerivation.scala @@ -1,5 +1,5 @@ /* - * Copyright 2019 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. @@ -16,7 +16,7 @@ package magnolify.cats -import cats._ +import cats.{Eq, Group, Hash, Monoid, Semigroup, Show} import cats.kernel.{Band, CommutativeGroup, CommutativeMonoid, CommutativeSemigroup} // set implicit priority to avoid conflicts @@ -24,9 +24,7 @@ import cats.kernel.{Band, CommutativeGroup, CommutativeMonoid, CommutativeSemigr // use shapeless.LowPriority so the // provided cats type classes are always preferred // triggers derivation as last resort -package object auto extends LowPriority0Implicits - -trait LowPriority0Implicits extends LowPriority2Implicits { +trait CatsAutoDerivation0 extends CatsAutoDerivation1 { implicit def genShow[T](implicit lp: shapeless.LowPriority): Show[T] = macro CatsMacros.genShowMacro[T] // CommutativeGroup <: Group | CommutativeMonoid @@ -37,7 +35,7 @@ trait LowPriority0Implicits extends LowPriority2Implicits { macro CatsMacros.genHashMacro[T] } -trait LowPriority2Implicits extends LowPriority3Implicits { +trait CatsAutoDerivation1 extends CatsAutoDerivation2 { implicit def genEq[T](implicit lp: shapeless.LowPriority): Eq[T] = macro CatsMacros.genEqMacro[T] // Group <: Monoid @@ -45,13 +43,13 @@ trait LowPriority2Implicits extends LowPriority3Implicits { macro CatsMacros.genGroupMacro[T] } -trait LowPriority3Implicits extends LowPriority4Implicits { +trait CatsAutoDerivation2 extends CatsAutoDerivation3 { // CommutativeMonoid <: Monoid | CommutativeSemigroup implicit def genCommutativeMonoid[T](implicit lp: shapeless.LowPriority): CommutativeMonoid[T] = macro CatsMacros.genCommutativeMonoidMacro[T] } -trait LowPriority4Implicits extends LowPriority5Implicits { +trait CatsAutoDerivation3 extends CatsAutoDerivation4 { // CommutativeSemigroup <: Semigroup implicit def genCommutativeSemigroup[T](implicit lp: shapeless.LowPriority @@ -62,13 +60,13 @@ trait LowPriority4Implicits extends LowPriority5Implicits { macro CatsMacros.genMonoidMacro[T] } -trait LowPriority5Implicits extends LowPriority6Implicits { +trait CatsAutoDerivation4 extends CatsAutoDerivation5 { // Band <: Semigroup implicit def genBand[T](implicit lp: shapeless.LowPriority): Band[T] = macro CatsMacros.genBandMacro[T] } -trait LowPriority6Implicits { +trait CatsAutoDerivation5 { implicit def genSemigroup[T](implicit lp: shapeless.LowPriority): Semigroup[T] = macro CatsMacros.genSemigroupMacro[T] } diff --git a/cats/src/main/scala/magnolify/cats/CatsInstances.scala b/cats/src/main/scala/magnolify/cats/CatsInstances.scala new file mode 100644 index 000000000..73e931db1 --- /dev/null +++ b/cats/src/main/scala/magnolify/cats/CatsInstances.scala @@ -0,0 +1,34 @@ +/* + * 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.cats + +trait CatsInstances { + val BandDerivation: magnolify.cats.BandDerivation.type = magnolify.cats.BandDerivation + val CommutativeGroupDerivation: magnolify.cats.CommutativeGroupDerivation.type = + magnolify.cats.CommutativeGroupDerivation + val CommutativeMonoidDerivation: magnolify.cats.CommutativeMonoidDerivation.type = + magnolify.cats.CommutativeMonoidDerivation + val CommutativeSemigroupDerivation: magnolify.cats.CommutativeSemigroupDerivation.type = + magnolify.cats.CommutativeSemigroupDerivation + val EqDerivation: magnolify.cats.EqDerivation.type = magnolify.cats.EqDerivation + val GroupDerivation: magnolify.cats.GroupDerivation.type = magnolify.cats.GroupDerivation + val HashDerivation: magnolify.cats.HashDerivation.type = magnolify.cats.HashDerivation + val MonoidDerivation: magnolify.cats.MonoidDerivation.type = magnolify.cats.MonoidDerivation + val SemigroupDerivation: magnolify.cats.SemigroupDerivation.type = + magnolify.cats.SemigroupDerivation + val ShowDerivation: magnolify.cats.ShowDerivation.type = magnolify.cats.ShowDerivation +} diff --git a/cats/src/main/scala/magnolify/cats/CatsMacros.scala b/cats/src/main/scala/magnolify/cats/CatsMacros.scala index a7afec64b..73eab97da 100644 --- a/cats/src/main/scala/magnolify/cats/CatsMacros.scala +++ b/cats/src/main/scala/magnolify/cats/CatsMacros.scala @@ -16,78 +16,78 @@ package magnolify.cats -import scala.reflect.macros._ import scala.annotation.nowarn +import scala.reflect.macros.* private object CatsMacros { @nowarn("msg=parameter lp in method genShowMacro is never used") def genShowMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.ShowDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.ShowDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genEqMacro is never used") def genEqMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.EqDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.EqDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genHashMacro is never used") def genHashMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.HashDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.HashDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genSemigroupMacro is never used") def genSemigroupMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.SemigroupDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.SemigroupDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genMonoidMacro is never used") def genMonoidMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.MonoidDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.MonoidDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genCommutativeSemigroupMacro is never used") def genCommutativeSemigroupMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.CommutativeSemigroupDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.CommutativeSemigroupDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genCommutativeMonoidMacro is never used") def genCommutativeMonoidMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.CommutativeMonoidDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.CommutativeMonoidDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genGroupMacro is never used") def genGroupMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.GroupDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.GroupDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genCommutativeGroupMacro is never used") def genCommutativeGroupMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.CommutativeGroupDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.CommutativeGroupDerivation.apply[$wtt]""" } @nowarn("msg=parameter lp in method genBandMacro is never used") def genBandMacro[T: c.WeakTypeTag](c: whitebox.Context)(lp: c.Tree): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.cats.semiauto.BandDerivation.apply[$wtt]""" + q"""_root_.magnolify.cats.BandDerivation.apply[$wtt]""" } } diff --git a/cats/src/main/scala/magnolify/cats/semiauto/CommutativeGroupDerivation.scala b/cats/src/main/scala/magnolify/cats/CommutativeGroupDerivation.scala similarity index 98% rename from cats/src/main/scala/magnolify/cats/semiauto/CommutativeGroupDerivation.scala rename to cats/src/main/scala/magnolify/cats/CommutativeGroupDerivation.scala index 48b467938..fef518a17 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/CommutativeGroupDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/CommutativeGroupDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.kernel.CommutativeGroup import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/CommutativeMonoidDerivation.scala b/cats/src/main/scala/magnolify/cats/CommutativeMonoidDerivation.scala similarity index 98% rename from cats/src/main/scala/magnolify/cats/semiauto/CommutativeMonoidDerivation.scala rename to cats/src/main/scala/magnolify/cats/CommutativeMonoidDerivation.scala index d568f26da..2e6cf0c31 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/CommutativeMonoidDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/CommutativeMonoidDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.kernel.CommutativeMonoid import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/CommutativeSemigroupDerivation.scala b/cats/src/main/scala/magnolify/cats/CommutativeSemigroupDerivation.scala similarity index 98% rename from cats/src/main/scala/magnolify/cats/semiauto/CommutativeSemigroupDerivation.scala rename to cats/src/main/scala/magnolify/cats/CommutativeSemigroupDerivation.scala index 93fe80e1a..90ab1699c 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/CommutativeSemigroupDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/CommutativeSemigroupDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.kernel.CommutativeSemigroup import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/EqDerivation.scala b/cats/src/main/scala/magnolify/cats/EqDerivation.scala similarity index 97% rename from cats/src/main/scala/magnolify/cats/semiauto/EqDerivation.scala rename to cats/src/main/scala/magnolify/cats/EqDerivation.scala index 47624c7fd..b1a081fce 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/EqDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/EqDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.Eq import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/GroupDerivation.scala b/cats/src/main/scala/magnolify/cats/GroupDerivation.scala similarity index 98% rename from cats/src/main/scala/magnolify/cats/semiauto/GroupDerivation.scala rename to cats/src/main/scala/magnolify/cats/GroupDerivation.scala index ba0ad094a..776df1259 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/GroupDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/GroupDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.Group import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/HashDerivation.scala b/cats/src/main/scala/magnolify/cats/HashDerivation.scala similarity index 98% rename from cats/src/main/scala/magnolify/cats/semiauto/HashDerivation.scala rename to cats/src/main/scala/magnolify/cats/HashDerivation.scala index 001388000..448a64ed8 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/HashDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/HashDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.Hash import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/MonoidDerivation.scala b/cats/src/main/scala/magnolify/cats/MonoidDerivation.scala similarity index 99% rename from cats/src/main/scala/magnolify/cats/semiauto/MonoidDerivation.scala rename to cats/src/main/scala/magnolify/cats/MonoidDerivation.scala index 529190d1e..a1724b785 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/MonoidDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/MonoidDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.Monoid import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/SemigroupDerivation.scala b/cats/src/main/scala/magnolify/cats/SemigroupDerivation.scala similarity index 98% rename from cats/src/main/scala/magnolify/cats/semiauto/SemigroupDerivation.scala rename to cats/src/main/scala/magnolify/cats/SemigroupDerivation.scala index 92647a3db..4dfd9a513 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/SemigroupDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/SemigroupDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.Semigroup import magnolia1._ diff --git a/cats/src/main/scala/magnolify/cats/semiauto/ShowDerivation.scala b/cats/src/main/scala/magnolify/cats/ShowDerivation.scala similarity index 97% rename from cats/src/main/scala/magnolify/cats/semiauto/ShowDerivation.scala rename to cats/src/main/scala/magnolify/cats/ShowDerivation.scala index b2b4eed91..c0bdb669c 100644 --- a/cats/src/main/scala/magnolify/cats/semiauto/ShowDerivation.scala +++ b/cats/src/main/scala/magnolify/cats/ShowDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.cats.semiauto +package magnolify.cats import cats.Show import magnolia1._ diff --git a/guava/src/main/scala/magnolify/guava/semiauto/package.scala b/cats/src/main/scala/magnolify/cats/syntaxes.scala similarity index 81% rename from guava/src/main/scala/magnolify/guava/semiauto/package.scala rename to cats/src/main/scala/magnolify/cats/syntaxes.scala index ab45a50e4..f68d19d7f 100644 --- a/guava/src/main/scala/magnolify/guava/semiauto/package.scala +++ b/cats/src/main/scala/magnolify/cats/syntaxes.scala @@ -1,5 +1,5 @@ /* - * Copyright 2019 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. @@ -14,6 +14,7 @@ * limitations under the License. */ -package magnolify.guava +package magnolify.cats -package object semiauto extends FunnelImplicits +object auto extends CatsAutoDerivation0 +object semiauto extends CatsInstances diff --git a/guava/src/main/scala/magnolify/guava/FunnelDerivation.scala b/guava/src/main/scala/magnolify/guava/FunnelDerivation.scala new file mode 100644 index 000000000..74847176e --- /dev/null +++ b/guava/src/main/scala/magnolify/guava/FunnelDerivation.scala @@ -0,0 +1,54 @@ +/* + * 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: ReadOnlyCaseClass[Typeclass, T]): Typeclass[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[Typeclass, T]): Typeclass[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 apply[T]: Typeclass[T] = macro Magnolia.gen[T] + + def by[T, S](f: T => S)(implicit fnl: Funnel[S]): Funnel[T] = new Funnel[T] { + override def funnel(from: T, into: PrimitiveSink): Unit = fnl.funnel(f(from), into) + } +} diff --git a/guava/src/main/scala/magnolify/guava/semiauto/FunnelDerivation.scala b/guava/src/main/scala/magnolify/guava/FunnelImplicits.scala similarity index 59% rename from guava/src/main/scala/magnolify/guava/semiauto/FunnelDerivation.scala rename to guava/src/main/scala/magnolify/guava/FunnelImplicits.scala index 8d5eabaab..caaf12c9a 100644 --- a/guava/src/main/scala/magnolify/guava/semiauto/FunnelDerivation.scala +++ b/guava/src/main/scala/magnolify/guava/FunnelImplicits.scala @@ -1,5 +1,5 @@ /* - * Copyright 2019 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. @@ -14,45 +14,12 @@ * limitations under the License. */ -package magnolify.guava.semiauto +package magnolify.guava -import com.google.common.base.Charsets import com.google.common.hash.{Funnel, Funnels, PrimitiveSink} -import magnolia1._ import scala.annotation.nowarn -object FunnelDerivation { - type Typeclass[T] = Funnel[T] - - def join[T](caseClass: ReadOnlyCaseClass[Typeclass, T]): Typeclass[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[Typeclass, T]): Typeclass[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 apply[T]: Typeclass[T] = macro Magnolia.gen[T] - - def by[T, S](f: T => S)(implicit fnl: Funnel[S]): Funnel[T] = new Funnel[T] { - override def funnel(from: T, into: PrimitiveSink): Unit = fnl.funnel(f(from), into) - } -} - trait FunnelImplicits { private def funnel[T](f: (PrimitiveSink, T) => Unit): Funnel[T] = new Funnel[T] { override def funnel(from: T, into: PrimitiveSink): Unit = f(into, from) diff --git a/guava/src/main/scala/magnolify/guava/GuavaAutoDerivation.scala b/guava/src/main/scala/magnolify/guava/GuavaAutoDerivation.scala new file mode 100644 index 000000000..fcbdf9253 --- /dev/null +++ b/guava/src/main/scala/magnolify/guava/GuavaAutoDerivation.scala @@ -0,0 +1,23 @@ +/* + * 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 + +trait GuavaAutoDerivation { + implicit def genFunnel[T]: Funnel[T] = macro GuavaMacros.genFunnelMacro[T] +} diff --git a/guava/src/main/scala/magnolify/guava/GuavaInstances.scala b/guava/src/main/scala/magnolify/guava/GuavaInstances.scala new file mode 100644 index 000000000..a753d71eb --- /dev/null +++ b/guava/src/main/scala/magnolify/guava/GuavaInstances.scala @@ -0,0 +1,21 @@ +/* + * 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 + +trait GuavaInstances { + val FunnelDerivation: magnolify.guava.FunnelDerivation.type = magnolify.guava.FunnelDerivation +} diff --git a/guava/src/main/scala/magnolify/guava/auto/package.scala b/guava/src/main/scala/magnolify/guava/GuavaMacros.scala similarity index 66% rename from guava/src/main/scala/magnolify/guava/auto/package.scala rename to guava/src/main/scala/magnolify/guava/GuavaMacros.scala index 56b4c6bfa..1e37952d2 100644 --- a/guava/src/main/scala/magnolify/guava/auto/package.scala +++ b/guava/src/main/scala/magnolify/guava/GuavaMacros.scala @@ -1,5 +1,5 @@ /* - * Copyright 2019 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. @@ -16,17 +16,14 @@ package magnolify.guava -import com.google.common.hash.Funnel -import magnolify.guava.semiauto.FunnelImplicits +import scala.reflect.macros.* -import scala.reflect.macros._ +private object GuavaMacros { -package object auto extends FunnelImplicits { def genFunnelMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.guava.semiauto.FunnelDerivation.apply[$wtt]""" + q"""_root_.magnolify.guava.FunnelDerivation.apply[$wtt]""" } - implicit def genFunnel[T]: Funnel[T] = macro genFunnelMacro[T] } diff --git a/guava/src/main/scala/magnolify/guava/syntaxes.scala b/guava/src/main/scala/magnolify/guava/syntaxes.scala new file mode 100644 index 000000000..f755445be --- /dev/null +++ b/guava/src/main/scala/magnolify/guava/syntaxes.scala @@ -0,0 +1,20 @@ +/* + * 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 + +object auto extends FunnelImplicits with GuavaAutoDerivation +object semiauto extends FunnelImplicits with GuavaInstances diff --git a/scalacheck/src/main/scala/magnolify/scalacheck/semiauto/ArbitraryDerivation.scala b/scalacheck/src/main/scala/magnolify/scalacheck/ArbitraryDerivation.scala similarity index 98% rename from scalacheck/src/main/scala/magnolify/scalacheck/semiauto/ArbitraryDerivation.scala rename to scalacheck/src/main/scala/magnolify/scalacheck/ArbitraryDerivation.scala index e6a882367..1632d6af1 100644 --- a/scalacheck/src/main/scala/magnolify/scalacheck/semiauto/ArbitraryDerivation.scala +++ b/scalacheck/src/main/scala/magnolify/scalacheck/ArbitraryDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.scalacheck.semiauto +package magnolify.scalacheck import magnolia1._ import org.scalacheck.rng.Seed diff --git a/scalacheck/src/main/scala/magnolify/scalacheck/semiauto/CogenDerivation.scala b/scalacheck/src/main/scala/magnolify/scalacheck/CogenDerivation.scala similarity index 97% rename from scalacheck/src/main/scala/magnolify/scalacheck/semiauto/CogenDerivation.scala rename to scalacheck/src/main/scala/magnolify/scalacheck/CogenDerivation.scala index 3f29618b2..c130a8d1f 100644 --- a/scalacheck/src/main/scala/magnolify/scalacheck/semiauto/CogenDerivation.scala +++ b/scalacheck/src/main/scala/magnolify/scalacheck/CogenDerivation.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package magnolify.scalacheck.semiauto +package magnolify.scalacheck import magnolia1._ import org.scalacheck.Cogen diff --git a/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckAutoDerivation.scala b/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckAutoDerivation.scala new file mode 100644 index 000000000..c3432c934 --- /dev/null +++ b/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckAutoDerivation.scala @@ -0,0 +1,24 @@ +/* + * 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.scalacheck + +import org.scalacheck.{Arbitrary, Cogen} + +trait ScalacheckAutoDerivation { + implicit def genArbitrary[T]: Arbitrary[T] = macro ScalacheckMacros.genArbitraryMacro[T] + implicit def genCogen[T]: Cogen[T] = macro ScalacheckMacros.genCogenMacro[T] +} diff --git a/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckInstances.scala b/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckInstances.scala new file mode 100644 index 000000000..c3558298f --- /dev/null +++ b/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckInstances.scala @@ -0,0 +1,24 @@ +/* + * 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.scalacheck + +trait ScalacheckInstances { + val ArbitraryDerivation: magnolify.scalacheck.ArbitraryDerivation.type = + magnolify.scalacheck.ArbitraryDerivation + val CogenDerivation: magnolify.scalacheck.CogenDerivation.type = + magnolify.scalacheck.CogenDerivation +} diff --git a/scalacheck/src/main/scala/magnolify/scalacheck/auto/package.scala b/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckMacros.scala similarity index 64% rename from scalacheck/src/main/scala/magnolify/scalacheck/auto/package.scala rename to scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckMacros.scala index e416350b1..931838bfe 100644 --- a/scalacheck/src/main/scala/magnolify/scalacheck/auto/package.scala +++ b/scalacheck/src/main/scala/magnolify/scalacheck/ScalacheckMacros.scala @@ -1,5 +1,5 @@ /* - * Copyright 2019 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. @@ -16,23 +16,20 @@ package magnolify.scalacheck -import org.scalacheck._ +import scala.reflect.macros.* -import scala.reflect.macros._ +private object ScalacheckMacros { -package object auto { def genArbitraryMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.scalacheck.semiauto.ArbitraryDerivation.apply[$wtt]""" + q"""_root_.magnolify.scalacheck.ArbitraryDerivation.apply[$wtt]""" } def genCogenMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { - import c.universe._ + import c.universe.* val wtt = weakTypeTag[T] - q"""_root_.magnolify.scalacheck.semiauto.CogenDerivation.apply[$wtt]""" + q"""_root_.magnolify.scalacheck.CogenDerivation.apply[$wtt]""" } - implicit def genArbitrary[T]: Arbitrary[T] = macro genArbitraryMacro[T] - implicit def genCogen[T]: Cogen[T] = macro genCogenMacro[T] } diff --git a/scalacheck/src/main/scala/magnolify/scalacheck/syntaxes.scala b/scalacheck/src/main/scala/magnolify/scalacheck/syntaxes.scala new file mode 100644 index 000000000..b59f5d169 --- /dev/null +++ b/scalacheck/src/main/scala/magnolify/scalacheck/syntaxes.scala @@ -0,0 +1,20 @@ +/* + * 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.scalacheck + +object auto extends ScalacheckAutoDerivation +object semiauto extends ScalacheckInstances