Skip to content

Commit

Permalink
misc: Use fewerBraces (#234)
Browse files Browse the repository at this point in the history
Closes #210
  • Loading branch information
zaxxel authored Apr 19, 2024
1 parent 553876a commit 074122f
Show file tree
Hide file tree
Showing 53 changed files with 367 additions and 477 deletions.
11 changes: 8 additions & 3 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.6.1"
version = "3.8.1"
runner.dialect = "scala3"

maxColumn = 150
Expand All @@ -7,7 +7,12 @@ newlines.source = keep

rewrite.scala3 {
convertToNewSyntax = yes
removeOptionalBraces = yes
removeOptionalBraces {
"enabled": true,
"fewerBracesMinSpan": 2,
"fewerBracesMaxSpan": 600,
"oldSyntaxToo": yes
}
}

align {
Expand All @@ -25,4 +30,4 @@ docstrings {
style = Asterisk
oneline = keep
wrap = no
}
}
3 changes: 1 addition & 2 deletions borer/src/io/github/iltotore/iron/borer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ object borer:
encoder.asInstanceOf[Encoder[A :| B]]

inline given [A, B](using inline decoder: Decoder[A], inline constraint: Constraint[A, B]): Decoder[A :| B] =
Decoder { r =>
Decoder: r =>
decoder.read(r).refineEither match
case Left(msg) => r.validationFailure(msg)
case Right(x) => x
}

inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Encoder[m.IronType]): Encoder[T] =
ev.asInstanceOf[Encoder[T]]
Expand Down
16 changes: 6 additions & 10 deletions borer/test/src/io/github/iltotore/iron/BorerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ object BorerSuite extends TestSuite:

val tests: Tests = Tests {

test("opaque alias encoding") {
test("opaque alias encoding"):
Json.encode(Temperature(15.0)).toUtf8String ==> "15.0"
}

test("opaque alias decoding") {
test("opaque alias decoding"):
Json.decode("15.0".getBytes).to[Temperature].valueEither ==> Right(Temperature(15.0))
Json.decode("-15.0".getBytes).to[Temperature].valueEither.left.map(_.getMessage) ==>
Left("Should be strictly positive (input position 0)")
}

test("transparent alias encoding") {
Json.encode(15.0:Moisture).toUtf8String ==> "15.0"
}
test("transparent alias encoding"):
Json.encode(15.0: Moisture).toUtf8String ==> "15.0"

test("transparent alias decoding") {
Json.decode("15.0".getBytes).to[Moisture].valueEither ==> Right(15.0:Moisture)
test("transparent alias decoding"):
Json.decode("15.0".getBytes).to[Moisture].valueEither ==> Right(15.0: Moisture)
Json.decode("-15.0".getBytes).to[Moisture].valueEither.left.map(_.getMessage) ==>
Left("Should be strictly positive (input position 0)")
}
}
4 changes: 2 additions & 2 deletions cats/src/io/github/iltotore/iron/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ object cats extends IronCatsInstances:
* Represent all Cats' typeclass instances for Iron.
*/
private trait IronCatsInstances extends IronCatsLowPriority, RefinedTypeOpsCats:

given [F[_]](using functor: Functor[F]): MapLogic[F] with

override def map[A, B](wrapper: F[A], f: A => B): F[B] = functor.map(wrapper)(f)

// The `NotGiven` implicit parameter is mandatory to avoid ambiguous implicit error when both Eq[A] and Hash[A]/PartialOrder[A] exist
Expand Down
42 changes: 14 additions & 28 deletions cats/test/src/io/github/iltotore/iron/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ object CatsSuite extends TestSuite:

