Skip to content

Instantly share code, notes, and snippets.

@hadilq
hadilq / DoWorkCompleteMoreComplex.kt
Created January 10, 2021 23:12
Complete doWork method for more complex scenario
fun doWork(): Result<Unit> {
return input() into
::parse elseIf
{
return Failure("Cannot parse email")
} into
::validate elseIf
{
InvalidEmailAddress { message -> return Failure(message) }
EmptySubject(::fixEmptySubject)
@hadilq
hadilq / DoWorkCompleteSimple.kt
Created January 10, 2021 23:10
Complete doWork method
fun doWork(): Result<Unit> {
return input() into
::parse elseIf
{
return Failure("Cannot parse email")
} into
::validate elseIf
{
InvalidEmailAddress { message -> return Failure(message) }
EmptySubject { message -> return Failure(message) }
@hadilq
hadilq / SendDSL.kt
Created January 10, 2021 23:08
Send DSL
send() elseIf {
IOFailure { error: IOException -> /* handle failure */ }
Timeout { error: TimeoutException -> /* handle failure */ }
UnAuthorized { error: UnAuthorizedException ->
/* handle failure */
}
}
@hadilq
hadilq / ValidationResultElseIf.kt
Created January 10, 2021 23:07
Validate DSL extension function
public inline infix fun ValidationResult.elseIf(build: ValidationResultElseIfBuilder.() -> Unit):
Success {
if (this is Success) {
return this
} else {
val builder = ValidationResultElseIfBuilder(this)
builder.build()
return builder.result
}
}
public class ValidationResultElseIfBuilder(
public val parent: ValidationResult
) {
public lateinit var result: Success
public inline fun InvalidEmailAddress(block: (errorMessage: String) -> Success): Unit {
if(parent is InvalidEmailAddress) {
result = block(parent.errorMessage)
}
}
@hadilq
hadilq / DoWorkValidate.kt
Created January 10, 2021 23:05
Validate in doWork method
doWork() : Result<Email> {
...
validate() elseIf {
InvalidEmailAddress { errorMessage: String ->
return Failure(errorMessage)
}
EmptySubject { errorMessage: String -> /* handle failure */ }
EmptyBody { errorMessage: String -> /* handle failure */ }
}
...
@hadilq
hadilq / validateDSL.kt
Created January 10, 2021 23:02
validate DSL
validate() elseIf {
InvalidEmailAddress { errorMessage: String ->
/* handle failure */
}
EmptySubject { errorMessage: String -> /* handle failure */ }
EmptyBody { errorMessage: String -> /* handle failure */ }
}
@hadilq
hadilq / SendResult.kt
Created January 10, 2021 23:01
The result of send method
sealed class SendResult
data class SendSuccess(val email: Email): SendResult()
data class IOFailure(val error: IOException): SendResult()
data class Timeout(val error: TimeoutException): SendResult()
data class UnAuthorized(val error: UnAuthorizedException): SendResult()
sealed class Result<T>
data class Success<T>(val value: T): Result<T>()
data class Failure<T>(val errorMessage: String): Result<T>()
@hadilq
hadilq / ValidationResult.kt
Created January 10, 2021 22:58
The result of validate method
sealed class ValidationResult
data class ValidationSuccess(val email: Email): ValidationResult()
data class InvalidEmailAddress(val errorMessage: String): ValidationResult()
data class EmptySubject(val errorMessage: String): ValidationResult()
data class EmptyBody(val errorMessage: String): ValidationResult()