Skip to content

Commit

Permalink
fix: Code snippets in README and website (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore authored Jan 12, 2023
1 parent 1fc1087 commit 502cfbd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ To learn more about Iron, see the [microsite](https://iltotore.github.io/iron/do
## Example

```scala
def log(x: Double :| Greater[0.0]): Double =
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.numeric.*

def log(x: Double :| Positive): Double =
Math.log(x) //Used like a normal `Double`

log(1.0) //Automatically verified at compile time.
log(-1.0) //Error: -1.0 is not greater than 0.0!
log(-1.0) //Compile-time error: Should be strictly positive

val runtimeValue = ???
val runtimeValue: Double = ???
log(runtimeValue.refine) //Explicitly refine your external values at runtime.

runtimeValue.refineEither.map(log) //Use monadic style for functional validation
Expand All @@ -42,7 +45,6 @@ SBT:
libraryDependencies += "io.github.iltotore" %% "iron" % "version"
```


Mill:

```scala
Expand Down
5 changes: 3 additions & 2 deletions docs/_docs/modules/cats.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ The [User example](../reference/refinement.md) now looks like this:
```scala
import cats.data.EitherNec
import cats.syntax.all.*

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

case class User(name: String :| Alphanumeric, age: Int :| Greater[0])
case class User(name: String :| Alphanumeric, age: Int :| Positive)

def createUserAcc(name: String, age: Int): EitherNec[String, User] =
(
Expand All @@ -53,7 +54,7 @@ Or with custom messages:
```scala
type Username = Alphanumeric DescribedAs "Username should be alphanumeric"

type Age = Greater[0] DescribedAs "Age should be positive"
type Age = Positive DescribedAs "Age should be positive"

case class User(name: String :| Username, age: Int :| Age)

Expand Down
8 changes: 4 additions & 4 deletions docs/_docs/modules/circe.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ ivy"io.github.iltotore::iron-circe:version"
Given Encoder/Decoder for Iron enables using refined types with any Circe feature including automatic derivation:

```scala
import io.circe.*
import io.circe.parser.*

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

import io.circe.*
import io.circe.parser.*

type Username = Alphanumeric DescribedAs "Username should be alphanumeric"

type Age = Greater[0] DescribedAs "Age should be positive"
type Age = Positive DescribedAs "Age should be positive"

case class User(name: String :| Username, age: Int :| Age)

Expand Down
6 changes: 3 additions & 3 deletions docs/_docs/modules/zio-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ ivy"io.github.iltotore::iron-zio-json:version"
Given Encoder/Decoder for Iron enables using refined types for JSON serialization/deserialization:

```scala
import zio.json.*

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

import zio.json.*

case class User(name: String :| Alphanumeric, age: Int :| Greater[0])
case class User(name: String :| Alphanumeric, age: Int :| Positive)

given JsonCodec[User] = DeriveJsonCodec.gen

Expand Down
9 changes: 8 additions & 1 deletion docs/_docs/modules/zio.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ The [User example](../reference/refinement.md) now looks like this:


```scala
import zio.prelude.Validation

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


type Username = Alphanumeric DescribedAs "Username should be alphanumeric"

type Age = Greater[0] DescribedAs "Age should be positive"
type Age = Positive DescribedAs "Age should be positive"

case class User(name: String :| Username, age: Int :| Age)

Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Refined types solve both problems by ensuring that constraints are checked compi
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.numeric.*

case class User(age: Int :| Greater[0])
case class User(age: Int :| Positive)

User(1) //Compiles
User("1") //Does not compile
Expand Down

0 comments on commit 502cfbd

Please sign in to comment.