Created
March 13, 2022 09:30
-
-
Save raulraja/8ea9dbad4379245c242bd0717d63fc5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.coral.fp.control | |
import arrow.core.ValidatedNel | |
import arrow.core.invalidNel | |
import arrow.core.validNel | |
import arrow.core.zip | |
import kotlin.jvm.JvmInline | |
@JvmInline | |
public value class ProductId private constructor(public val value: Int) { | |
public companion object { | |
public operator fun invoke(value: Int): ValidatedNel<String, ProductId> = | |
if (value in 1..99999999) ProductId(value).validNel() | |
else "productId invalid".invalidNel() | |
} | |
} | |
@JvmInline | |
public value class ProductDescription private constructor(public val value: String) { | |
public companion object { | |
public operator fun invoke(value: String): ValidatedNel<String, ProductDescription> = | |
if (value != "" && value.length <= 200) ProductDescription(value).validNel() | |
else "description invalid".invalidNel() | |
} | |
} | |
public class Product private constructor( | |
public val id: ProductId, | |
public val description: ProductDescription | |
) { | |
public companion object { | |
public operator fun invoke( | |
validatedId: ValidatedNel<String, ProductId>, | |
validatedDescription: ValidatedNel<String, ProductDescription> | |
): ValidatedNel<String, Product> = | |
validatedId.zip(validatedDescription, ::Product) | |
} | |
} | |
public fun foo() { | |
val product: ValidatedNel<String, Product> = | |
Product(ProductId(1), ProductDescription("Pizza")) | |
product.fold({ errors -> println(errors) }, { p -> println(p) }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment