Skip to content

Commit

Permalink
Added improved boolean syntaxx
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Dec 30, 2023
1 parent 512eb78 commit 3833b20
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.Parser
import com.sksamuel.tribune.core.flatMap
import com.sksamuel.tribune.core.map

/**
* Transforms a [Parser] that produces Strings by converting the string to a Boolean using [toBoolean].
* Transforms a String producing [Parser] into a Boolean producing Parser,
* by converting the String to a Boolean using the library function [toBoolean].
*/
fun <I, E> Parser<I, String, E>.boolean(ifError: (String) -> E): Parser<I, Boolean, E> =
fun <I, E> Parser<I, String, E>.toBoolean(): Parser<I, Boolean, E> =
flatMap {
val b = it.toBoolean()
b.right()
}

/**
* Transforms a [Parser] that produces Strings by converting the string to a Boolean using [toBooleanStrict].
* Transforms a String producing [Parser] into a Boolean producing Parser,
* by converting the String to a Boolean using the library function [toBooleanStrict].
*/
fun <I, E> Parser<I, String, E>.booleanStrict(ifError: (String) -> E): Parser<I, Boolean, E> =
flatMap {
val b = it.toBooleanStrict()
b.right()
fun <I, E> Parser<I, String, E>.toBooleanStrict(ifError: (String) -> E): Parser<I, Boolean, E> =
flatMap { input ->
runCatching { input.toBooleanStrict() }.fold({ it.right() }, { ifError(input).leftNel() })
}

/**
* Transforms a [Parser] that produces Strings by converting the string to a Boolean using [toBooleanStrictOrNull].
* Transforms a nullable String producing [Parser] into a nullable Boolean producing Parser,
* by converting the String to a Boolean using the library function [toBooleanStrictOrNull].
*/
fun <I, E> Parser<I, String, E>.booleanStrictOrNull(ifError: (String) -> E): Parser<I, Boolean?, E> =
flatMap {
val b = it.toBooleanStrictOrNull()
b?.right() ?: ifError(it).leftNel()
}
fun <I, E> Parser<I, String, E>.toBooleanStrictOrNull(): Parser<I, Boolean?, E> =
map { it.toBooleanStrictOrNull() }
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.sksamuel.tribune.core
import arrow.core.EitherNel
import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.booleans.boolean
import com.sksamuel.tribune.core.floats.float
import com.sksamuel.tribune.core.ints.int
import com.sksamuel.tribune.core.longs.long
Expand Down Expand Up @@ -47,13 +46,6 @@ class ValidatedTest : FunSpec() {
val result: EitherNel<Nothing, Foo> = p.parse("foo")
}

test("parser should support booleans") {
val p = Parser<String>().boolean { "not a boolean" }
p.parse("foo").leftOrNull() shouldBe listOf("not a boolean")
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
}

test("parser should support longs") {
val p = Parser<String>().long { "not a long" }
p.parse("foo").leftOrNull() shouldBe listOf("not a long")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sksamuel.tribune.core.booleans

import com.sksamuel.tribune.core.Parser
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class BooleanTest : FunSpec() {
init {
test("parser should support String -> Boolean") {
val p = Parser<String>().toBoolean()
p.parse("foo").getOrNull() shouldBe false
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
}

test("parser should support String -> Boolean using strict mode") {
val p = Parser<String>().toBooleanStrict { "not a boolean" }
p.parse("foo").leftOrNull() shouldBe listOf("not a boolean")
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
}

test("parser should support String -> Boolean? using strict mode or null") {
val p = Parser<String>().toBooleanStrictOrNull()
p.parse("foo").getOrNull() shouldBe null
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
}
}
}

0 comments on commit 3833b20

Please sign in to comment.