Prepare for new Param Builder.

This commit is contained in:
Oliver Rümpelein 2020-05-01 12:17:54 +02:00
parent 53def3bc00
commit 63c7d101a1
11 changed files with 53 additions and 44 deletions

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
enum class Classification(
override val prettyName: String,
@ -19,7 +19,7 @@ enum class Classification(
}
}
fun RcdbQueryParamsBuilder.classification(body: ClassificationBuilder.() -> ClassificationBuilder): RcdbQueryParamsBuilder {
fun MultiParamsBuilder.classification(body: ClassificationBuilder.() -> ClassificationBuilder): MultiParamsBuilder {
val builder = ClassificationBuilder()
builder.body()
this[Classification.staticParamName] = builder.build()

View file

@ -1,20 +1,20 @@
package de.pheerai.rcdbquery.dataMappings
// TODO: Find way to only have one generic parameter
sealed class RcdbParamOptionBuilder<out U : Any, T : RcdbParamOption<U>> {
sealed class MultiParamBuilder<out U : Any, T : RcdbParamOption<U>> {
private val paramList: MutableList<T> = mutableListOf()
open fun add(param: T): RcdbParamOptionBuilder<U, T> {
open fun add(param: T): MultiParamBuilder<U, T> {
paramList.add(param)
return this
}
protected open fun addAll(items: Array<T>): RcdbParamOptionBuilder<U, T> {
protected open fun addAll(items: Array<T>): MultiParamBuilder<U, T> {
paramList.addAll(items)
return this
}
protected open fun addAll(items: Collection<T>): RcdbParamOptionBuilder<U, T> {
protected open fun addAll(items: Collection<T>): MultiParamBuilder<U, T> {
paramList.addAll(items)
return this
}
@ -22,7 +22,7 @@ sealed class RcdbParamOptionBuilder<out U : Any, T : RcdbParamOption<U>> {
fun build() = paramList.toList()
}
class VendorBuilder : RcdbParamOptionBuilder<Int, Vendor>() {
class VendorBuilder : MultiParamBuilder<Int, Vendor>() {
override fun add(param: Vendor): VendorBuilder {
super.add(param)
return this
@ -36,7 +36,7 @@ class VendorBuilder : RcdbParamOptionBuilder<Int, Vendor>() {
fun gerstlauer() = this.add(Vendor.GERSTLAUER)
}
class OrderBuilder : RcdbParamOptionBuilder<Int, Order>() {
class OrderBuilder : MultiParamBuilder<Int, Order>() {
override fun add(param: Order): OrderBuilder {
super.add(param)
return this
@ -77,10 +77,10 @@ class OrderBuilder : RcdbParamOptionBuilder<Int, Order>() {
fun rideTime() = this.add(Order.RIDE_TIME)
fun state() = this.add(Order.STATUS)
fun closing() = this.add(Order.CLOSING)
fun all() = this.addAll(Order.values().filter { it.relevantForAll })
fun allRelevant() = this.addAll(Order.values().filter { it.relevantForAll })
}
class StatusBuilder : RcdbParamOptionBuilder<Int, Status>() {
class StatusBuilder : MultiParamBuilder<Int, Status>() {
override fun add(param: Status): StatusBuilder {
super.add(param)
return this
@ -92,7 +92,7 @@ class StatusBuilder : RcdbParamOptionBuilder<Int, Status>() {
fun stored() = this.add(Status.STORED)
}
class ClassificationBuilder : RcdbParamOptionBuilder<Int, Classification>() {
class ClassificationBuilder : MultiParamBuilder<Int, Classification>() {
override fun add(param: Classification): ClassificationBuilder {
super.add(param)
return this

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
@Suppress("unused")
enum class Order(
@ -51,7 +51,7 @@ enum class Order(
}
}
fun RcdbQueryParamsBuilder.sortBy(body: OrderBuilder.() -> OrderBuilder): RcdbQueryParamsBuilder {
fun MultiParamsBuilder.sortBy(body: OrderBuilder.() -> OrderBuilder): MultiParamsBuilder {
val builder = OrderBuilder()
builder.body()
this[Order.staticParamName] = builder.build()

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
class Page(override val paramValue: Int) : RcdbParamOption<Int> {
override val fullName = "The page to show"
@ -11,7 +11,7 @@ class Page(override val paramValue: Int) : RcdbParamOption<Int> {
}
}
fun RcdbQueryParamsBuilder.page(page: Int): RcdbQueryParamsBuilder = if (Page.staticParamName !in this.keys()) {
fun MultiParamsBuilder.page(page: Int) = if (Page.staticParamName !in this.keys()) {
also { this[Page.staticParamName] = listOf(Page(page)) }
} else {
error(

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
class SearchTerm(override val paramValue: String) : RcdbParamOption<String> {
override val prettyName = "Search Term"
@ -11,7 +11,7 @@ class SearchTerm(override val paramValue: String) : RcdbParamOption<String> {
}
}
fun RcdbQueryParamsBuilder.searchTerm(term: String): RcdbQueryParamsBuilder =
fun MultiParamsBuilder.searchTerm(term: String) =
if (SearchTerm.staticParamName !in this.keys()) {
also { this[SearchTerm.staticParamName] = listOf(SearchTerm(term)) }
} else {

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
@Suppress("unused")
enum class SearchType(
@ -19,7 +19,7 @@ enum class SearchType(
}
}
fun RcdbQueryParamsBuilder.searchType(searchType: SearchType): RcdbQueryParamsBuilder =
fun MultiParamsBuilder.searchType(searchType: SearchType) =
if (SearchType.staticParamName !in this.keys()) {
also { this[SearchType.staticParamName] = listOf(searchType) }
} else {

View file

@ -2,7 +2,7 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
class StartsWith(override val paramValue: String) : RcdbParamOption<String> {
override val prettyName = "Starts with"
@ -13,7 +13,7 @@ class StartsWith(override val paramValue: String) : RcdbParamOption<String> {
}
}
fun RcdbQueryParamsBuilder.startsWith(term: String) = if (StartsWith.staticParamName !in this.keys()) {
fun MultiParamsBuilder.startsWith(term: String) = if (StartsWith.staticParamName !in this.keys()) {
also { this[StartsWith.staticParamName] = listOf(StartsWith(term)) }
} else {
error(

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
@Suppress("unused")
enum class Status(
@ -29,7 +29,7 @@ enum class Status(
}
}
fun RcdbQueryParamsBuilder.status(body: StatusBuilder.() -> StatusBuilder): RcdbQueryParamsBuilder {
fun MultiParamsBuilder.status(body: StatusBuilder.() -> StatusBuilder): MultiParamsBuilder {
val builder = StatusBuilder()
builder.body()
this[Status.staticParamName] = builder.build()

View file

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
@Suppress("unused")
enum class Vendor(
@ -244,7 +244,7 @@ enum class Vendor(
companion object {
const val staticParamName = "mk"
fun getByName(name: String): Vendor? = values().firstOrNull { it.prettyName == name }
fun getByName(name: String) = values().firstOrNull { it.prettyName == name }
fun searchByName(name: String): List<Vendor> {
val searchName = name.toLowerCase()
return values().filter {
@ -254,7 +254,7 @@ enum class Vendor(
}
}
fun RcdbQueryParamsBuilder.vendors(body: VendorBuilder.() -> VendorBuilder): RcdbQueryParamsBuilder {
fun MultiParamsBuilder.vendors(body: VendorBuilder.() -> VendorBuilder): MultiParamsBuilder {
val builder = VendorBuilder()
builder.body()
this[Vendor.staticParamName] = builder.build()

View file

@ -1,14 +1,13 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
import de.pheerai.rcdbquery.dsl.MultiParamsBuilder
//region Unknown fields
@Suppress("unused")
@Deprecated(
"The parameter for extra columns has not yet been discovered (if it even exists). Use multiple values in the `sortBy` param instead (first values take sorting precedence)",
ReplaceWith("sortBy"), DeprecationLevel.ERROR
)
fun RcdbQueryParamsBuilder.extraColumns(orders: List<Order>): Nothing =
fun MultiParamsBuilder.extraColumns(orders: List<Order>): Nothing =
error("The parameter for extra columns has not yet been discovered (if it even exists). Use multiple values in the `sortBy` param instead (first values take sorting precedence)")
@Suppress("unused")
@ -16,5 +15,5 @@ fun RcdbQueryParamsBuilder.extraColumns(orders: List<Order>): Nothing =
"The parameter for extra columns has not yet been discovered (if it even exists). Use multiple values in the `sortBy` param instead (first values take sorting precedence)",
ReplaceWith("sortBy"), DeprecationLevel.ERROR
)
fun RcdbQueryParamsBuilder.extraColumns(vararg orders: Order): Nothing =
fun MultiParamsBuilder.extraColumns(vararg orders: Order): Nothing =
error("The parameter for extra columns has not yet been discovered (if it even exists). Use multiple values in the `sortBy` param instead (first values take sorting precedence)")

View file

@ -3,37 +3,47 @@ package de.pheerai.rcdbquery.dsl
import de.pheerai.rcdbquery.dataMappings.RcdbParamOption
import de.pheerai.rcdbquery.dataMappings.SearchType
class RcdbQueryParams(val params: Map<String, List<String>>)
// In preparation for new handler
sealed class Params<T> {
abstract val params: Map<String, T>
}
class RcdbQueryParamsBuilder {
// TODO: Make way for single-valued params as well
private val params: MutableMap<String, List<RcdbParamOption<Any>>> = mutableMapOf()
class MultiParams(override val params: Map<String, List<String>>) : Params<List<String>>()
class SingleParams(override val params: Map<String, String>) : Params<String>()
interface ParamsBuilder<out T, in U> {
fun build(): T
operator fun set(paramName: String, options: U)
}
class MultiParamsBuilder : ParamsBuilder<MultiParams, List<RcdbParamOption<Any>>> {
private val multiParams: MutableMap<String, List<RcdbParamOption<Any>>> = mutableMapOf()
/**
* Creates the Query Params from this builder.
* If no Search type has been set, this will default to "Coaster"
*/
fun build() = params.apply {
override fun build() = multiParams.apply {
putIfAbsent(SearchType.staticParamName, listOf(SearchType.COASTER))
}
.filter { it.value.isNotEmpty() }
.mapValues { e -> e.value.map { o -> o.paramValue.toString() } }
.let { RcdbQueryParams(it) }
.let { MultiParams(it) }
operator fun set(paramName: String, options: List<RcdbParamOption<Any>>) {
this.params[paramName] = options
override operator fun set(paramName: String, options: List<RcdbParamOption<Any>>) {
this.multiParams[paramName] = options
}
operator fun get(paramName: String) = this.params[paramName]
operator fun get(paramName: String) = this.multiParams[paramName]
fun keys() = this.params.keys.toSet()
fun keys() = this.multiParams.keys.toSet()
}
fun rcdbQuery(body: RcdbQueryParamsBuilder.() -> RcdbQueryParamsBuilder) =
fun rcdbQuery(body: MultiParamsBuilder.() -> MultiParamsBuilder) =
RcdbUrlQuery("https://www.rcdb.com/r.htm?", rcdbQueryParams(body)).toString()
data class RcdbUrlQuery(val baseUrl: String, val params: RcdbQueryParams) {
data class RcdbUrlQuery(val baseUrl: String, val params: MultiParams) {
override fun toString(): String {
return params.params
.asSequence()
@ -46,8 +56,8 @@ data class RcdbUrlQuery(val baseUrl: String, val params: RcdbQueryParams) {
/**
* Builder for the parameters only (mainly because it might be possilbe in the future to have an API with a POST search object?
*/
fun rcdbQueryParams(body: RcdbQueryParamsBuilder.() -> RcdbQueryParamsBuilder): RcdbQueryParams {
val builder = RcdbQueryParamsBuilder()
fun rcdbQueryParams(body: MultiParamsBuilder.() -> MultiParamsBuilder): MultiParams {
val builder = MultiParamsBuilder()
builder.body()
return builder.build()
}