DecodingResult

value class DecodingResult<out T>

A discriminated union that encapsulates a successful outcome with a value of type T or a failure with an arbitrary Throwable exception. A schema mismatch is a special type of failure that may be partially recoverable.

Since

0.4.0

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Returns true if this instance represents a failed outcome. In this case isSuccess returns false.

Link copied to clipboard

Returns true if this instance represents a schema mismatch. In this case isSuccess returns false.

Link copied to clipboard

Returns true if this instance represents a successful outcome. In this case isFailure returns false.

Functions

Link copied to clipboard

Returns the encapsulated Throwable exception if this instance represents failure or null if it is success.

Link copied to clipboard
fun <R, T> DecodingResult<T>.fold(onSuccess: (value: T) -> R, onFailure: (exception: Throwable) -> R): R

Returns the result of onSuccess for the encapsulated value if this instance represents success or the result of onFailure for the encapsulated Throwable if this instance represents failure.

fun <R, T> DecodingResult<T>.fold(onSuccess: (value: T) -> R, onSchemaMismatch: (JsonElement, exception: Throwable) -> R, onFailure: (exception: Throwable) -> R): R

Returns the result of onSuccess for the encapsulated value if this instance represents success, the result of onSchemaMismatch for the encapsulated JsonElement and Throwable if this instance represents a schema mismatch, or the result of onFailure for the encapsulated Throwable if this instance represents any other failure.

Link copied to clipboard
inline fun <R, T : R> DecodingResult<T>.getOrDefault(defaultValue: R): R

Returns the encapsulated value if this instance represents success or the defaultValue if it is failure.

Link copied to clipboard
inline fun <R, T : R> DecodingResult<T>.getOrElse(onFailure: (exception: Throwable) -> R): R

Returns the encapsulated value if this instance represents success or the result of onFailure for the encapsulated Throwable exception if it is failure.

inline fun <R, T : R> DecodingResult<T>.getOrElse(onSchemaMismatch: (JsonElement, exception: Throwable) -> R, onFailure: (exception: Throwable) -> R): R

Returns the encapsulated value if this instance represents success, the result of onSchemaMismatch for the encapsulated JsonElement and Throwable if it is schema mismatch or the result of onFailure for the encapsulated Throwable if it is failure.

Link copied to clipboard
inline fun getOrNull(): T?

Returns the encapsulated value if this instance represents success or null if it is failure.

Link copied to clipboard
inline fun <T> DecodingResult<T>.getOrThrow(): T

Returns the encapsulated value if this instance represents success or throws the encapsulated Throwable if it is failure.

Link copied to clipboard
inline fun <R, T> DecodingResult<T>.map(transform: (value: T) -> R): DecodingResult<R>

Returns the encapsulated result of the given transform function applied to the encapsulated value if this instance represents success or the original encapsulated Throwable if it is failure.

Link copied to clipboard
inline fun <R, T> DecodingResult<T>.mapCatching(transform: (value: T) -> R): DecodingResult<R>

Returns the encapsulated result of the given transform function applied to the encapsulated value if this instance represents success or the original encapsulated Throwable if it is failure.

Link copied to clipboard
inline fun <T> DecodingResult<T>.onFailure(action: (exception: Throwable) -> Unit): DecodingResult<T>

Performs the given action on the encapsulated Throwable exception if this instance represents failure. Returns the receiver DecodingResult.

Link copied to clipboard
inline fun <T> DecodingResult<T>.onSuccess(action: (value: T) -> Unit): DecodingResult<T>

Performs the given action on the encapsulated value if this instance represents success. Returns the receiver DecodingResult.

Link copied to clipboard
inline fun <R, T : R> DecodingResult<T>.recover(transform: (exception: Throwable) -> R): DecodingResult<R>

Returns the encapsulated result of the given transform function applied to the encapsulated Throwable exception if this instance represents failure or the original encapsulated value if it is success.

Link copied to clipboard
inline fun <R, T : R> DecodingResult<T>.recoverCatching(transform: (exception: Throwable) -> R): DecodingResult<R>

Returns the encapsulated result of the given transform function applied to the encapsulated Throwable exception if this instance represents failure or the original encapsulated value if it is success.

Link copied to clipboard
open override fun toString(): String

Returns a string representation of this result.

Link copied to clipboard
fun <E> DecodingResult<List<E>>.tryRecoverPartially(json: Json = Json, deserializer: DeserializationStrategy<E>): List<DecodingResult<E>>
fun <K, V> DecodingResult<Map<K, V>>.tryRecoverPartially(json: Json = Json, keyDeserializer: DeserializationStrategy<K>, deserializer: DeserializationStrategy<V>): Map<K, DecodingResult<V>>

Tries to recover the response partially if the result represents a schema mismatch. If the result represents another failure, an IllegalStateException is thrown. Successful results are simply transformed.