Oliver Rümpelein
cc703d5554
- Native builds to compile now - Add Arvm 32 bit target - Add Jvm main class for test runs
75 lines
2.7 KiB
Kotlin
75 lines
2.7 KiB
Kotlin
package de.pheerai.rcdbquery.dsl
|
|
|
|
import de.pheerai.rcdbquery.dataMappings.*
|
|
|
|
inline class RcdbQueryParams(val params: Map<String, List<Int>>)
|
|
class RcdbQueryParamsBuilder {
|
|
// TODO: Somewhat wrap away, then use param of one option.
|
|
private val params: MutableMap<String, List<RcdbParamOption>> = mutableMapOf()
|
|
|
|
fun build() = RcdbQueryParams(
|
|
params
|
|
.filter { it.value.isNotEmpty() }
|
|
.mapValues { e -> e.value.map { o -> o.paramId } }
|
|
)
|
|
|
|
operator fun set(paramName: String, options: List<RcdbParamOption>) {
|
|
when (paramName) {
|
|
"order" -> if (this.params.containsKey(paramName)) {
|
|
this.params[paramName] = this.params[paramName].append(options)
|
|
} else {
|
|
this.params[paramName] = options
|
|
}
|
|
else -> this.params[paramName] = options
|
|
}
|
|
}
|
|
|
|
private fun List<RcdbParamOption>?.append(options: List<RcdbParamOption>) = this?.toMutableList()?.also { it.addAll(options) } ?: options
|
|
}
|
|
|
|
fun rcdbQuery(body: RcdbQueryParamsBuilder.() -> RcdbQueryParamsBuilder) =
|
|
RcdbUrlQuery("https://www.rcdb.com/r.htm?", rcdbQueryParams(body)).toString()
|
|
|
|
data class RcdbUrlQuery(val baseUrl: String, val params: RcdbQueryParams) {
|
|
override fun toString(): String {
|
|
return params.params
|
|
.asSequence()
|
|
.map{ e -> e.key to e.value.joinToString(separator = ",") }
|
|
.map { p -> "${p.first}=${p.second}" }
|
|
.joinToString(prefix = baseUrl, separator = "&")
|
|
}
|
|
}
|
|
|
|
fun rcdbQueryParams(body: RcdbQueryParamsBuilder.() -> RcdbQueryParamsBuilder): RcdbQueryParams {
|
|
val builder = RcdbQueryParamsBuilder()
|
|
builder.body()
|
|
return builder.build()
|
|
}
|
|
|
|
/* TODO: Even better typing (usage of
|
|
* ```
|
|
* vendors {
|
|
* vekoma()
|
|
* intamin()
|
|
* of(Vendors.PRESTON_AND_BARBIERI)
|
|
* }
|
|
* ```
|
|
* instead.
|
|
* Maybe use inline class for MutableList / List?
|
|
*/
|
|
fun RcdbQueryParamsBuilder.vendors(vendors: List<Vendor>) = apply { this[Vendor.staticParamName] = vendors }
|
|
|
|
fun RcdbQueryParamsBuilder.vendors(vararg vendors: Vendor) = this.vendors(vendors.asList())
|
|
|
|
fun RcdbQueryParamsBuilder.sortBy(orders: List<Order>) = apply {
|
|
this[Order.staticParamName] =orders
|
|
}
|
|
|
|
fun RcdbQueryParamsBuilder.sortBy(vararg orders: Order) = this.sortBy(orders.asList())
|
|
|
|
fun RcdbQueryParamsBuilder.searchFor(searchType: SearchType) =
|
|
apply { this[SearchType.staticParamName] = listOf(searchType) }
|
|
|
|
fun RcdbQueryParamsBuilder.custom(paramName: String, values: List<RcdbParamOption>) = apply { this[paramName] = values }
|
|
fun RcdbQueryParamsBuilder.custom(paramName: String, vararg values: RcdbParamOption) = apply { this[paramName] = values.asList() }
|