Skip to content

Commit

Permalink
Refactor form derivation in Tree and ListElement samples
Browse files Browse the repository at this point in the history
  • Loading branch information
cheleb committed Sep 27, 2024
1 parent 96c6a84 commit 6603d78
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 88 deletions.
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/EitherSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ val either = {
either: Either[Cat, Dog],
primitiveEither: Either[Cat, String],
optionalInt: Option[Int]
) derives Form
)

val eitherVar = Var(
EitherSample(
Expand Down
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 @@ -9,20 +9,20 @@ import com.raquo.airstream.state.Var
import com.raquo.laminar.api.L

val enums = {
enum Color(val code: String) derives Form:
enum Color(val code: String):
case Black extends Color("000")
case White extends Color("FFF")
case Isabelle extends Color("???")

case class Basket(color: Color, cat: Cat) derives Form
case class Basket(color: Color, cat: Cat)

given colorForm: Form[Color] = enumForm(Color.values, Color.fromOrdinal)

case class Cat(
name: String,
age: Int,
color: Color
) derives Form
)

val eitherVar = Var(
Basket(Color.Black, Cat("Scala", 10, Color.White))
Expand Down
4 changes: 2 additions & 2 deletions examples/client/src/main/scala/samples/ListElement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import dev.cheleb.scalamigen.*
import com.raquo.laminar.api.L.*

val list = {
case class Person2(id: Int, name: String, age: Int) derives Form
case class Person2(id: Int, name: String, age: Int)

case class ListElement(
ints: List[Person2]
) derives Form
)

given (Person2 => Int) = _.id

Expand Down
6 changes: 3 additions & 3 deletions examples/client/src/main/scala/samples/Persons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ val person = {
email: Option[String],
age: BigInt,
size: Double
) derives Form
)
case class Pet(
name: String,
age: BigInt,
House: House,
size: Double :| Positive
) derives Form
)

case class House(capacity: Int) derives Form
case class House(capacity: Int)

// Provide default for optional
given Defaultable[Pet] with
Expand Down
10 changes: 5 additions & 5 deletions examples/client/src/main/scala/samples/Sealed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import com.raquo.airstream.state.Var
import com.raquo.laminar.nodes.ReactiveHtmlElement

val sealedClasses = {
sealed trait Animal derives Form
sealed trait Animal

case class Horse(name: String, age: Int) extends Animal derives Form
case class Horse(name: String, age: Int) extends Animal
case class Lama(name: String, age: Int, splitDistance: Int) extends Animal
derives Form
case class Otter(name: String, age: Int) extends Animal derives Form

case class Owner(name: String, pet: Animal) derives Form
case class Otter(name: String, age: Int) extends Animal

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

Sample(
"Sealed", {
Expand Down
4 changes: 2 additions & 2 deletions examples/client/src/main/scala/samples/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import dev.cheleb.scalamigen.*
import com.raquo.laminar.api.L.*

import com.raquo.airstream.state.Var
import dev.cheleb.scalamigen.FormDerive.autoDerived

val tree = {

enum Tree[+T] derives Form:
enum Tree[+T]:
case Empty extends Tree[Nothing]
case Node(value: T, left: Tree[T], right: Tree[T])
object Tree:
Expand Down
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/Validation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ val validation = {
optionalInt: Option[Int],
doubleGreaterThanEight: Double :| GreaterEqual[8.0],
optionalDoublePositive: Option[Double :| Positive]
) derives Form
)

given IronTypeValidator[Double, GreaterEqual[8.0]] =
_.toDoubleOption match
Expand Down
6 changes: 3 additions & 3 deletions examples/client/src/main/scala/samples/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ object CurrencyCode:
opaque type Password = String
object Password:
def apply(password: String): Password = password
given Form[Password] = Form.secretForm(apply)
given Form[Password] = secretForm(apply)

case class Cat(name: String, weight: Int, kind: Boolean = true) derives Form
case class Dog(name: String, weight: Int) derives Form
case class Cat(name: String, weight: Int, kind: Boolean = true)
case class Dog(name: String, weight: Int)

given Defaultable[Cat] with
def default = Cat("", 0)
Expand Down
Loading

0 comments on commit 6603d78

Please sign in to comment.