-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
3 deletions.
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
37 changes: 37 additions & 0 deletions
37
zio-schema-derivation/shared/src/main/scala-2/zio/schema/Factory.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,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) | ||
} | ||
""" | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
zio-schema-derivation/shared/src/main/scala-3/zio/schema/Factory.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,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) | ||
} | ||
} |
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