Omit "this" prefix from method references
https://kotlinlang.org/docs/reference/whatsnew12.html#support-for--foo-as-a-shorthand-for-thisfoo
This commit is contained in:
parent
631f81d8ae
commit
687c4c9d7f
@ -83,7 +83,7 @@ having sufficient privileges, and the command executing the associated action.
|
|||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
tokenize(command.toLowerCase())
|
tokenize(command.toLowerCase())
|
||||||
.andThen(this::findCommand)
|
.andThen(::findCommand)
|
||||||
.andThen { cmd -> checkPrivileges(loggedInUser, cmd) }
|
.andThen { cmd -> checkPrivileges(loggedInUser, cmd) }
|
||||||
.andThen { execute(user = loggedInUser, command = cmd, timestamp = LocalDateTime.now()) }
|
.andThen { execute(user = loggedInUser, command = cmd, timestamp = LocalDateTime.now()) }
|
||||||
.mapBoth(
|
.mapBoth(
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
package com.github.michaelbull.result.example.service
|
package com.github.michaelbull.result.example.service
|
||||||
|
|
||||||
import com.github.michaelbull.result.*
|
import com.github.michaelbull.result.Err
|
||||||
|
import com.github.michaelbull.result.Ok
|
||||||
|
import com.github.michaelbull.result.Result
|
||||||
|
import com.github.michaelbull.result.andThen
|
||||||
import com.github.michaelbull.result.example.model.domain.Customer
|
import com.github.michaelbull.result.example.model.domain.Customer
|
||||||
import com.github.michaelbull.result.example.model.domain.CustomerId
|
import com.github.michaelbull.result.example.model.domain.CustomerId
|
||||||
import com.github.michaelbull.result.example.model.domain.DomainMessage
|
import com.github.michaelbull.result.example.model.domain.DomainMessage
|
||||||
import com.github.michaelbull.result.example.model.entity.CustomerEntity
|
import com.github.michaelbull.result.example.model.entity.CustomerEntity
|
||||||
|
import com.github.michaelbull.result.map
|
||||||
|
import com.github.michaelbull.result.mapBoth
|
||||||
|
import com.github.michaelbull.result.mapError
|
||||||
import java.sql.SQLTimeoutException
|
import java.sql.SQLTimeoutException
|
||||||
|
|
||||||
object CustomerService {
|
object CustomerService {
|
||||||
@ -12,7 +18,7 @@ object CustomerService {
|
|||||||
|
|
||||||
fun getAll(): Result<Collection<Customer>, DomainMessage> {
|
fun getAll(): Result<Collection<Customer>, DomainMessage> {
|
||||||
return Result.of(repository::findAll)
|
return Result.of(repository::findAll)
|
||||||
.mapError(this::exceptionToDomainMessage)
|
.mapError(::exceptionToDomainMessage)
|
||||||
.andThen { result: Collection<CustomerEntity> ->
|
.andThen { result: Collection<CustomerEntity> ->
|
||||||
Ok(result.map {
|
Ok(result.map {
|
||||||
val customer = Customer.from(it)
|
val customer = Customer.from(it)
|
||||||
@ -39,21 +45,24 @@ object CustomerService {
|
|||||||
private fun updateCustomer(entity: CustomerEntity, old: Customer, new: Customer) =
|
private fun updateCustomer(entity: CustomerEntity, old: Customer, new: Customer) =
|
||||||
Result.of { repository.update(entity) }
|
Result.of { repository.update(entity) }
|
||||||
.map { differenceBetween(old, new) }
|
.map { differenceBetween(old, new) }
|
||||||
.mapError(this::exceptionToDomainMessage)
|
.mapError(::exceptionToDomainMessage)
|
||||||
|
|
||||||
private fun createCustomer(entity: CustomerEntity) =
|
private fun createCustomer(entity: CustomerEntity) =
|
||||||
Result.of { repository.insert(entity) }
|
Result.of { repository.insert(entity) }
|
||||||
.mapError(this::exceptionToDomainMessage)
|
|
||||||
.map { DomainMessage.CustomerCreated }
|
.map { DomainMessage.CustomerCreated }
|
||||||
|
.mapError(::exceptionToDomainMessage)
|
||||||
|
|
||||||
private fun findCustomer(customers: Collection<Customer>, id: CustomerId): Result<Customer, DomainMessage.CustomerNotFound> {
|
private fun findCustomer(customers: Collection<Customer>, id: CustomerId): Result<Customer, DomainMessage.CustomerNotFound> {
|
||||||
val customer = customers.find { it.id == id }
|
val customer = customers.find { it.id == id }
|
||||||
return if (customer != null) Ok(customer) else Err(DomainMessage.CustomerNotFound)
|
return if (customer != null) Ok(customer) else Err(DomainMessage.CustomerNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun differenceBetween(old: Customer, new: Customer) = when {
|
private fun differenceBetween(old: Customer, new: Customer): DomainMessage.EmailAddressChanged? {
|
||||||
new.email != old.email -> DomainMessage.EmailAddressChanged(old.email.address, new.email.address)
|
return if (new.email != old.email) {
|
||||||
else -> null
|
DomainMessage.EmailAddressChanged(old.email.address, new.email.address)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun exceptionToDomainMessage(t: Throwable) = when (t) {
|
private fun exceptionToDomainMessage(t: Throwable) = when (t) {
|
||||||
|
Loading…
Reference in New Issue
Block a user