Skip to content

Commit

Permalink
Derive IronTypeValidator
Browse files Browse the repository at this point in the history
Thx Iltotore for the guidance
  • Loading branch information
cheleb committed Oct 4, 2024
1 parent c364e25 commit 6f6a338
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 44 deletions.
6 changes: 3 additions & 3 deletions examples/client/src/main/scala/samples/EnumSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ val enums = {
color: Color
)

val eitherVar = Var(
val enumVar = Var(
Basket(Color.Black, Cat("Scala", 10, Color.White))
)
Sample(
"Enums",
eitherVar.asForm,
enumVar.asForm,
div(
child <-- eitherVar.signal.map { item =>
child <-- enumVar.signal.map { item =>
div(
s"$item"
)
Expand Down
12 changes: 6 additions & 6 deletions examples/client/src/main/scala/samples/Sealed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ val sealedClasses = {

case class Owner(name: String, pet: Animal)

val eitherVar = Var(Owner("Agnes", Horse("Niram <3", 6, Color.Isabelle)))
val sealedVar = Var(Owner("Agnes", Horse("Niram <3", 6, Color.Isabelle)))

case class Switcher(
name: String,
Expand All @@ -45,7 +45,7 @@ val sealedClasses = {
name,
button(
name.filter(_.isLetter),
onClick.mapToUnit --> (_ => eitherVar.update(_.copy(pet = a)))
onClick.mapToUnit --> (_ => sealedVar.update(_.copy(pet = a)))
)
)
}
Expand All @@ -65,18 +65,18 @@ val sealedClasses = {
Sample(
"Sealed",
div(
child <-- eitherVar.signal
child <-- sealedVar.signal
.distinctByFn((old, nw) => old.pet.getClass == nw.pet.getClass)
.map { item =>
div(
eitherVar.asForm,
sealedVar.asForm,
switchers
.filterNot(_.name == eitherVar.now().pet.getClass.getSimpleName)
.filterNot(_.name == sealedVar.now().pet.getClass.getSimpleName)
.map(_.button)
)
}
),
div(child <-- eitherVar.signal.map { item =>
div(child <-- sealedVar.signal.map { item =>
div(
s"$item"
)
Expand Down
2 changes: 2 additions & 0 deletions examples/client/src/main/scala/samples/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dev.cheleb.scalamigen.*

import com.raquo.laminar.api.L.*

/** Poc poc =D
*/
val tree = {

enum Tree[+T]:
Expand Down
16 changes: 1 addition & 15 deletions examples/client/src/main/scala/samples/Validation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ val validation = {
given Defaultable[Double :| GreaterEqual[8.0]] with
def default: Double :| GreaterEqual[8.0] = 8.0

given IronTypeValidator[Double, GreaterEqual[8.0]] =
_.toDoubleOption match
case None => Left("Not a number")
case Some(double) => double.refineEither[GreaterEqual[8.0]]

val ironSampleVar = Var(
IronSample(CurrencyCode("Eur"), Some("name"), Some(1), 9.1, Some(1))
)
Expand Down Expand Up @@ -71,16 +66,7 @@ val validation = {
|
| given Defaultable[Double :| GreaterEqual[8.0]] with
| def default: Double :| GreaterEqual[8.0] = 8.0
|
| given Validator[IronSample] with
| def isValid(a: IronSample): Boolean =
| true
|
| given IronTypeValidator[Double, GreaterEqual[8.0]] =
| _.toDoubleOption match
| case None => Left("Not a number")
| case Some(double) => double.refineEither[GreaterEqual[8.0]]
|
|
| val ironSampleVar = Var(
| IronSample(CurrencyCode("Eur"), Some("name"), Some(1), 9.1, Some(1))
| )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.cheleb.scalamigen

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*

/** Type validator for
* [IronType](https://iltotore.github.io/iron/docs/index.html).
Expand All @@ -17,22 +16,20 @@ trait IronTypeValidator[T, C] {

object IronTypeValidator {

/** Validator for [Iron type Double
* positive](https://iltotore.github.io/iron/io/github/iltotore/iron/constraint/numeric$.html#Positive-0).
/** Create an IronTypeValidator for a given IronType.
*
* @param baseValidator
* @param constraint
* @return
*/
given IronTypeValidator[Double, Positive] with
def validate(a: String): Either[String, IronType[Double, Positive]] =
a.toDoubleOption match
case None => Left("Not a number")
case Some(double) => double.refineEither[Positive]
given [A, C](using
baseValidator: Validator[A],
constraint: RuntimeConstraint[A, C]
): IronTypeValidator[A, C] with
def validate(a: String): Either[String, IronType[A, C]] =
baseValidator.validate(a).flatMap { a =>
if constraint.test(a) then Right(a.assume[C])
else Left(constraint.message)
}

// inline transparent given [A, C](using
// baseValidator: Validator[A],
// constraint: Constraint[A, C]
// ): IronTypeValidator[A, C] = new IronTypeValidator[A, C] {
// def validate(a: String): Either[String, IronType[A, C]] =
// baseValidator.validate(a).flatMap { a =>
// a.refineEither[C]
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ trait Validator[A] {
}

object Validator {
given Validator[Double] = new Validator[Double] {
override def validate(str: String): Either[String, Double] =
given Validator[Double] with
def validate(str: String): Either[String, Double] =
str.toDoubleOption.toRight("Not a number")
}
}

0 comments on commit 6f6a338

Please sign in to comment.