Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
feat: Add operation-definition.kt
Browse files Browse the repository at this point in the history
Signed-off-by: Ibrahim AlTamimi <ibm.iloom@gmail.com>
  • Loading branch information
ibm-iloom committed Feb 26, 2021
1 parent 2e8a827 commit fac4b29
Show file tree
Hide file tree
Showing 33 changed files with 232 additions and 131 deletions.
2 changes: 1 addition & 1 deletion ktx-radix-adapter/ktx-radix-adapter-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-adapter</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ktx-radix-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
7 changes: 6 additions & 1 deletion ktx-radix-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
Expand Down Expand Up @@ -108,6 +108,11 @@
<artifactId>ktx-radix-development-usecase-reactive</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>ktx-radix-development-operation</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
14 changes: 14 additions & 0 deletions ktx-radix-development/ktx-radix-development-operation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>ktx-radix-development-operation</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.quee.ktx.radix.develop.operation

/**
* Created By [*Ibrahim Al-Tamimi *](https://www.linkedin.com/in/iloom/)
* Created At 21, **Sun February, 2021**
* Project *ktx-radix* [Quee.IO]
*/
interface Operation<RQ, RS> {
fun RQ.operate(): RS
}

interface OperationRole<RQ, RS> {
fun RQ.operateRole(): RS
}

interface Operator<RQ, RS> {
fun RQ.operate(response: RS)
}

infix fun <RQ, RS> Operation<RQ, RS>.operationBuilder(
builder: OperationBuilder<RQ, RS>.() -> Unit,
): Operation<RQ, RS> =
OperationBuilder(this)
.apply {
builder()
}

class OperationBuilder<RQ, RS>(
private val mainOperation: Operation<RQ, RS>,
) : Operation<RQ, RS> {
private val roles: MutableList<OperationRole<RQ, Unit>> = mutableListOf()
private val afterOperators: MutableList<Operator<RQ, RS>> = mutableListOf()

private fun RQ.before() =
roles.forEach { it.run { operateRole() } }

private fun RQ.process() =
mainOperation.run { operate() }

private fun RQ.after(response: RS) =
afterOperators.forEach {
it.run { operate(response) }
}

infix fun after(operator: Operator<RQ, RS>) {
this.afterOperators.add(operator)
}

infix fun after(operator: RQ.(RS) -> Unit) {
this.afterOperators.add(object : Operator<RQ, RS> {
override fun RQ.operate(response: RS) {
operator(response)
}
})
}

infix fun install(role: OperationRole<RQ, Unit>) {
roles.add(role)
}

infix fun install(role: RQ.() -> Unit) {
roles.add(object : OperationRole<RQ, Unit> {
override fun RQ.operateRole() {
role()
}
})
}

override fun RQ.operate(): RS {
before()
return process().apply {
after(this)
}
}
}


2 changes: 1 addition & 1 deletion ktx-radix-development/ktx-radix-development-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ktx-radix-development/ktx-radix-development-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ktx-radix-development</artifactId>
<groupId>io.quee.ktx.radix</groupId>
<version>v1.1.3-RELEASE</version>
<version>v1.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -17,10 +17,5 @@
<artifactId>ktx-radix-development-usecase-validation</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quee.ktx.radix.develop.usecase

import io.quee.ktx.radix.develop.usecase.model.UseCaseRequest
import io.quee.ktx.radix.develop.usecase.validation.func.DefaultUseCaseValidator
import io.quee.ktx.radix.develop.usecase.validation.validate

/**
* Created By [*Ibrahim Al-Tamimi *](https://www.linkedin.com/in/iloom/)
* Created At 26, **Fri February, 2021**
* Project *ktx-radix* [Quee.IO]
*/
object JavaXValidator : (UseCaseRequest) -> Unit {
private val useCaseValidator = DefaultUseCaseValidator.create()
override fun invoke(p1: UseCaseRequest) =
useCaseValidator.run {
validate(p1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.quee.ktx.radix.develop.usecase

import io.quee.ktx.radix.develop.usecase.actionable.ActionableCommandUseCase
import io.quee.ktx.radix.develop.usecase.actionable.ActionableFunctionalUseCase
import io.quee.ktx.radix.develop.usecase.factory.UseCaseFactory
import io.quee.ktx.radix.develop.usecase.model.UseCaseRequest
import io.quee.ktx.radix.develop.usecase.model.UseCaseResponse

/**
* Created By [*Ibrahim AlTamimi *](https://www.linkedin.com/in/iloom/)
* Created At 28, **Sat November, 2020**
* Project *ktx-radix* [Quee.IO]
*/

typealias UseCaseRole<RQ, RS> = RQ.() -> RS
typealias UseCaseOperator<RQ, RS> = RQ.() -> RS

fun <F : UseCaseFactory, RQ : UseCaseRequest> F.commandUseCaseBuilder(
command: UseCaseOperator<RQ, Unit>,
builder: BuilderCommandUseCase<RQ>.(F) -> Unit,
): CommandUseCase<RQ> =
BuilderCommandUseCase(command).apply {
builder(this@commandUseCaseBuilder)
}

infix fun <RQ : UseCaseRequest> UseCaseOperator<RQ, Unit>.commandUseCaseBuilder(
builder: BuilderCommandUseCase<RQ>.() -> Unit,
): CommandUseCase<RQ> =
BuilderCommandUseCase(this).apply {
builder()
}

fun <F : UseCaseFactory, RQ : UseCaseRequest, RS : UseCaseResponse> F.functionalUseCaseBuilder(
function: UseCaseOperator<RQ, RS>,
builder: BuilderFunctionalUseCase<RQ, RS>.(F) -> Unit,
): FunctionalUseCase<RQ, RS> =
BuilderFunctionalUseCase(function).apply {
builder(this@functionalUseCaseBuilder)
}

infix fun <RQ : UseCaseRequest, RS : UseCaseResponse> UseCaseOperator<RQ, RS>.functionalUseCaseBuilder(
builder: BuilderFunctionalUseCase<RQ, RS>.() -> Unit,
): FunctionalUseCase<RQ, RS> =
BuilderFunctionalUseCase(this).apply {
builder()
}

class BuilderCommandUseCase<RQ : UseCaseRequest>(
private val command: UseCaseOperator<RQ, Unit>,
) : ActionableCommandUseCase<RQ>() {
private val afterOperators: MutableList<UseCaseOperator<RQ, Unit>> = mutableListOf()
private val roles: MutableList<UseCaseRole<RQ, Unit>> = mutableListOf()
override fun RQ.before() =
roles.forEach {
it()
}

override fun RQ.doExecute() = command()
override fun RQ.after(response: Unit) = afterOperators.forEach { it() }

infix fun afterCommand(operator: UseCaseOperator<RQ, Unit>) {
this.afterOperators.add(operator)
}

infix fun install(role: UseCaseRole<RQ, Unit>) {
roles.add(role)
}
}

class BuilderFunctionalUseCase<RQ : UseCaseRequest, RS : UseCaseResponse>(
private val function: UseCaseOperator<RQ, RS>,
) : ActionableFunctionalUseCase<RQ, RS>() {
private val roles: MutableList<UseCaseRole<RQ, Unit>> = mutableListOf()
private val afterOperators: MutableList<UseCaseOperator<RQ, RS>> = mutableListOf()

override fun RQ.before() = roles.forEach { it() }

override fun RQ.doProcess() = function()

override fun RQ.after(response: RS) = afterOperators.forEach { it() }

infix fun afterFunction(operator: UseCaseOperator<RQ, RS>) {
this.afterOperators.add(operator)
}

infix fun install(role: UseCaseRole<RQ, Unit>) {
roles.add(role)
}
}
Loading

0 comments on commit fac4b29

Please sign in to comment.