-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Magnolia derivation for Debug typeclass
- Loading branch information
Showing
5 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
magnolia-tests/shared/src/test/scala/zio/debug/magnolia/DeriveDebugSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package zio.debug.magnolia | ||
|
||
import zio.Scope | ||
import zio.prelude._ | ||
import zio.test.Assertion._ | ||
import zio.test.{ZIOSpecDefault, _} | ||
|
||
case class Lair(name: String, animal: Animal) | ||
|
||
object Lair { | ||
implicit val debug: Debug[Lair] = DeriveDebug.derived[Lair] | ||
} | ||
|
||
case class Animal(name: String, age: Int) | ||
|
||
object Animal { | ||
implicit val debug: Debug[Animal] = DeriveDebug.derived[Animal] | ||
} | ||
|
||
case class Adult(name: String, age: Int, chidren: List[Child]) | ||
case class Child(name: String, age: Int) | ||
|
||
object Adult { | ||
implicit val debug: Debug[Adult] = DeriveDebug.derived[Adult] | ||
} | ||
|
||
object Child { | ||
implicit val debug: Debug[Child] = DeriveDebug.derived[Child] | ||
} | ||
|
||
object Test extends ZIOSpecDefault { | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("DeriveDebug")( | ||
test("should derive Debug for case class") { | ||
val animal = Animal("tiger", 10) | ||
|
||
assert(animal.debug.render)(equalTo("Animal(name = \"tiger\", age = 10)")) | ||
|
||
}, | ||
test("should derive Debug for nested case class") { | ||
val lair = Lair("jungle", Animal("tiger", 10)) | ||
|
||
assert(lair.debug.render)(equalTo("Lair(name = \"jungle\", animal = Animal(name = \"tiger\", age = 10))")) | ||
}, | ||
test("should derive Debug for case class with list") { | ||
val adult = Adult("John", 30, List(Child("Alice", 5), Child("Bob", 10))) | ||
|
||
assert(adult.debug.render)( | ||
equalTo( | ||
"Adult(name = \"John\", age = 30, chidren = List(Child(name = \"Alice\", age = 5), Child(name = \"Bob\", age = 10)))" | ||
) | ||
) | ||
} | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
magnolia/shared/src/main/scala-2/zio/debug/magnolia/DeriveDebug.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package zio.debug.magnolia | ||
|
||
import magnolia1._ | ||
import zio.prelude.Debug | ||
import zio.prelude.Debug.Repr | ||
|
||
import scala.collection.immutable.ListMap | ||
|
||
object DeriveDebug { | ||
|
||
type Typeclass[T] = Debug[T] | ||
|
||
def join[T](ctx: CaseClass[Debug, T]): Debug[T] = | ||
if (ctx.isValueClass) { (a: T) => | ||
Repr.VConstructor( | ||
ctx.typeName.owner.split('.').toList, | ||
ctx.typeName.short, | ||
ctx.parameters.map(p => p.typeclass.debug(p.dereference(a))).toList | ||
) | ||
} else if (ctx.isObject) { (_: T) => | ||
Repr.Object(ctx.typeName.owner.split('.').toList, ctx.typeName.short) | ||
} else { (a: T) => | ||
Repr.Constructor( | ||
ctx.typeName.owner.split('.').toList, | ||
ctx.typeName.short, | ||
ListMap.apply(ctx.parameters.map(p => p.label -> p.typeclass.debug(p.dereference(a))): _*) | ||
) | ||
} | ||
|
||
def split[T](ctx: SealedTrait[Debug, T]): Debug[T] = | ||
new Debug[T] { self => | ||
def debug(a: T): Repr = ctx.split(a) { sub => | ||
sub.typeclass.debug(sub.cast(a)) | ||
} | ||
} | ||
|
||
def derived[T]: Debug[T] = macro Magnolia.gen[T] | ||
} |
40 changes: 40 additions & 0 deletions
40
magnolia/shared/src/main/scala-3/zio/debug/magnolia/DeriveDebug.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package zio.debug.magnolia | ||
|
||
import magnolia1._ | ||
// import zio.prelude.Debug | ||
// import zio.prelude.Debug.Repr | ||
|
||
import scala.collection.immutable.ListMap | ||
import scala.language.experimental.macros | ||
import zio.prelude.Debug | ||
import zio.prelude.Debug.Repr | ||
|
||
object DeriveDebug extends AutoDerivation[Debug] { | ||
|
||
type Typeclass[T] = Debug[T] | ||
|
||
def join[T](ctx: CaseClass[Debug, T]): Debug[T] = | ||
if (ctx.isValueClass) { (a: T) => | ||
Repr.VConstructor( | ||
ctx.typeInfo.owner.split('.').toList, | ||
ctx.typeInfo.short, | ||
ctx.parameters.map(p => p.typeclass.debug(p.deref(a))).toList | ||
) | ||
} else if (ctx.isObject) { (_: T) => | ||
Repr.Object(ctx.typeInfo.owner.split('.').toList, ctx.typeInfo.short) | ||
} else { (a: T) => | ||
Repr.Constructor( | ||
ctx.typeInfo.owner.split('.').toList, | ||
ctx.typeInfo.short, | ||
ListMap.from(ctx.parameters.map(p => p.label -> p.typeclass.debug(p.deref(a)))) | ||
) | ||
} | ||
|
||
def split[T](ctx: SealedTrait[Debug, T]): Debug[T] = | ||
new Debug[T] { self => | ||
def debug(a: T): Repr = ctx.choose(a) { sub => | ||
sub.typeclass.debug(sub.cast(a)) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters