Skip to content

Commit

Permalink
Added if null variants
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Dec 30, 2023
1 parent 179a31d commit 7207484
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 8 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========================


### 2.0.0

* Updated to Arrow 1.2.x - Note: Arrow has deprecated `Validated`. In this release, `Parser` has been updated to use `EitherNel`.
* Moved some parsers to new packages.
* Removed all previously deprecated functions.
* Added variants of string parsers to work on nullable strings.

### 1.3.0

* Released support (and min version) for Kotlin 1.8.x
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sksamuel.tribune.core
package com.sksamuel.tribune.core.booleans

import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.Parser
import com.sksamuel.tribune.core.flatMap

fun <I, E> Parser<I, String, E>.boolean(ifError: (String) -> E): Parser<I, Boolean, E> =
flatMap {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.sksamuel.tribune.core
package com.sksamuel.tribune.core.doubles

import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.Parser
import com.sksamuel.tribune.core.filter
import com.sksamuel.tribune.core.flatMap
import com.sksamuel.tribune.core.map

/**
* Extends a [Parser] of output type string to parse that string into a double.
Expand Down Expand Up @@ -40,3 +44,6 @@ fun <I, E> Parser<I, Double, E>.inrange(
flatMap {
if (it in range) it.right() else ifError(it).leftNel()
}

fun <I, E> Parser<I, Double, E>.nullIf(fn: (Double) -> Boolean): Parser<I, Double?, E> =
this.map { if (fn(it)) null else it }
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sksamuel.tribune.core
package com.sksamuel.tribune.core.enums

import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.Parser
import com.sksamuel.tribune.core.flatMap

inline fun <I, E, reified ENUM : Enum<ENUM>> Parser<I, String, E>.enum(crossinline ifError: (String) -> E): Parser<I, ENUM, E> {
return flatMap { symbol ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.sksamuel.tribune.core
package com.sksamuel.tribune.core.floats

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

/**
* Extends a [Parser] of output type string to parse that string into a double.
Expand All @@ -16,3 +19,10 @@ fun <I, E> Parser<I, String, E>.float(ifError: (String) -> E): Parser<I, Float,
val f = it.toFloatOrNull()
f?.right() ?: ifError(it).leftNel()
}

fun <I, E> Parser<I, Float, E>.nullIf(fn: (Float) -> Boolean): Parser<I, Float?, E> =
this.map { if (fn(it)) null else it }

@JvmName("nullIfNullable")
fun <I, E> Parser<I, Float?, E>.nullIf(fn: (Float) -> Boolean): Parser<I, Float?, E> =
this.map { if (it == null || fn(it)) null else it }
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.sksamuel.tribune.core
package com.sksamuel.tribune.core.ints

import arrow.core.invalidNel
import arrow.core.Either
import arrow.core.leftNel
import arrow.core.right
import arrow.core.validNel
import com.sksamuel.tribune.core.Parser
import com.sksamuel.tribune.core.filter
import com.sksamuel.tribune.core.flatMap
import com.sksamuel.tribune.core.map

/**
* Chains a [Parser] to convert String -> Int.
Expand Down Expand Up @@ -42,10 +45,24 @@ fun <I, E> Parser<I, Int, E>.min(min: Int, ifError: (Int) -> E): Parser<I, Int,
if (it >= min) it.right() else ifError(it).leftNel()
}

fun <I, E> Parser<I, Int?, E>.min(min: Int, ifError: (Int) -> E): Parser<I, Int?, E> =
flatMap {
if (it == null) Either.Right(null) else if (it >= min) it.right() else ifError(it).leftNel()
}

fun <I, E> Parser<I, Int, E>.max(min: Int, ifError: (Int) -> E): Parser<I, Int, E> =
flatMap {
if (it >= min) it.right() else ifError(it).leftNel()
}

fun <I, E> Parser<I, Int?, E>.max(min: Int, ifError: (Int) -> E): Parser<I, Int?, E> =
flatMap {
if (it == null) Either.Right(null) else if (it >= min) it.right() else ifError(it).leftNel()
}

fun <I, E> Parser<I, Int, E>.nullIf(fn: (Int) -> Boolean): Parser<I, Int?, E> =
this.map { if (fn(it)) null else it }

@JvmName("nullIfNullable")
fun <I, E> Parser<I, Long?, E>.nullIf(fn: (Long) -> Boolean): Parser<I, Long?, E> =
this.map { if (it == null || fn(it)) null else it }
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.sksamuel.tribune.core
package com.sksamuel.tribune.core.longs

import arrow.core.Either
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

/**
* Extends a [Parser] of output type string to parse that string into a long.
Expand Down Expand Up @@ -32,3 +36,21 @@ fun <I, E> Parser<I, Long, E>.max(min: Long, ifError: (Long) -> E): Parser<I, Lo
if (it >= min) it.right() else ifError(it).leftNel()
}

@JvmName("minOrNull")
fun <I, E> Parser<I, Long?, E>.min(min: Long, ifError: (Long) -> E): Parser<I, Long?, E> =
flatMap {
if (it == null) Either.Right(null) else if (it >= min) it.right() else ifError(it).leftNel()
}

@JvmName("maxOrNull")
fun <I, E> Parser<I, Long?, E>.max(min: Long, ifError: (Long) -> E): Parser<I, Long?, E> =
flatMap {
if (it == null) Either.Right(null) else if (it >= min) it.right() else ifError(it).leftNel()
}

fun <I, E> Parser<I, Long, E>.nullIf(fn: (Long) -> Boolean): Parser<I, Long?, E> =
this.map { if (fn(it)) null else it }

@JvmName("nullIfNullable")
fun <I, E> Parser<I, Long?, E>.nullIf(fn: (Long) -> Boolean): Parser<I, Long?, E> =
this.map { if (it == null || fn(it)) null else it }
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ fun <I, E> Parser<I, String?, E>.nullOrNotBlank(ifBlank: () -> E): Parser<I, Str
if (it == null) null.right() else if (it.isBlank()) ifBlank().leftNel() else it.right()
}
}

fun <I, E> Parser<I, String, E>.nullIf(fn: (String) -> Boolean): Parser<I, String?, E> =
this.map { if (fn(it)) null else it }

@JvmName("nullIfNullable")
fun <I, E> Parser<I, String?, E>.nullIf(fn: (String) -> Boolean): Parser<I, String?, E> =
this.map { if (it == null || fn(it)) null else it }
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package com.sksamuel.tribune.core

import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.doubles.double
import com.sksamuel.tribune.core.doubles.inrange
import com.sksamuel.tribune.core.doubles.negative
import com.sksamuel.tribune.core.doubles.nonNegative
import com.sksamuel.tribune.core.doubles.positive
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ 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
import com.sksamuel.tribune.core.strings.minlen
import com.sksamuel.tribune.core.strings.notBlank
import com.sksamuel.tribune.core.strings.notNullOrBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sksamuel.tribune.core

import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.enums.enum
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package com.sksamuel.tribune.core

import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.ints.inrange
import com.sksamuel.tribune.core.ints.int
import com.sksamuel.tribune.core.ints.negative
import com.sksamuel.tribune.core.ints.nonNegative
import com.sksamuel.tribune.core.ints.positive
import com.sksamuel.tribune.core.longs.long
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

Expand Down

0 comments on commit 7207484

Please sign in to comment.