val tests: Tests = Tests {

test("Cats instances are resolved for String iron types") {
test("Cats instances are resolved for String iron types"):
Eq[String :| NameR]
Hash[String :| NameR]
Order[String :| NameR]
PartialOrder[String :| NameR]
Show[String :| NameR]
}

test("Cats instances are resolved for new types") {
test("Cats instances are resolved for new types"):
Eq[Temperature]
Hash[Temperature]
Order[Temperature]
Expand All @@ -53,76 +52,66 @@ object CatsSuite extends TestSuite:
Hash[Moisture]
Order[Moisture]
Show[Moisture]
}

test("Cats instances are resolved for Int iron types") {
test("Cats instances are resolved for Int iron types"):
Eq[Int :| AgeR]
Hash[Int :| AgeR]
UpperBounded[Int :| AgeR]
Order[Int :| AgeR]
PartialOrder[Int :| AgeR]
Show[Int :| AgeR]
LowerBounded[Int :| AgeR]
}

test("Cats instances are resolved for a case class with iron types") {
test("Cats instances are resolved for a case class with iron types"):
Eq[Person]
Order[Person]
Show[Person]
}

test("alley") {
test("commutativeMonoid") {
test("int") {
test("int"):
test("pos") - assert(CommutativeMonoid[Int :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Int :| Negative].combine(-1, -5) == -6)
}

test("long") {
test("long"):
test("pos") - assert(CommutativeMonoid[Long :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Long :| Negative].combine(-1, -5) == -6)
}

test("float") {
test("float"):
test("pos") - assert(CommutativeMonoid[Float :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Float :| Negative].combine(-1, -5) == -6)
}

test("double") {
test("double"):
test("pos") - assert(CommutativeMonoid[Double :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Double :| Negative].combine(-1, -5) == -6)
}
}
}

test("eitherNec") {
test("eitherNec"):
import io.github.iltotore.iron.cats.*

val eitherNecWithFailingPredicate = Temperature.eitherNec(-5.0)
assert(eitherNecWithFailingPredicate == Left(NonEmptyChain.one("Should be strictly positive")), "'eitherNec' returns left if predicate fails")
val eitherNecWithSucceedingPredicate = Temperature.eitherNec(100)
assert(eitherNecWithSucceedingPredicate == Right(Temperature(100)), "right should contain result of 'apply'")
}

test("eitherNel") {
test("eitherNel"):
import io.github.iltotore.iron.cats.*

val eitherNelWithFailingPredicate = Temperature.eitherNel(-5.0)
assert(eitherNelWithFailingPredicate == Left(NonEmptyList.one("Should be strictly positive")), "'eitherNel' returns left if predicate fails")
val eitherNelWithSucceedingPredicate = Temperature.eitherNel(100)
assert(eitherNelWithSucceedingPredicate == Right(Temperature(100)), "right should contain result of 'apply'")
}

test("validated") {
test("validated"):
import io.github.iltotore.iron.cats.*

val validatedWithFailingPredicate = Temperature.validated(-5.0)
assert(validatedWithFailingPredicate == Invalid("Should be strictly positive"), "'eitherNec' returns left if predicate fails")
val validatedWithSucceedingPredicate = Temperature.validated(100)
assert(validatedWithSucceedingPredicate == Valid(Temperature(100)), "right should contain result of 'apply'")
}

test("validatedNec") {
test("validatedNec"):
import io.github.iltotore.iron.cats.*

val validatedNecWithFailingPredicate = Temperature.validatedNec(-5.0)
Expand All @@ -132,9 +121,8 @@ object CatsSuite extends TestSuite:
)
val validatedNecWithSucceedingPredicate = Temperature.validatedNec(100)
assert(validatedNecWithSucceedingPredicate == Valid(Temperature(100)), "valid should contain result of 'apply'")
}

test("validatedNel") {
test("validatedNel"):
import io.github.iltotore.iron.cats.*

val validatedNelWithFailingPredicate = Temperature.validatedNel(-5.0)
Expand All @@ -144,10 +132,8 @@ object CatsSuite extends TestSuite:
)
val validatedNelWithSucceedingPredicate = Temperature.validatedNel(100)
assert(validatedNelWithSucceedingPredicate == Valid(Temperature(100)), "valid should contain result of 'apply'")
}

