From 3833b20a861d15ae0ae77460a56213c927c8764a Mon Sep 17 00:00:00 2001 From: sksamuel Date: Sat, 30 Dec 2023 15:13:13 -0600 Subject: [PATCH] Added improved boolean syntaxx --- .../tribune/core/booleans/booleans.kt | 26 ++++++++-------- .../com/sksamuel/tribune/core/ParserTest.kt | 8 ----- .../tribune/core/booleans/BooleanTest.kt | 30 +++++++++++++++++++ 3 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 tribune-core/src/test/kotlin/com/sksamuel/tribune/core/booleans/BooleanTest.kt diff --git a/tribune-core/src/main/kotlin/com/sksamuel/tribune/core/booleans/booleans.kt b/tribune-core/src/main/kotlin/com/sksamuel/tribune/core/booleans/booleans.kt index 7d396a4..4e81032 100644 --- a/tribune-core/src/main/kotlin/com/sksamuel/tribune/core/booleans/booleans.kt +++ b/tribune-core/src/main/kotlin/com/sksamuel/tribune/core/booleans/booleans.kt @@ -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 Parser.boolean(ifError: (String) -> E): Parser = +fun Parser.toBoolean(): Parser = 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 Parser.booleanStrict(ifError: (String) -> E): Parser = - flatMap { - val b = it.toBooleanStrict() - b.right() +fun Parser.toBooleanStrict(ifError: (String) -> E): Parser = + 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 Parser.booleanStrictOrNull(ifError: (String) -> E): Parser = - flatMap { - val b = it.toBooleanStrictOrNull() - b?.right() ?: ifError(it).leftNel() - } +fun Parser.toBooleanStrictOrNull(): Parser = + map { it.toBooleanStrictOrNull() } diff --git a/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/ParserTest.kt b/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/ParserTest.kt index 369dc1a..4a431d7 100644 --- a/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/ParserTest.kt +++ b/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/ParserTest.kt @@ -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 @@ -47,13 +46,6 @@ class ValidatedTest : FunSpec() { val result: EitherNel = p.parse("foo") } - test("parser should support booleans") { - val p = Parser().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().long { "not a long" } p.parse("foo").leftOrNull() shouldBe listOf("not a long") diff --git a/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/booleans/BooleanTest.kt b/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/booleans/BooleanTest.kt new file mode 100644 index 0000000..bcc7570 --- /dev/null +++ b/tribune-core/src/test/kotlin/com/sksamuel/tribune/core/booleans/BooleanTest.kt @@ -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().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().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().toBooleanStrictOrNull() + p.parse("foo").getOrNull() shouldBe null + p.parse("true").getOrNull() shouldBe true + p.parse("false").getOrNull() shouldBe false + } + } +}