All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kernl.data.ext.Samples.kt Maven / Gradle / Ivy

Go to download

Kernl: A Kotlin Symbol Processing (KSP) library for automatic repository generation.

There is a newer version: 0.0.1-beta6
Show newest version
package io.github.mattshoe.shoebox.kernl.data.ext

import io.github.mattshoe.shoebox.kernl.data.DataResult

private fun fetchData(): DataResult {
    return DataResult.Success("")
}

private fun sampleValueOrNull() {
    val result = fetchData() // Example function that returns DataResult

    val value: String? = result.valueOrNull()
    println("Retrieved value: $value")

    // Expected Output:
    // If result is DataResult.Success -> "Retrieved value: "
    // If result is DataResult.Error or DataResult.Invalidated -> "Retrieved value: null"
}

private fun sampleUnwrap() {
    val result = fetchData() // Example function that returns DataResult

    try {
        val data = result.unwrap()
        println("Data: $data")
    } catch (e: Throwable) {
        println("Error occurred: ${e.message}")
    }

    // Expected Output:
    // If result is DataResult.Success -> "Data: "
    // If result is DataResult.Error -> "Error occurred: "
    // If result is DataResult.Invalidated -> "Error occurred: Attempted to unwrap an Invalidated data result."
}

private fun sampleUnwrapWithErrorHandling() {
    val result = fetchData() // Example function that returns DataResult

    val data: String? = result.unwrap { error ->
        println("Handled error: ${error.message}")
    }

    println("Unwrapped data: $data")

    // Expected Output:
    // If result is DataResult.Success -> "Unwrapped data: "
    // If result is DataResult.Error -> "Handled error: " and "Unwrapped data: null"
    // If result is DataResult.Invalidated -> "Handled error: Attempted to unwrap an Invalidated data result." and "Unwrapped data: null"
}

/**
 * Demonstrates the usage of the `orElse` extension function.
 * Retrieves data or provides a default value in case of an error or invalidation.
 */
fun sampleOrElse() {
    val result = fetchData() // Example function that returns DataResult

    val data: String = result.orElse { error ->
        "Default value due to error: ${error.message}"
    }

    println("Result data: $data")

    // Expected Output:
    // If result is DataResult.Success -> "Result data: "
    // If result is DataResult.Error -> "Result data: Default value due to error: "
    // If result is DataResult.Invalidated -> "Result data: Default value due to error: Attempted to unwrap an Invalidated data result."
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy