Skip to content

Commit

Permalink
adapt to arrow deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
sschaeffner committed Feb 3, 2023
1 parent 911370c commit 7d84b69
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AddingDevice(
.swap()
.map { sourcingEvent.aggregateRootId }
}
.tap { log.debug("...addDevice done") }
.tapLeft { log.error("...addDevice error: $it") }
.onRight { log.debug("...addDevice done") }
.onLeft { log.error("...addDevice error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class GettingConfiguration(
.map { entries -> entries.toMap() }
.map { pinToTool -> Result(device = it, attachedTools = pinToTool) }
}
.tap { log.debug("...getConfiguration successful") }
.tapLeft { log.error("...getConfiguration error: $it") }
.onRight { log.debug("...getConfiguration successful") }
.onLeft { log.error("...getConfiguration error: $it") }
}

data class Result(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RestartingDevice(

return deviceRepository.getById(deviceId)
.flatMap { restartDevice.restartDevice(deviceId, correlationId) }
.tap { log.debug("...restartDevice done") }
.tapLeft { log.error("...restartDevice error: $it") }
.onRight { log.debug("...restartDevice done") }
.onLeft { log.error("...restartDevice error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cloud.fabX.fabXaccess.device.application

import arrow.core.Either
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import cloud.fabX.fabXaccess.common.application.LoggerFactory
import cloud.fabX.fabXaccess.common.model.CorrelationId
import cloud.fabX.fabXaccess.common.model.DeviceId
Expand Down Expand Up @@ -39,37 +41,38 @@ class UnlockingTool(
.flatMap { toolRepository.getToolById(toolId) }
.flatMap { requireToolTypeUnlock(it, correlationId) }
.flatMap { unlockingToolAtDevice.unlockTool(deviceId, toolId, correlationId) }
.tap { log.debug("...unlockTool done") }
.tapLeft { log.error("...unlockTool error: $it") }
.onRight { log.debug("...unlockTool done") }
.onLeft { log.error("...unlockTool error: $it") }
}

private fun requireToolAttachedToDevice(
device: Device,
toolId: ToolId,
correlationId: CorrelationId
): Either<Error, Unit> =
Either.conditionally(device.hasAttachedTool(toolId),
{
Error.ToolNotAttachedToDevice(
"Tool $toolId not attached to device ${device.id}.",
device.id,
toolId,
correlationId
)
},
{}
)
if (!device.hasAttachedTool(toolId)) {
Error.ToolNotAttachedToDevice(
"Tool $toolId not attached to device ${device.id}.",
device.id,
toolId,
correlationId
).left()
} else {
Unit.right()
}

private fun requireToolTypeUnlock(
tool: Tool,
correlationId: CorrelationId
): Either<Error, Unit> =
Either.conditionally(tool.type == ToolType.UNLOCK, {
if (tool.type != ToolType.UNLOCK) {
Error.ToolTypeNotUnlock(
"Cannot remotely unlock: Tool type is not unlock.",
tool.id,
tool.type,
correlationId
)
}, {})
).left()
} else {
Unit.right()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cloud.fabX.fabXaccess.device.model

import arrow.core.Either
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import cloud.fabX.fabXaccess.common.model.CorrelationId
import cloud.fabX.fabXaccess.common.model.Error
import cloud.fabX.fabXaccess.common.model.Identity
Expand Down Expand Up @@ -35,38 +37,32 @@ data class MacSecretIdentity(val mac: String, val secret: String) : DeviceIdenti
mac: String,
correlationId: CorrelationId?
): Either<Error, Unit> {
return Either.conditionally(
mac.matches(macRegex),
{
Error.MacInvalid(
"Mac is invalid (has to match $macRegex).",
mac,
macRegex,
correlationId
)
},
{}
)
return if (!mac.matches(macRegex)) {
Error.MacInvalid(
"Mac is invalid (has to match $macRegex).",
mac,
macRegex,
correlationId
).left()
} else {
Unit.right()
}
}

private fun requireValidSecret(
secret: String,
correlationId: CorrelationId?
): Either<Error, Unit> {
return Either.conditionally(
secret.matches(secretRegex),
{
Error.SecretInvalid(
"Secret is invalid (has to match $secretRegex).",
secret,
secretRegex,
correlationId
)
},
{}
)
return if (!secret.matches(secretRegex)) {
Error.SecretInvalid(
"Secret is invalid (has to match $secretRegex).",
secret,
secretRegex,
correlationId
).left()
} else {
Unit.right()
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AddingQualification(
.toEither { }
.swap()
.map { sourcingEvent.aggregateRootId }
.tap { log.debug("...addQualification done") }
.tapLeft { log.error("...addQualification error: $it") }
.onRight { log.debug("...addQualification done") }
.onLeft { log.error("...addQualification error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import arrow.core.Either
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.left
import arrow.core.right
import cloud.fabX.fabXaccess.common.model.AggregateRootEntity
import cloud.fabX.fabXaccess.common.model.ChangeableValue
import cloud.fabX.fabXaccess.common.model.CorrelationId
Expand Down Expand Up @@ -117,20 +119,18 @@ data class Qualification internal constructor(
): Either<Error, Unit> {
val tools = gettingToolsByQualificationId.getToolsByQualificationId(id)

return Either.conditionally(
tools.isEmpty(),
{
val toolIds = tools.joinToString { tool -> tool.id.toString() }
return if (tools.isNotEmpty()) {
val toolIds = tools.joinToString { tool -> tool.id.toString() }

Error.QualificationInUse(
"Qualification in use by tools ($toolIds).",
id,
tools.map { tool -> tool.id }.toSet(),
correlationId
)
},
{}
)
Error.QualificationInUse(
"Qualification in use by tools ($toolIds).",
id,
tools.map { tool -> tool.id }.toSet(),
correlationId
).left()
} else {
Unit.right()
}
}

class EventHistoryDoesNotStartWithQualificationCreated(message: String) : Exception(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AddingTool(
.toEither { it.aggregateRootId }
.swap()
}
.tap { log.debug("...addTool done") }
.tapLeft { log.debug("...addTool error: $it") }
.onRight { log.debug("...addTool done") }
.onLeft { log.debug("...addTool error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AddingUser(
.toEither { it.aggregateRootId }
.swap()
}
.tap { log.debug("...addUser done") }
.tapLeft { log.error("...addUser error: $it") }
.onRight { log.debug("...addUser done") }
.onLeft { log.error("...addUser error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ChangingPassword(
.toEither { }
.swap()
}
.tap { log.debug("...changeOwnPassword done") }
.tapLeft { log.error("...changeOwnPassword error: $it") }
.onRight { log.debug("...changeOwnPassword done") }
.onLeft { log.error("...changeOwnPassword error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cloud.fabX.fabXaccess.user.application
import arrow.core.Either
import arrow.core.Option
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import cloud.fabX.fabXaccess.common.application.LoggerFactory
import cloud.fabX.fabXaccess.common.model.CorrelationId
import cloud.fabX.fabXaccess.common.model.Error
Expand Down Expand Up @@ -68,23 +70,21 @@ class DeletingUser(
return requireUserIsSoftDeleted(correlationId, userId)
.flatMap { hardDeletingUser.hardDelete(userId) }
.map { }
.tap { log.debug("...hardDeleteUser done") }
.tapLeft { log.error("...hardDeleteUser error: $it") }
.onRight { log.debug("...hardDeleteUser done") }
.onLeft { log.error("...hardDeleteUser error: $it") }
}

private suspend fun requireUserIsSoftDeleted(
correlationId: CorrelationId,
userId: UserId
): Either<Error, Unit> =
Either.conditionally(
gettingSoftDeletedUsers.getSoftDeleted().map { it.id }.contains(userId),
{
Error.SoftDeletedUserNotFound(
"Soft deleted user not found.",
userId,
correlationId
)
},
{ }
)
if (!gettingSoftDeletedUsers.getSoftDeleted().map { it.id }.contains(userId)) {
Error.SoftDeletedUserNotFound(
"Soft deleted user not found.",
userId,
correlationId
).left()
} else {
Unit.right()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GettingAuthorizedTools(
}
}
}
.tap { log.debug("...getAuthorizedTools done") }
.tapLeft { log.error("...getAuthorizedTools error: $it") }
.onRight { log.debug("...getAuthorizedTools done") }
.onLeft { log.error("...getAuthorizedTools error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GettingUserIdByWikiName(

return gettingUserByWikiName.getByWikiName(wikiName)
.map { it.id }
.tap { log.debug("...getUserIdByWikiName done") }
.tapLeft { log.error("...getUserIdByWikiName error: $it") }
.onRight { log.debug("...getUserIdByWikiName done") }
.onLeft { log.error("...getUserIdByWikiName error: $it") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class LoggingUnlockedTool(loggerFactory: LoggerFactory) {
return actor.onBehalfOf.toOption()
.toEither { Error.NotAuthenticated("Required authentication not found.", correlationId) }
.map { it.userId }
.tap {
.onRight {
log.info("User $it unlocked tool $toolId")
}
.tapLeft { log.error("Error while logging tool unlock ($actor, $toolId): $it") }
.onLeft { log.error("Error while logging tool unlock ($actor, $toolId): $it") }
.map { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cloud.fabX.fabXaccess.user.application

import arrow.core.Either
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import arrow.core.toOption
import cloud.fabX.fabXaccess.common.application.LoggerFactory
import cloud.fabX.fabXaccess.common.model.CorrelationId
Expand Down Expand Up @@ -33,11 +35,11 @@ class ValidatingSecondFactor(
.flatMap { onBehalfOfId ->
userRepository.getById(onBehalfOfId)
.flatMap {
Either.conditionally(
it.identities.contains(secondIdentity),
{ Error.InvalidSecondFactor("Invalid second factor provided.", correlationId) },
{ }
)
if (!it.identities.contains(secondIdentity)) {
Error.InvalidSecondFactor("Invalid second factor provided.", correlationId).left()
} else {
Unit.right()
}
}
}
}
Expand Down
Loading

0 comments on commit 7d84b69

Please sign in to comment.