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

Make possible to create factory methods for Deriver API #662

Merged
merged 1 commit into from
Feb 27, 2024
Merged
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 @@ -372,9 +372,8 @@ object Derive {
q"""$deriver.deriveUnknown[$tpe]($summoned)"""
}

val tree = recurse(weakTypeOf[A], schema.tree, List.empty[Frame[c.type]], top = true)
//println(tree)
tree
recurse(weakTypeOf[A], schema.tree, List.empty[Frame[c.type]], top = true)

}

final case class Frame[C <: whitebox.Context](ctx: C, ref: String, tpe: C#Type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package zio.schema

import scala.language.experimental.macros
import scala.reflect.macros.whitebox

/**
* Useful to create factory methods.
*
* import Factory._
* def createSomeTrait[A: Factory](deriver: Deriver[SomeTrait])(implicit schema: Schema[A]): SomeTrait[A] =
* implicitly[Factory[A]].derive[SomeTrait](deriver)
*
*/
trait Factory[A] {
def derive[F[_]](deriver: Deriver[F])(implicit schema: Schema[A]): F[A]
}

object Factory {

implicit def factory[A]: Factory[A] = macro factoryImpl[A]

def factoryImpl[A: c.WeakTypeTag](
c: whitebox.Context
): c.Tree = {
import c.universe._

val tpeA = weakTypeOf[A]

q"""
new Factory[$tpeA] {
override def derive[F[_]](deriver: Deriver[F])(implicit schema: Schema[$tpeA]): F[$tpeA] = Derive.derive[F, $tpeA](deriver)

}

"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import zio.Chunk
import Schema._

object Derive {

inline def derive[F[_], A](deriver: Deriver[F])(implicit schema: Schema[A]): F[A] = ${ deriveInstance[F, A]('deriver, 'schema) }

private def deriveInstance[F[_]: Type, A: Type](deriver: Expr[Deriver[F]], schema: Expr[Schema[A]])(using ctx: Quotes): Expr[F[A]] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zio.schema

/**
* Useful to create factory methods.
*
* import Factory._
* def createSomeTrait[A: Factory](deriver: Deriver[SomeTrait])(implicit schema: Schema[A]): SomeTrait[A] =
* implicitly[Factory[A]].derive[SomeTrait](deriver)
*
*/
trait Factory[A] {
def derive[F[_]](deriver: Deriver[F])(implicit schema: Schema[A]): F[A]
}

object Factory {

inline implicit def factory[A]: Factory[A] = new Factory[A] {
override def derive[F[_]](deriver: Deriver[F])(implicit schema: Schema[A]): F[A] = Derive.derive[F, A](deriver)(schema)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ import zio.{ Chunk, Scope }
)
}
),
suite("support factory")(
test("factory") {
import zio.schema.Factory
import zio.schema.Factory._
def createSomeTrait[A: Factory](deriver: Deriver[TC])(implicit schema: Schema[A]): TC[A] =
implicitly[Factory[A]].derive[TC](deriver)

implicit val im: Factory[Enum1] = factory[Enum1]

val tc = createSomeTrait[Enum1](deriver)
assertTrue(tc.isDerived == true)
}
),
versionSpecificSuite
)

Expand Down
Loading