test("refineAll") {
test("refineAll"):
test - assert(Temperature.optionAll(NonEmptyList.of(1, 2, -3)).isEmpty)
test - assert(Temperature.optionAll(NonEmptyList.of(1, 2, 3)).contains(NonEmptyList.of(Temperature(1), Temperature(2), Temperature(3))))
}
}
11 changes: 5 additions & 6 deletions circe/src/io/github/iltotore/iron/circe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ object circe:
*/
inline given [A, B](using inline encoder: Encoder[A]): Encoder[A :| B] = encoder.asInstanceOf[Encoder[A :| B]]

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: Decoder[mirror.IronType]): Decoder[T] =
inline given [T](using mirror: RefinedTypeOps.Mirror[T], ev: Decoder[mirror.IronType]): Decoder[T] =
ev.asInstanceOf[Decoder[T]]

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: Encoder[mirror.IronType]): Encoder[T] =
inline given [T](using mirror: RefinedTypeOps.Mirror[T], ev: Encoder[mirror.IronType]): Encoder[T] =
ev.asInstanceOf[Encoder[T]]

/**
* A [[KeyDecoder]] for refined types. Decodes to the underlying type then checks the constraint.
*
Expand All @@ -46,9 +46,8 @@ object circe:
*/
inline given [A, B](using inline encoder: KeyEncoder[A]): KeyEncoder[A :| B] = encoder.asInstanceOf[KeyEncoder[A :| B]]

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: KeyDecoder[mirror.IronType]): KeyDecoder[T] =
inline given [T](using mirror: RefinedTypeOps.Mirror[T], ev: KeyDecoder[mirror.IronType]): KeyDecoder[T] =
ev.asInstanceOf[KeyDecoder[T]]

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: KeyEncoder[mirror.IronType]): KeyEncoder[T] =
inline given [T](using mirror: RefinedTypeOps.Mirror[T], ev: KeyEncoder[mirror.IronType]): KeyEncoder[T] =
ev.asInstanceOf[KeyEncoder[T]]

12 changes: 4 additions & 8 deletions ciris/test/src/io/github/iltotore/iron/CirisSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import io.github.iltotore.iron.constraint.numeric.Positive
import utest.*

