PoisonedYouth
06/08/2023, 8:13 AMCLOVIS
06/09/2023, 3:08 PMCLOVIS
06/09/2023, 3:14 PMbind
instead of `flatMap`in a lot of cases to simplify the code. For example, these two snippets do exactly the same thing:
fun addNewUser(user: UserDto): Either<Failure, User> {
return user.toUser().flatMap {
userRepository.save(it)
}
}
fun addNewUser(user: UserDto) = either {
userRepository.save(
user.toUser().bind()
)
}
In this small example, the difference is slight, but it allows to avoid the nested lambda, thus reducing indentation. It also composes much better.PoisonedYouth
06/09/2023, 3:17 PMPoisonedYouth
06/09/2023, 5:05 PMoverride fun addNewUser(user: UserDto): Either<Failure, User> = either {
userRepository.save(user.toUser().bind()).bind()
}
PoisonedYouth
06/09/2023, 5:46 PMarrow-exact
library gives me the posibility to create value classes that are returning an Exact
result type depending of valid/invalid input. Out of this I can compose more complex objects. This solves the problem of validation depends on single properites.
But for me the problem remains that I maybe have validations which include multiple properties. So for this I still need a workaround in case I want to use a data class. In this case with Exact
I also need to use an object type containing multiple properties as raw input.
Something like:
data class TopicDto(
val id: Identity,
val name: String,
val category: String
)
data class Topic(
val id: Identity,
val name: String,
val category: String,
) {
companion object : Exact<TopicDto, Topic> {
override fun Raise<ExactError>.spec(raw: TopicDto): Topic {
ensure(raw.name.isNotEmpty())
ensure(raw.category.length > 5)
return Topic(
id = raw.id,
name = raw.name,
category = raw.category
)
}
}
}
CLOVIS
06/09/2023, 6:13 PMPoisonedYouth
06/09/2023, 6:41 PMCLOVIS
06/09/2023, 6:43 PMPoisonedYouth
06/09/2023, 6:45 PM