Skip to content

Commit

Permalink
feat: apply suggestion of removing nested either of SimpleSequenceCall
Browse files Browse the repository at this point in the history
  • Loading branch information
realdavidvega committed May 2, 2023
1 parent 38fd29c commit 813324f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.xebia.functional.chains

import arrow.core.Either
import arrow.core.NonEmptyList
import arrow.core.raise.Raise
import arrow.core.raise.either
import arrow.core.raise.ensure
import arrow.core.raise.ensureNotNull
import arrow.core.raise.recover
import arrow.core.raise.zipOrAccumulate

fun Raise<Chain.Error>.SimpleSequentialChain(
fun Raise<Chain.Error>.SimpleSequenceChain(
chains: List<Chain>,
inputKey: String = "input",
outputKey: String = "output",
chainOutput: Chain.ChainOutput = Chain.ChainOutput.OnlyOutput
): SimpleSequentialChain =
SimpleSequentialChain.either(chains, inputKey, outputKey, chainOutput).bind()
): SimpleSequenceChain =
SimpleSequenceChain.either(chains, inputKey, outputKey, chainOutput).bind()

class SimpleSequentialChain private constructor(
class SimpleSequenceChain private constructor(
private val chains: List<Chain>,
private val inputKey: String,
private val outputKey: String,
Expand All @@ -37,20 +37,21 @@ class SimpleSequentialChain private constructor(

companion object {
fun either(
chains: List<Chain>, inputKey: String, outputKey: String, chainOutput: Chain.ChainOutput
): Either<SequenceChain.InvalidKeys, SimpleSequentialChain> =
chains: List<Chain>,
inputKey: String,
outputKey: String,
chainOutput: Chain.ChainOutput
): Either<SequenceChain.InvalidKeys, SimpleSequenceChain> =
either {
chains.map { chain ->
either<NonEmptyList<Chain.Error>, Chain> {
val mappedChains: List<Chain> = chains.map { chain ->
recover({
zipOrAccumulate(
{ validateInputKeys(chain.config.inputKeys) },
{ validateOutputKeys(chain.config.outputKeys) }
) { _, _ -> chain }
}.bind()
{ validateOutputKeys(chain.config.outputKeys) }) { _, _ -> chain }
}) { raise(SequenceChain.InvalidKeys(it.joinToString(transform = Chain.Error::reason))) }
}
}.mapLeft {
SequenceChain.InvalidKeys(it.joinToString(transform = Chain.Error::reason))
}.map { SimpleSequentialChain(chains, inputKey, outputKey, chainOutput) }
SimpleSequenceChain(mappedChains, inputKey, outputKey, chainOutput)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SimpleSequentialChainSpec : StringSpec({
val chains = listOf(chain1)

either {
val ssc = SimpleSequentialChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
val ssc = SimpleSequenceChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
ssc.run(mapOf("input" to "123")).bind()
} shouldBeRight mapOf("input" to "123", "output" to "123dr")
}
Expand All @@ -24,7 +24,7 @@ class SimpleSequentialChainSpec : StringSpec({
val chains = listOf(chain1, chain2, chain3)

either {
val ssc = SimpleSequentialChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
val ssc = SimpleSequenceChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
ssc.run(mapOf("input" to "123")).bind()
} shouldBeRight mapOf("input" to "123", "output" to "123drdrdr")
}
Expand All @@ -35,7 +35,7 @@ class SimpleSequentialChainSpec : StringSpec({
val chains = listOf(chain1, chain2)

either {
val ssc = SimpleSequentialChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
val ssc = SimpleSequenceChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
ssc.run(mapOf("input" to "123")).bind()
} shouldBeLeft SequenceChain.InvalidKeys("The expected inputs are more than one: {bar}, {foo}")
}
Expand All @@ -46,22 +46,10 @@ class SimpleSequentialChainSpec : StringSpec({
val chains = listOf(chain1, chain2)

either {
val ssc = SimpleSequentialChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
val ssc = SimpleSequenceChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
ssc.run(mapOf("input" to "123")).bind()
} shouldBeLeft SequenceChain.InvalidKeys("The expected outputs are more than one: {bar}, {foo}")
}

"SimpleSequentialChain should fail if multiple input and output variables are expected" {
val chain1 = FakeChain(inputVariables = setOf("foo", "bar"), outputVariables = setOf("bar", "foo"))
val chain2 = FakeChain(inputVariables = setOf("bar"), outputVariables = setOf("baz"))
val chains = listOf(chain1, chain2)

either {
val ssc = SimpleSequentialChain(chains = chains, chainOutput = Chain.ChainOutput.InputAndOutput)
ssc.run(mapOf("input" to "123")).bind()
} shouldBeLeft SequenceChain.InvalidKeys("The expected inputs are more than one: {foo}, {bar}, " +
"The expected outputs are more than one: {bar}, {foo}")
}
})

data class FakeChain(private val inputVariables: Set<String>, private val outputVariables: Set<String>) : Chain {
Expand Down

0 comments on commit 813324f

Please sign in to comment.