add support of ranges in strings

This commit is contained in:
InsanusMokrassar 2020-07-24 13:16:56 +06:00
parent 5c452d58b7
commit 5358bfdb47
4 changed files with 36 additions and 2 deletions

View File

@ -12,6 +12,7 @@
* Updates in libraries: * Updates in libraries:
* Klock `1.11.3` -> `1.11.14` * Klock `1.11.3` -> `1.11.14`
* Coroutines `1.3.7` -> `1.3.8` * Coroutines `1.3.7` -> `1.3.8`
* Ranges support was included. Now it is possible to correctly use syntax `0-5` in strings schedules
### 0.2.3 ### 0.2.3

View File

@ -81,6 +81,16 @@ sealed class TimeBuilder (
*/ */
@Suppress("unused") @Suppress("unused")
infix fun upTo(endIncluding: Int): Array<Int> = this from 0 upTo endIncluding infix fun upTo(endIncluding: Int): Array<Int> = this from 0 upTo endIncluding
/**
* Will fill up this timeline from [this] up to [endIncluding]
*/
@Suppress("MemberVisibilityCanBePrivate")
infix operator fun Int.rangeTo(endIncluding: Int) = upTo(endIncluding)
/**
* Shortcut for "[from] 0 [rangeTo] [endIncluding]"
*/
@Suppress("MemberVisibilityCanBePrivate")
infix operator fun rangeTo(endIncluding: Int) = (this from 0) rangeTo endIncluding
internal fun build() = result ?.map { it.toByte() } ?.toTypedArray() internal fun build() = result ?.map { it.toByte() } ?.toTypedArray()
} }

View File

@ -7,6 +7,10 @@ private fun createSimpleScheduler(from: String, dataRange: IntRange): Array<Byte
val results = things.flatMap { val results = things.flatMap {
when { when {
it.contains("-") -> {
val splitted = it.split("-")
(splitted.first().toInt().clamp(dataRange) .. splitted[1].toInt().clamp(dataRange)).toList()
}
it.contains("/") -> { it.contains("/") -> {
val (start, step) = it.split("/") val (start, step) = it.split("/")
val startNum = (if (start.isEmpty() || start == "*") { val startNum = (if (start.isEmpty() || start == "*") {

View File

@ -1,12 +1,12 @@
package com.insanusmokrassar.krontab.utils package com.insanusmokrassar.krontab.utils
import com.insanusmokrassar.krontab.buildSchedule import com.insanusmokrassar.krontab.buildSchedule
import com.insanusmokrassar.krontab.builder.buildSchedule
import com.insanusmokrassar.krontab.createSimpleScheduler
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.flow.takeWhile
import kotlin.math.max
import kotlin.math.min
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -60,4 +60,23 @@ class StringParseTest {
} }
} }
} }
@Test
fun testThatFlowIsCorrectlyWorkEverySeveralSecondsRangeBuiltOnString() {
val rangesEnds = listOf(0 to 5, 30 to 35)
val kronScheduler = buildSchedule("${rangesEnds.joinToString(",") { "${it.first}-${it.second}" }} * * * *")
val flow = kronScheduler.asFlow()
runTest {
val ranges = rangesEnds.map { it.first .. it.second }.flatten().toMutableList()
val expectedCollects = rangesEnds.sumBy { it.second - it.first + 1 }
var collected = 0
flow.takeWhile { ranges.isNotEmpty() }.collect {
ranges.remove(it.seconds)
collected++
}
assertEquals(expectedCollects, collected)
}
}
} }