Skip to content

Drop operation #77

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 1, 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
41 changes: 41 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/Drop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import operation.StandardLogicOperation
import utils.asList
import utils.secondOrNull
import utils.thirdOrNull

object Drop : StandardLogicOperation {
override fun evaluateLogic(expression: Any?, data: Any?): Any? =
with(expression.asList) {
val dropCandidate = firstOrNull()
val count = secondOrNull()
val mode = (thirdOrNull() as? String).toDropMode()

(count as? Int)?.let { dropCandidate.dropElements(it, mode) }
}

private fun String?.toDropMode() = when (this) {
"first" -> DropMode.First
"last" -> DropMode.Last
else -> DropMode.Unknown
}

private fun Any?.dropElements(count: Int, mode: DropMode) =
when (this) {
is String -> modeBasedDrop(mode = mode, first = { drop(count) }, last = { dropLast(count) })
is List<*> -> modeBasedDrop(mode = mode, first = { drop(count) }, last = { dropLast(count) })
else -> null
}

private fun modeBasedDrop(mode: DropMode, first: (() -> Any?), last: (() -> Any?)) =
when (mode) {
DropMode.First -> first()
DropMode.Last -> last()
DropMode.Unknown -> null
}
}

private sealed class DropMode {
object First : DropMode()
object Last : DropMode()
object Unknown : DropMode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ object OperationsProvider {
"length" to Length,
"lowercase" to Lowercase,
"uppercase" to Uppercase,
"toArray" to ToArray,

// time
"currentTime" to CurrentTimeMillis,

// array
"size" to Size,
"distinct" to Distinct,
"toArray" to ToArray,

"drop" to Drop,
"reverse" to Reverse
)

Expand Down
187 changes: 187 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/DropTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import JsonLogicResult.Failure
import JsonLogicResult.Success
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import string.Lowercase

class DropTest : FunSpec({
val operatorName = "drop"
val logicEngine = JsonLogicEngine.Builder()
.addStandardOperations(
mapOf(
operatorName to Drop,
"lowercase" to Lowercase,
"reverse" to Reverse
)
).build()

withData(
nameFn = { input -> "Should evaluated ${input.expression} with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf(
operatorName to listOf(
mapOf(
"reverse" to listOf(
listOf(
"element1",
"element2",
"element3"
)
)
), 2, "first"
)
),
result = Success(listOf("element1"))
),
TestInput(
expression = mapOf(operatorName to listOf(mapOf("lowercase" to mapOf("var" to "key")), 4, "last")),
data = mapOf("key" to "APPLE"),
result = Success("a")
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4), 2, "last")),
result = Success(listOf(1, 2))
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4), 0, "last")),
result = Success(listOf(1, 2, 3, 4))
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4), 2, "first")),
result = Success(listOf(3, 4))
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4), null, "first")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4), 2)),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4), "2", "first")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(1, 2, 3, 4))),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(true, false, false), 2, "first")),
result = Success(listOf(false))
),
TestInput(
expression = mapOf(operatorName to listOf(listOf(null, null, null), 1, "first")),
result = Success(listOf(null, null))
),
TestInput(
expression = mapOf(operatorName to listOf(listOf("banana", "strawberry", "spinach"), 1, "last")),
result = Success(listOf("banana", "strawberry"))
),
TestInput(
expression = mapOf(operatorName to listOf(listOf("banana", "strawberry", "spinach"), 3, "last")),
result = Success(emptyList<String>())
),
TestInput(
expression = mapOf(operatorName to listOf(listOf("banana", "strawberry", "spinach"), 3, "first")),
result = Success(emptyList<String>())
),
TestInput(
expression = mapOf(operatorName to listOf(listOf("banana", 1, true), 2, "first")),
result = Success(listOf(true))
),
TestInput(
expression = mapOf(
operatorName to listOf(
listOf(listOf("banana"), listOf("apple"), listOf("pear")),
1,
"last"
)
),
result = Success(listOf(listOf("banana"), listOf("apple")))
),
TestInput(
expression = mapOf(
operatorName to listOf(
mapOf(
operatorName to listOf(
listOf(
"pear",
"cabbage",
"courgette"
), 1, "first"
)
), 1, "last"
)
),
result = Success(listOf("cabbage"))
),
TestInput(
expression = mapOf(
operatorName to listOf(
mapOf(
operatorName to listOf(
"101", 1, "first"
)
), 1, "last"
)
),
result = Success("0")
),
TestInput(
expression = mapOf(operatorName to listOf("12345", 2, "first")),
result = Success("345")
),
TestInput(
expression = mapOf(operatorName to listOf("12345", 0, "first")),
result = Success("12345")
),
TestInput(
expression = mapOf(operatorName to listOf("", 5, "first")),
result = Success("")
),
TestInput(
expression = mapOf(operatorName to listOf(" ", 4, "first")),
result = Success(" ")
),
TestInput(
expression = mapOf(operatorName to listOf("12345", 2, "last")),
result = Success("123")
),
TestInput(
expression = mapOf(operatorName to mapOf("var" to "key")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to 1.3),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to null),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to true),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to emptyList<Any>()),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(mapOf("var" to "key"), 2, "last")),
data = mapOf("key" to listOf(1, 2, 3, 4, 5)),
result = Success(listOf(1, 2, 3))
),
)
// given
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)

// then
evaluationResult shouldBe testInput.result
}
})