-
Notifications
You must be signed in to change notification settings - Fork 6
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
Drop operation #77
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae22836
Drop base
3ac9a2b
Partly implemented drop
375af90
Full Drop operation with unit tests
63c8242
more unit tests
a5b5277
Merge branch 'main' into drop-operation
KTGypsy eea8045
Enum to sealed class conversion
24bb079
Merge remote-tracking branch 'origin/drop-operation' into drop-operation
f387f3f
Detekt fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.