Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change error handling for Exact #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ the Arrow's `Raise` DSL to `ensure` the value is not blank.
```kotlin
import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ErrorMessage
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankString {
override fun Raise<ErrorMessage>.spec(raw: String): NotBlankString {
ensure(raw.isNotBlank())
return NotBlankString(raw)
}
Expand All @@ -29,7 +29,7 @@ value class NotBlankString private constructor(val value: String) {
```

We can then easily create values of `NotBlankString` `from` a `String`, which returns us a
`Either` with the `ExactError` or the `NotBlankString`. We can also use `fromOrNull` to get a
`Either` with the `ErrorMessage` or the `NotBlankString`. We can also use `fromOrNull` to get a
nullable value, or `fromOrThrow` to throw an `ExactException`.

**note:** Make sure to define your constructor as `private` to prevent creating invalid values.
Expand All @@ -45,7 +45,7 @@ The output of the above program is:

```text
Either.Right(NotBlankString(value=Hello))
Either.Left(ExactError(message=Failed condition.))
Either.Left(ErrorMessage(message=Failed condition.))
```

<!--- KNIT example-readme-01.kt -->
Expand All @@ -72,32 +72,26 @@ You can define a second type `NotBlankTrimmedString` that is a `NotBlankString`
trimmed. `ensureExact` allows us to compose `Exact` instances and easily
reuse the `NotBlankString` type.
<!--- INCLUDE
import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankString {
ensure(raw.isNotBlank())
return NotBlankString(raw)
}
}
companion object : Exact<String, NotBlankString> by Exact({
ensure(it.isNotBlank())
NotBlankString(it)
})
}
-->

```kotlin
@JvmInline
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensure(raw, NotBlankString)
return NotBlankTrimmedString(raw.trim())
}
}
companion object : Exact<String, NotBlankTrimmedString> by Exact({
ensure(it, NotBlankString)
NotBlankTrimmedString(it.trim())
})
}
```

Expand Down
4 changes: 2 additions & 2 deletions guide/src/commonMain/kotlin/examples/example-exact-01.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package arrow.exact.knit.example.exampleExact01

import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ErrorMessage
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankString {
override fun Raise<ErrorMessage>.spec(raw: String): NotBlankString {
ensure(raw.isNotBlank())
return NotBlankString(raw)
}
Expand Down
22 changes: 8 additions & 14 deletions guide/src/commonMain/kotlin/examples/example-exact-03.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
// This file was automatically generated from Exact.kt by Knit tool. Do not edit.
package arrow.exact.knit.example.exampleExact03

import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import kotlin.jvm.JvmInline

class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankString {
ensure(raw.isNotBlank())
return NotBlankString(raw)
}
}
companion object : Exact<String, NotBlankString> by Exact({
ensure(it.isNotBlank())
NotBlankString(it)
})
}

@JvmInline
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensure(raw, NotBlankString)
return NotBlankTrimmedString(raw.trim())
}
}
companion object : Exact<String, NotBlankTrimmedString> by Exact({
ensure(it, NotBlankString)
NotBlankTrimmedString(it.trim())
})
}
32 changes: 13 additions & 19 deletions guide/src/commonMain/kotlin/examples/example-exact-04.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
// This file was automatically generated from Exact.kt by Knit tool. Do not edit.
package arrow.exact.knit.example.exampleExact04

import arrow.core.raise.Raise
import arrow.core.raise.ensure
import arrow.exact.Exact
import arrow.exact.ExactEither
import arrow.exact.ExactError
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensure(raw.isNotBlank())
return NotBlankTrimmedString(raw.trim())
}
}
companion object : Exact<String, NotBlankTrimmedString> by Exact({
ensure(it.isNotBlank())
NotBlankTrimmedString(it.trim())
})
}

sealed interface UsernameError {
Expand All @@ -26,15 +22,13 @@ sealed interface UsernameError {

@JvmInline
value class Username private constructor(val value: String) {
companion object : ExactEither<UsernameError, String, Username> {
override fun Raise<UsernameError>.spec(raw: String): Username {
val username =
ensure(raw, NotBlankTrimmedString) {
UsernameError.Invalid
}.value
ensure(username.length < 100) { UsernameError.Invalid }
ensure(username !in listOf("offensive")) { UsernameError.Offensive(username) }
return Username(username)
}
}
companion object : ExactEither<UsernameError, String, Username> by ExactEither({
val username =
ensure(it, NotBlankTrimmedString) {
UsernameError.Invalid
}.value
ensure(username.length < 100) { UsernameError.Invalid }
ensure(username !in listOf("offensive")) { UsernameError.Offensive(username) }
Username(username)
})
}
4 changes: 2 additions & 2 deletions guide/src/commonMain/kotlin/examples/example-readme-01.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package arrow.exact.knit.example.exampleReadme01

import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ErrorMessage
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankString {
override fun Raise<ErrorMessage>.spec(raw: String): NotBlankString {
ensure(raw.isNotBlank())
return NotBlankString(raw)
}
Expand Down
22 changes: 8 additions & 14 deletions guide/src/commonMain/kotlin/examples/example-readme-03.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
// This file was automatically generated from README.md by Knit tool. Do not edit.
package arrow.exact.knit.example.exampleReadme03

import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankString private constructor(val value: String) {
companion object : Exact<String, NotBlankString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankString {
ensure(raw.isNotBlank())
return NotBlankString(raw)
}
}
companion object : Exact<String, NotBlankString> by Exact({
ensure(it.isNotBlank())
NotBlankString(it)
})
}

@JvmInline
value class NotBlankTrimmedString private constructor(val value: String) {
companion object : Exact<String, NotBlankTrimmedString> {
override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString {
ensure(raw, NotBlankString)
return NotBlankTrimmedString(raw.trim())
}
}
companion object : Exact<String, NotBlankTrimmedString> by Exact({
ensure(it, NotBlankString)
NotBlankTrimmedString(it.trim())
})
}
2 changes: 1 addition & 1 deletion guide/src/jvmTest/kotlin/examples/spec/ExactExampleSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ExactExampleSpec : StringSpec({
captureOutput("ExampleExact01") { arrow.exact.knit.example.exampleExact01.example() }
.verifyOutputLines(
"Either.Right(NotBlankString(value=Hello))",
"Either.Left(ExactError(message=Failed condition.))"
"Either.Left(ErrorMessage(message=Failed condition.))"
)
}

Expand Down
2 changes: 1 addition & 1 deletion guide/src/jvmTest/kotlin/examples/spec/ReadMeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ReadMeSpec : StringSpec({
captureOutput("ExampleReadme01") { arrow.exact.knit.example.exampleReadme01.example() }
.verifyOutputLines(
"Either.Right(NotBlankString(value=Hello))",
"Either.Left(ExactError(message=Failed condition.))"
"Either.Left(ErrorMessage(message=Failed condition.))"
)
}

Expand Down
Loading