Skip to content

Commit

Permalink
allow factory method (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablf authored Feb 27, 2024
1 parent 9bce206 commit 00d999b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
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

0 comments on commit 00d999b

Please sign in to comment.