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

Do not use package object for syntaxes #838

Draft
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.kernel.Band
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,17 +16,15 @@

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
// see: https://typelevel.org/cats/guidelines.html#implicit-instance-priority
// 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
Expand All @@ -37,21 +35,21 @@ 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
implicit def genGroup[T](implicit lp: shapeless.LowPriority): Group[T] =
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
Expand All @@ -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]
}
34 changes: 34 additions & 0 deletions cats/src/main/scala/magnolify/cats/CatsInstances.scala
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 21 additions & 21 deletions cats/src/main/scala/magnolify/cats/CatsMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.kernel.CommutativeGroup
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.kernel.CommutativeMonoid
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.kernel.CommutativeSemigroup
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.Eq
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.Group
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.Hash
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.Monoid
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.Semigroup
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package magnolify.cats.semiauto
package magnolify.cats

import cats.Show
import magnolia1._
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
54 changes: 54 additions & 0 deletions guava/src/main/scala/magnolify/guava/FunnelDerivation.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading