Skip to content

Commit

Permalink
Refactor import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
cheleb committed Sep 27, 2024
1 parent 604b3ff commit 96c6a84
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 316 deletions.
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/HelloWorld.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ case class Sample(name: String, component: HtmlElement)

object App extends App {

val sample = Var(samples.sealedClasses.component)
val sample = Var(samples.either.component)

private def item(name: String) = SideNavigation.item(
_.text := name,
Expand Down
13 changes: 10 additions & 3 deletions examples/client/src/main/scala/samples/EitherSample.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

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

Expand All @@ -10,10 +10,17 @@ val either = {

case class EitherSample(
either: Either[Cat, Dog],
primitiveEither: Either[Cat, String],
optionalInt: Option[Int]
)
) derives Form

val eitherVar = Var(EitherSample(Left(Cat("Scala le chat", 6)), Some(1)))
val eitherVar = Var(
EitherSample(
Left(Cat("Scala le chat", 6)),
Right("Forty two"),
Some(1)
)
)
Sample(
"Either", {

Expand Down
8 changes: 4 additions & 4 deletions examples/client/src/main/scala/samples/EnumSample.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

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

Expand All @@ -9,20 +9,20 @@ import com.raquo.airstream.state.Var
import com.raquo.laminar.api.L

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

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

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
10 changes: 6 additions & 4 deletions examples/client/src/main/scala/samples/ListElement.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

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

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

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

given (Person2 => Int) = _.id

val listPersonVar = Var(
ListElement(List(1, 2, 3).map(id => Person2(id, "Vlad", 20)))
)

given (Person2 => Int) = _.id
Sample(
"List",
div(
Expand All @@ -27,4 +28,5 @@ val list = {
listPersonVar.asForm
)
)

}
8 changes: 4 additions & 4 deletions examples/client/src/main/scala/samples/Persons.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

import com.raquo.laminar.api.L.*
import magnolia1.*
Expand All @@ -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)
case class House(capacity: Int) derives Form

// Provide default for optional
given Defaultable[Pet] with
Expand Down
11 changes: 6 additions & 5 deletions examples/client/src/main/scala/samples/Sealed.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

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

import com.raquo.airstream.state.Var
import com.raquo.laminar.nodes.ReactiveHtmlElement

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

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

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

Sample(
"Sealed", {
Expand Down
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/SimpleSample.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

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

Expand Down
6 changes: 3 additions & 3 deletions examples/client/src/main/scala/samples/Tree.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package samples

import dev.cheleb.scalamigen.{*, given}
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]:
enum Tree[+T] derives Form:
case Empty extends Tree[Nothing]
case Node(value: T, left: Tree[T], right: Tree[T])
object Tree:
Expand Down
4 changes: 2 additions & 2 deletions examples/client/src/main/scala/samples/Validation.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package samples

import dev.cheleb.scalamigen.{*, given}
import dev.cheleb.scalamigen.*

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

Expand All @@ -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] = secretForm(apply)
given Form[Password] = Form.secretForm(apply)

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

given Defaultable[Cat] with
def default = Cat("", 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.cheleb.scalamigen

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

/** Typeclass for default values.
*
* This typeclass is used to provide default values for a given type. It is
Expand All @@ -19,3 +22,24 @@ trait Defaultable[A] {
*/
def label: String = default.getClass.getSimpleName()
}

object Defaultable {

/** Default value for Int is 0.
*/
inline given Defaultable[Int] with
def default = 0

/** Default value for String is "".
*/
inline given Defaultable[String] with
def default = ""

/** Default value for [Iron type Double
* positive](https://iltotore.github.io/iron/io/github/iltotore/iron/constraint/numeric$.html#Positive-0)
* is 0.0.
*/
inline given Defaultable[IronType[Double, Positive]] with
def default = 1.0.refineUnsafe[Positive]

}
Loading

0 comments on commit 96c6a84

Please sign in to comment.