-
Couldn't load subscription status.
- Fork 1.4k
Implement union types Gen instance derivation
#9723
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
test-magnolia-tests/src/test/scala-3/zio/test/magnolia/DeriveGenSpec_3.scala
This file contains hidden or 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,67 @@ | ||
| package zio.test.magnolia | ||
|
|
||
| import zio._ | ||
| import zio.test.Assertion._ | ||
| import zio.test.GenUtils._ | ||
| import zio.test.magnolia.DeriveGen._ | ||
| import zio.test._ | ||
| import zio.test.TestAspect.samples | ||
|
|
||
| import java.time.{Instant, LocalDate, LocalDateTime, LocalTime} | ||
| import java.util.UUID | ||
|
|
||
| object DeriveGenSpec_3 extends ZIOBaseSpec { | ||
|
|
||
| sealed trait Color | ||
| object Color { | ||
| case object Red extends Color | ||
| case object Green extends Color | ||
| case object Blue extends Color | ||
| } | ||
|
|
||
| val genColor: Gen[Any, Color] = DeriveGen[Color] | ||
|
|
||
| type UnionType = Color | Boolean | ||
| given DeriveGen[UnionType] = DeriveGen.unionType[UnionType] | ||
| val anyUnionType: Gen[Any, UnionType] = DeriveGen[UnionType] | ||
|
|
||
| type NestedUnionType = String | UnionType | ||
| given DeriveGen[NestedUnionType] = DeriveGen.unionType[NestedUnionType] | ||
| val anyNestedUnionType: Gen[Any, NestedUnionType] = DeriveGen[NestedUnionType] | ||
|
|
||
| def assertDeriveGen[A](implicit ev: DeriveGen[A]): TestResult = assertCompletes | ||
|
|
||
| def spec = suite("DeriveGenSpec_3")( | ||
| suite("union type")( | ||
| test("derivation")(assertDeriveGen[UnionType]), | ||
| test("generates Color values") { | ||
| check(Gen.listOfN(100)(anyUnionType)) { vs => | ||
| assertTrue(vs.exists(_.isInstanceOf[Color])) | ||
| } | ||
| }, | ||
| test("generates Boolean values") { | ||
| check(Gen.listOfN(100)(anyUnionType)) { vs => | ||
| assertTrue(vs.exists(_.isInstanceOf[Boolean])) | ||
| } | ||
| } | ||
| ), | ||
| suite("nested union type")( | ||
| test("derivation")(assertDeriveGen[NestedUnionType]), | ||
| test("generates Color values") { | ||
| check(Gen.listOfN(100)(anyNestedUnionType)) { vs => | ||
| assertTrue(vs.exists(_.isInstanceOf[Color])) | ||
| } | ||
| }, | ||
| test("generates Boolean values") { | ||
| check(Gen.listOfN(100)(anyNestedUnionType)) { vs => | ||
| assertTrue(vs.exists(_.isInstanceOf[Boolean])) | ||
| } | ||
| }, | ||
| test("generates String values") { | ||
| check(Gen.listOfN(100)(anyNestedUnionType)) { vs => | ||
| assertTrue(vs.exists(_.isInstanceOf[String])) | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| } |
This file contains hidden or 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
65 changes: 65 additions & 0 deletions
65
test-magnolia/src/main/scala-3/zio/test/magnolia/TypeUnionDerivation.scala
This file contains hidden or 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,65 @@ | ||
| package zio.test.magnolia | ||
|
|
||
| import scala.quoted.* | ||
|
|
||
| import zio.test.Gen | ||
|
|
||
| /** | ||
| * Implementation copied/adapted from Caliban | ||
| * | ||
| * See: | ||
| * - https://github.com/ghostdogpr/caliban/pull/2215 | ||
| * - https://github.com/ghostdogpr/caliban/blob/v2.10.0/core/src/main/scala-3/caliban/schema/TypeUnionDerivation.scala | ||
| */ | ||
| object TypeUnionDerivation { | ||
| inline def derived[T]: DeriveGen[T] = ${ typeUnionDeriveGen[T] } | ||
|
|
||
| def typeUnionDeriveGen[T](using Quotes, Type[T]): Expr[DeriveGen[T]] = { | ||
| import quotes.reflect.* | ||
|
|
||
| val typeName = TypeRepr.of[T].show | ||
|
|
||
| if (typeName.contains("|")) { | ||
| report.error( | ||
| s"You must explicitly add type parameter to derive DeriveGen for a union type in order to capture the name of the type alias" | ||
| ) | ||
| } | ||
|
|
||
| class TypeAndDeriveGen[A](val deriveGen: Expr[DeriveGen[A]], val tpe: Type[A]) | ||
|
|
||
| def rec[A](using tpe: Type[A]): List[TypeAndDeriveGen[?]] = | ||
| TypeRepr.of(using tpe).dealias match { | ||
| case OrType(l, r) => | ||
| rec(using l.asType.asInstanceOf[Type[Any]]) ++ rec(using r.asType.asInstanceOf[Type[Any]]) | ||
| case otherRepr => | ||
| val expr: TypeAndDeriveGen[A] = | ||
| Expr.summon[DeriveGen[A]] match { | ||
| case Some(foundDeriveGen) => | ||
| TypeAndDeriveGen[A](foundDeriveGen, otherRepr.asType.asInstanceOf[Type[A]]) | ||
| case None => | ||
| val otherString: String = otherRepr.show | ||
| quotes.reflect.report.errorAndAbort(s"Couldn't resolve DeriveGen[$otherString]") | ||
| } | ||
|
|
||
| List(expr) | ||
| } | ||
|
|
||
| val typeAndDeriveGens: List[TypeAndDeriveGen[?]] = rec[T] | ||
|
|
||
| val deriveGenByTypeNameList: Expr[List[DeriveGen[Any]]] = Expr.ofList( | ||
| typeAndDeriveGens.map { case (t: TypeAndDeriveGen[a]) => | ||
| given Type[a] = t.tpe | ||
| '{ ${ t.deriveGen }.asInstanceOf[DeriveGen[Any]] } | ||
| } | ||
| ) | ||
|
|
||
| '{ | ||
| new DeriveGen[T] { | ||
| private lazy val gen: Gen[Any, T] = | ||
| Gen.oneOf[Any, T](${ deriveGenByTypeNameList }.map(_.derive).asInstanceOf[List[Gen[Any, T]]]*) | ||
|
|
||
| def derive: Gen[Any, T] = gen | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't read through the code too closely, but do we need this to be in the
magnoliapackage? Afaict we're not using magnolia here (or maybe we are and I missed it, in that case please ignore me)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not using Magnolia here indeed. Where should I put this?
Did a quick research.
DeriveGenis a concept coming fromzio-test-magnoliamoduleUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kyri-petrou Any chance to get this merged, please? I need it at work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I'm getting way too many github notifications and I missed this. Merging now :)