Skip to content
Open
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Scala Steward: Reformat with scalafmt 3.8.6
818bb309d47dc5586750f30ba9bd8999feb02471
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.8.5"
version = "3.8.6"
runner.dialect = scala3
preset = IntelliJ
maxColumn = 120
Expand Down
10 changes: 2 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ lazy val vigilo =
.withoutSuffixFor(JVMPlatform)
.in(file("vigilo"))
.settings(sharedSettings *)
.settings(
name := "vigilo",
organization := "io.vigilo",
version := "0.0.1"
)
.jvmSettings(
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % "test"
)
.settings(name := "vigilo", organization := "io.vigilo", version := "0.0.1")
.jvmSettings(libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % "test")
// configure Scala-Native settings
.nativeSettings( /* ... */ ) // defined in sbt-scala-native
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.10")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.10")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.20.2")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.20.2")
253 changes: 94 additions & 159 deletions vigilo/jvm/src/test/scala/io/vigilo/ValidatorTest.scala

Large diffs are not rendered by default.

19 changes: 4 additions & 15 deletions vigilo/shared/src/main/scala/io/vigilo/core/annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ sealed trait validation extends StaticAnnotation
// validate null, None, empty string, zero number and rel.valid
case class required() extends validation

case class string(
blank: Boolean = false,
empty: Boolean = false,
min: Int = 0,
max: Int = 0
) extends validation
case class string(blank: Boolean = false, empty: Boolean = false, min: Int = 0, max: Int = 0) extends validation

// work with Relation or Option[Relation]
case class rel() extends validation
Expand All @@ -31,18 +26,12 @@ case class email() extends validation
case class regexp(pattern: String) extends validation

// Work with any value
case class choice(
group: String,
min: Int = 1,
max: Int = 1
) extends validation
case class choice(group: String, min: Int = 1, max: Int = 1) extends validation

// Work with any value
case class options(values: Seq[Any]) extends validation

case class list(min: Int = 0, max: Int = 0) extends validation

case class date(min: Instant | Null = null,
max: Instant | Null = null,
pattern: String,
time: Boolean = false) extends validation
case class date(min: Instant | Null = null, max: Instant | Null = null, pattern: String, time: Boolean = false)
extends validation
26 changes: 8 additions & 18 deletions vigilo/shared/src/main/scala/io/vigilo/core/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,32 @@ import scala.quoted.*

object macros:

case class FieldInfo(
name: String,
annotations: Seq[validation]
)
case class FieldInfo(name: String, annotations: Seq[validation])

inline def extractValidationsFields[T]: Seq[FieldInfo] = ${ extractValidationsFieldsImpl[T] }

def extractValidationsFieldsImpl[T: Type](using
Quotes
): Expr[Seq[FieldInfo]] = {
def extractValidationsFieldsImpl[T: Type](using Quotes): Expr[Seq[FieldInfo]] = {
import quotes.reflect.*

val targetSym = TypeRepr.of[T].typeSymbol
val targetSym = TypeRepr.of[T].typeSymbol
val baseAnnoType = TypeRepr.of[validation]

def getAnnotations[A <: validation](using
Type[A]
): Seq[Expr[(String, Seq[A])]] = {
def getAnnotations[A <: validation](using Type[A]): Seq[Expr[(String, Seq[A])]] = {

val entries = targetSym
.primaryConstructor
.paramSymss
.flatten
val entries = targetSym.primaryConstructor.paramSymss.flatten
.map { field =>
val fieldName = Expr(field.name)
val fieldName = Expr(field.name)
// Filtra apenas anotações que estendem BaseAnnotation
val fieldAnnotations = field.annotations.filter { anno =>
anno.tpe <:< baseAnnoType
}
// Converte as anotações encontradas para uma expressão de lista
val annotationExprs = fieldAnnotations.map { anno =>
val annotationExprs = fieldAnnotations.map { anno =>
// Para cada anotação, criamos uma expressão para instanciá-la
val annoTerm = anno.asExpr.asInstanceOf[Expr[A]]
annoTerm
}
val annoListExpr = Expr.ofSeq(annotationExprs)
val annoListExpr = Expr.ofSeq(annotationExprs)
// Cria a entrada do map (nome do campo -> lista de anotações)
'{ ($fieldName, $annoListExpr) }
}
Expand Down
Loading