Skip to content

Join to string operation #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import array.Distinct
import array.Find
import array.JoinToString
import array.Size
import array.Sort
import operation.FunctionalLogicOperation
Expand Down Expand Up @@ -28,7 +29,8 @@ object OperationsProvider {
"size" to Size,
"sort" to Sort,
"distinct" to Distinct,

"joinToString" to JoinToString,

"drop" to Drop,
"reverse" to Reverse
)
Expand Down
47 changes: 47 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/array/JoinToString.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package array

import operation.StandardLogicOperation
import utils.asList

object JoinToString : StandardLogicOperation {
private const val ELEMENTS_ARG_INDEX = 0
private const val SEPARATOR_ARG_INDEX = 1
private const val PREFIX_ARG_INDEX = 2
private const val POSTFIX_ARG_INDEX = 3
private const val LIMIT_ARG_INDEX = 4
private const val TRUNCATED_ARG_INDEX = 5

override fun evaluateLogic(expression: Any?, data: Any?): Any? = expression.asList.toOperationArguments()?.join()

private fun JoinToStringArguments.join() =
elementsToJoin.joinToString(separator, prefix, postfix, limit, truncated)

private fun List<Any?>.toOperationArguments(): JoinToStringArguments? = runCatching {
checkLimitArg()?.let { limit ->
JoinToStringArguments(
elementsToJoin = get(ELEMENTS_ARG_INDEX).asList,
separator = get(SEPARATOR_ARG_INDEX) as String,
prefix = get(PREFIX_ARG_INDEX) as String,
postfix = get(POSTFIX_ARG_INDEX) as String,
limit = limit,
truncated = get(TRUNCATED_ARG_INDEX) as String
)
}
}.fold(
onSuccess = { it },
onFailure = { null }
)

private fun List<Any?>.checkLimitArg() = (get(LIMIT_ARG_INDEX) as Number).takeIf {
it.toDouble() == it.toInt().toDouble()
}?.toInt()
}

private data class JoinToStringArguments(
val elementsToJoin: List<Any?>,
val separator: String,
val prefix: String,
val postfix: String,
val limit: Int,
val truncated: String
)
236 changes: 236 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/array/JoinToStringTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package array

import JsonLogicEngine
import JsonLogicResult.Failure
import JsonLogicResult.Success
import TestInput
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

class JoinToStringTest : FunSpec({
val operationName = "joinToString"
val logicEngine =
JsonLogicEngine.Builder().addStandardOperations(
mapOf(
operationName to JoinToString,
"distinct" to Distinct
)
).build()

withData(
nameFn = { input -> "Should evaluated $operationName with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf(
operationName to listOf(
mapOf(
"distinct" to listOf(
listOf(
"strawberry",
"apple",
"banana",
"banana",
"pineapple"
)
)
), " + ", "add some: ", " and mix!", 3, "random fruits"
)
),
result = Success("add some: strawberry + apple + banana + random fruits and mix!")
),
TestInput(
expression = mapOf(
operationName to listOf(
mapOf(
"distinct" to listOf(
listOf(
"strawberry",
"apple",
"banana",
"banana",
"pineapple"
)
)
), " + ", "add some: ", " and mix!", null, "random fruits"
)
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operationName to listOf(
mapOf("distinct" to listOf(mapOf("var" to "fruits"))),
" + ",
"add some: ",
" and mix!",
3,
"random fruits"
)
),
data = mapOf(
"fruits" to listOf(
"strawberry",
"apple",
"banana",
"banana",
"pineapple"
)
),
result = Success("add some: strawberry + apple + banana + random fruits and mix!")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("test", "test", 1), ",", "", "", 30, "...")),
result = Success("test,test,1")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("test", "test", 1), ",", "", "", 30, "...")),
result = Success("test,test,1")
),
TestInput(
expression = mapOf(
operationName to listOf(
listOf(listOf("test"), listOf("test"), 1),
",",
"",
"",
30,
"..."
)
),
result = Success("[test],[test],1")
),
TestInput(
expression = mapOf(operationName to listOf(listOf(null, true, 1), ",", "", "", 30, "...")),
result = Success("null,true,1")
),
TestInput(
expression = mapOf(
operationName to listOf(
listOf("13", ".", 11, ".", 1990),
"",
"date:",
"",
4,
"19**"
)
),
result = Success("date:13.11.19**")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("sp", "ace"), " ", " ", " ", 30, "...")),
result = Success(" sp ace ")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("sp", "ace"), " ", " ", " ", 30, "...")),
result = Success(" sp ace ")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("fruit", "salad"), " ", "", "", 1, "*****")),
result = Success("fruit *****")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("fruit", "salad"), " ", "", "", 1.0, "*****")),
result = Success("fruit *****")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("fruit", "salad"), " ", "", "", 1.2, "*****")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to listOf(emptyList<String>(), " ", "", "", 1, "*****")),
result = Success("")
),
TestInput(
expression = mapOf(operationName to listOf(emptyList<String>(), " ", "", "", 0, "*****")),
result = Success("")
),
TestInput(
expression = mapOf(
operationName to listOf(
emptyList<String>(),
" ",
"no elements ",
"to join",
0,
"*****"
)
),
result = Success("no elements to join")
),
TestInput(
expression = mapOf(operationName to listOf(listOf(0.00, 0, 0), " ", "", "", 0, "*****")),
result = Success("*****")
),
TestInput(
expression = mapOf(
operationName to listOf(
listOf(0.00000, 0.000000, 0.00000000),
".",
"",
"",
3,
"*****"
)
),
result = Success("0.0.0.0.0.0")
),
TestInput(
expression = mapOf(operationName to listOf(listOf(127, 0, 0, 1), ".", "", "", 4, "*****")),
result = Success("127.0.0.1")
),
TestInput(
expression = mapOf(operationName to listOf(listOf("test", "test", 1), ",", "", "", "30", "...")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to null),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to true),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to false),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to 1.3),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to "strawberry"),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to mapOf("var" to "fruit")),
data = mapOf("fruit" to "banana"),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to emptyList<String>()),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to listOf(listOf("test", "test", 1))),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to listOf(listOf("test", "test", 1), ",", "", "", 30)),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operationName to listOf(listOf("test", "test", 1), ",", "", null, null, null)),
result = Failure.NullResult
)
)
// given
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)

// then
evaluationResult shouldBe testInput.result
}
})