object CirisSuite extends TestSuite:
val tests: Tests = Tests {
val tests: Tests = Tests:

test("decoder") {
test("ironType") {
test("decoder"):
test("ironType"):
test("success") - assert(summon[ConfigDecoder[String, Int :| Positive]].decode(None, "5") == Right(5))
test("failure") - assert(summon[ConfigDecoder[String, Int :| Positive]].decode(None, "-5").isLeft)
}

test("newType") {
test("newType"):
test("success") - assert(summon[ConfigDecoder[String, Temperature]].decode(None, "5") == Right(Temperature(5)))
test("failure") - assert(summon[ConfigDecoder[String, Temperature]].decode(None, "-5").isLeft)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package io.github.iltotore.iron
import io.github.iltotore.iron.constraint.numeric.Positive

opaque type Temperature = Int :| Positive
object Temperature extends RefinedTypeOps[Int, Positive, Temperature]
object Temperature extends RefinedTypeOps[Int, Positive, Temperature]
4 changes: 2 additions & 2 deletions decline/src/io/github/iltotore/iron/decline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ object decline:
def read(string: String): ValidatedNel[String, A :| B] =
argument.read(string) match
case Valid(a) => a.refineEither[B] match
case Left(value) => Validated.invalidNel(value)
case Right(value) => Validated.validNel(value)
case Left(value) => Validated.invalidNel(value)
case Right(value) => Validated.validNel(value)
case Invalid(e) => Validated.invalid(e)

def defaultMetavar: String = argument.defaultMetavar
Expand Down
16 changes: 6 additions & 10 deletions decline/test/src/io/github/iltotore/iron/DeclineSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import io.github.iltotore.iron.constraint.numeric.Positive
import utest.*

object DeclineSuite extends TestSuite:
val tests: Tests = Tests {
val tests: Tests = Tests:

test("Argument") {
test("ironType") {
test("success") - assert(summon[Argument[Int :| Positive]].read("5") == Valid(5))
test("Argument"):
test("ironType"):
test("success") - assert(summon[Argument[Int :| Positive]].read("5") == Valid(5))
test("failure") - assert(summon[Argument[Int :| Positive]].read("-5").isInvalid)
}

test("newType") {
test("success") - assert(summon[Argument[Temperature]].read("5") == Valid(5))
test("newType"):
test("success") - assert(summon[Argument[Temperature]].read("5") == Valid(5))
test("failure") - assert(summon[Argument[Temperature]].read("-5").isInvalid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package io.github.iltotore.iron
import io.github.iltotore.iron.constraint.numeric.Positive

opaque type Temperature = Int :| Positive
object Temperature extends RefinedTypeOps[Int, Positive, Temperature]
object Temperature extends RefinedTypeOps[Int, Positive, Temperature]
12 changes: 6 additions & 6 deletions doobie/src/io/github/iltotore/iron/doobie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object doobie:
* @tparam A the base type
* @tparam C the constraint type
*/
inline given[A, C] (using inline get: Get[A])(using Constraint[A, C], Show[A]): Get[A :| C] =
inline given [A, C](using inline get: Get[A])(using Constraint[A, C], Show[A]): Get[A :| C] =
get.temap[A :| C](_.refineEither)

/**
Expand All @@ -26,7 +26,7 @@ object doobie:
* @param ev the value getter of the underlying type
* @tparam T the new type
*/
inline given[T](using m: RefinedTypeOps.Mirror[T], ev: Get[m.IronType]): Get[T] =
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Get[m.IronType]): Get[T] =
ev.asInstanceOf[Get[T]]

/**
Expand All @@ -37,7 +37,7 @@ object doobie:
* @tparam A the base type
* @tparam C the constraint type
*/
inline given[A, C] (using inline put: Put[A])(using Constraint[A, C], Show[A]): Put[A :| C] =
inline given [A, C](using inline put: Put[A])(using Constraint[A, C], Show[A]): Put[A :| C] =
put.tcontramap(identity)

/**
Expand All @@ -47,7 +47,7 @@ object doobie:
* @param ev the value setter of the underlying type
* @tparam T the new type
*/
inline given[T](using m: RefinedTypeOps.Mirror[T], ev: Put[m.IronType]): Put[T] =
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Put[m.IronType]): Put[T] =
ev.asInstanceOf[Put[T]]

/**
Expand All @@ -58,7 +58,7 @@ object doobie:
* @tparam A the base type
* @tparam C the constraint type
*/
inline given[A, C] (using inline meta: Meta[A])(using Constraint[A, C], Show[A]): Meta[A :| C] =
inline given [A, C](using inline meta: Meta[A])(using Constraint[A, C], Show[A]): Meta[A :| C] =
meta.tiemap[A :| C](_.refineEither)(identity)

/**
Expand All @@ -68,5 +68,5 @@ object doobie:
* @param ev the value getter/setter of the underlying type
* @tparam T the new type
*/
inline given[T](using m: RefinedTypeOps.Mirror[T], ev: Meta[m.IronType]): Meta[T] =
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Meta[m.IronType]): Meta[T] =
ev.asInstanceOf[Meta[T]]
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ case class Account(
name: String :| Username,
password: String :| Password,
age: Int :| Age
) derives Codec
) derives Codec
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import io.github.iltotore.iron.*
assert(decoding == Right(account))

val decoding2 = Json.decode(invalidEncoding.getBytes).to[Account].valueEither
decoding2 match {
case Left(e: Borer.Error.ValidationFailure[_]) =>
decoding2 match
case Left(e: Borer.Error.ValidationFailure[?]) =>
// "Password must contain at least a letter, a digit and have a length between 6 and 20 (input position 26)"
println(e.getMessage)
case _ => throw new IllegalStateException
}
Loading

0 comments on commit 074122f

Please sign in to comment.