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

adding validations annotation to macros #297

Merged
merged 3 commits into from
Jul 15, 2022
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 @@ -227,6 +227,26 @@ object DeriveSchema {
.filter(_ != EmptyTree)
}.getOrElse(Nil)

@nowarn
val fieldValidations: List[Tree] =
tpe.typeSymbol.asClass.primaryConstructor.asMethod.paramLists.headOption.map { symbols =>
symbols.map { symbol =>
symbol.annotations.collect {
case annotation if (annotation.tree.tpe.toString.startsWith("zio.schema.annotation.validate")) =>
annotation.tree match {
case q"new $annConstructor(..$annotationArgs)" =>
q"..$annotationArgs"
case tree =>
c.warning(c.enclosingPosition, s"Unhandled annotation tree $tree")
EmptyTree
}
}
}.filter(_ != EmptyTree)
.map(_.foldLeft[c.universe.Tree](q"zio.schema.validation.Validation.succeed") {
case (acc, t) => q"$acc && $t"
})
}.getOrElse(Nil)

if (arity > 22) {
val fields = fieldTypes.zip(fieldAnnotations).map {
case (termSymbol, annotations) =>
Expand Down Expand Up @@ -284,8 +304,8 @@ object DeriveSchema {
case 22 => q"zio.schema.Schema.CaseClass22[..$typeArgs]"
}

val fieldDefs = fieldTypes.zip(fieldAnnotations).zipWithIndex.map {
case ((termSymbol, annotations), idx) =>
val fieldDefs = fieldTypes.zip(fieldAnnotations).zip(fieldValidations).zipWithIndex.map {
case (((termSymbol, annotations), validation), idx) =>
val fieldSchema = directInferSchema(
tpe,
concreteType(tpe, termSymbol.typeSignature),
Expand All @@ -295,9 +315,9 @@ object DeriveSchema {
val fieldLabel = termSymbol.name.toString.trim

if (annotations.nonEmpty)
q"""$fieldArg = zio.schema.Schema.Field.apply(label = $fieldLabel, schema = $fieldSchema, annotations = zio.Chunk.apply[Any](..$annotations))"""
q"""$fieldArg = zio.schema.Schema.Field.apply(label = $fieldLabel, schema = $fieldSchema, annotations = zio.Chunk.apply[Any](..$annotations), validation = $validation)"""
else
q"""$fieldArg = zio.schema.Schema.Field.apply(label = $fieldLabel, schema = $fieldSchema)"""
q"""$fieldArg = zio.schema.Schema.Field.apply(label = $fieldLabel, schema = $fieldSchema, validation = $validation)"""
}

val constructArgs = fieldTypes.zipWithIndex.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,19 @@ private case class DeriveSchema()(using val ctx: Quotes) extends ReflectionUtils

// Derive Field for a CaseClass
def deriveField(repr: TypeRepr, label: String, anns: List[Expr[Any]], stack: Stack) = {
import zio.schema.validation.Validation
repr.asType match { case '[t] =>
val schema = deriveSchema[t](stack)
val validators = anns.collect {
case ann if ann.isExprOf[Validation[t]] => ann.asExprOf[Validation[t]]
}
val validator: Expr[Validation[t]] = validators.foldLeft[Expr[Validation[t]]]('{Validation.succeed}){
case (acc, expr) => '{
$acc && $expr
}
}
val chunk = '{ zio.Chunk.fromIterable(${ Expr.ofSeq(anns.reverse) }) }
'{ Field(${Expr(label)}, $schema, $chunk) }
'{ Field(${Expr(label)}, $schema, $chunk, $validator) }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zio.schema.annotation

import zio.schema.validation.Validation

final case class validate[A](validation: Validation[A]) extends scala.annotation.StaticAnnotation
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ object Validation extends Regexs with Time {
def lessThan[A](value: A)(implicit numType: NumType[A]): Validation[A] =
Validation(Bool.Leaf(Num.LessThan(numType, value)))

def between[A](lower: A, upper: A)(implicit numType: NumType[A]): Validation[A] =
(greaterThan(lower) || equalTo(lower)) && ((lessThan(upper) || equalTo(upper)))

def equalTo[A](value: A)(implicit numType: NumType[A]): Validation[A] =
Validation(Bool.Leaf(Num.EqualTo(numType, value)))

Expand Down