-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
iron-borer
module providing borer CBOR and JSON codecs for refi…
…ned types
- Loading branch information
Showing
4 changed files
with
537 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.iltotore.iron | ||
|
||
import io.bullet.borer.{Encoder, Decoder} | ||
|
||
/** | ||
* Automatic construction of borer [[Encoder]] and [[Decoder]] instances for refined types. | ||
*/ | ||
object borer: | ||
|
||
inline given [A, B](using inline encoder: Encoder[A]): Encoder[A :| B] = | ||
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.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]] | ||
|
||
inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Decoder[m.IronType]): Decoder[T] = | ||
ev.asInstanceOf[Decoder[T]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.iltotore.iron | ||
|
||
import io.github.iltotore.iron.constraint.all.* | ||
import io.github.iltotore.iron.borer.given | ||
import utest.* | ||
import io.bullet.borer.{Encoder, Json} | ||
|
||
object BorerSuite extends TestSuite: | ||
|
||
val tests: Tests = Tests { | ||
|
||
test("opaque alias encoding") { | ||
Json.encode(Temperature(15.0)).toUtf8String ==> "15.0" | ||
} | ||
|
||
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 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)") | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
borer/test/src/io/github/iltotore/iron/RefinedOpsTypes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.iltotore.iron | ||
|
||
import io.github.iltotore.iron.* | ||
import io.github.iltotore.iron.constraint.numeric.Positive | ||
|
||
/** | ||
* We declare test types here, in a separate file, in order to make the opaque aliases truly opaque. | ||
*/ | ||
opaque type Temperature = Double :| Positive | ||
object Temperature extends RefinedTypeOps[Double, Positive, Temperature] | ||
|
||
type Moisture = Double :| Positive | ||
object Moisture extends RefinedTypeOps.Transparent[Moisture] |
Oops, something went wrong.