Cleanup after getting rid of Mulitplatform.

This commit is contained in:
Oliver Rümpelein 2020-04-19 13:18:17 +02:00
parent 6f028d6ec6
commit c01752b5d9
13 changed files with 131 additions and 142 deletions

View file

@ -8,7 +8,7 @@ fun main() {
}
fun generateQueryUrl() = rcdbQuery {
searchFor(SearchType.COASTER)
searchType(SearchType.COASTER)
searchTerm("Dragon")
vendors {
vekoma()

View file

@ -1,5 +1,8 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
@Suppress("unused")
enum class Order(
override val prettyName: String,
override val fullName: String,
@ -52,3 +55,10 @@ enum class Order(
const val staticParamName = "order"
}
}
fun RcdbQueryParamsBuilder.sortBy(body: OrderBuilder.() -> OrderBuilder): RcdbQueryParamsBuilder {
val builder = OrderBuilder()
builder.body()
this[Order.staticParamName] = builder.build()
return this
}

View file

@ -5,3 +5,4 @@ interface RcdbParamOption<out T> {
val fullName: String
val paramValue: T
}

View file

@ -0,0 +1,38 @@
package de.pheerai.rcdbquery.dataMappings
// TODO: Find way to only have one generic parameter
sealed class RcdbParamOptionBuilder<out U : Any, T : RcdbParamOption<U>> {
private val paramList: MutableList<T> = mutableListOf()
open fun add(param: T): RcdbParamOptionBuilder<U, T> {
paramList.add(param)
return this
}
fun build() = paramList.toList()
}
class VendorBuilder : RcdbParamOptionBuilder<Int, Vendor>() {
override fun add(param: Vendor): VendorBuilder {
super.add(param)
return this
}
fun of(vendor: Vendor) = this.add(vendor)
fun vekoma() = this.add(Vendor.VEKOMA)
fun intamin() = this.add(Vendor.INTAMIN)
fun mack() = this.add(Vendor.MACK)
fun bandm() = this.add(Vendor.B_AND_M)
fun gerstlauer() = this.add(Vendor.GERSTLAUER)
}
class OrderBuilder : RcdbParamOptionBuilder<Int, Order>() {
override fun add(param: Order): OrderBuilder {
super.add(param)
return this
}
fun of(order: Order) = this.add(order)
fun manufacturer() = this.add(Order.MANUFACTURER)
fun inversion() = this.add(Order.INVERSIONS)
}

View file

@ -1,2 +0,0 @@
package de.pheerai.rcdbquery.dataMappings

View file

@ -1,5 +1,7 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
class SearchTerm(override val paramValue: String): RcdbParamOption<String> {
override val prettyName = "Search Term"
override val fullName = "Search for elements whose name contain this term"
@ -7,3 +9,17 @@ class SearchTerm(override val paramValue: String): RcdbParamOption<String> {
const val staticParamName = "nc"
}
}
fun RcdbQueryParamsBuilder.searchTerm(term: String) {
if (SearchTerm.staticParamName !in this.keys()) {
this[SearchTerm.staticParamName] = listOf(
SearchTerm(term)
)
} else {
error(
"""Only one search term must ever be set
| Old term: ${this[SearchTerm.staticParamName]!![0].paramValue}
| New term: $term""".trimMargin()
)
}
}

View file

@ -1,5 +1,8 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
@Suppress("unused")
enum class SearchType(
override val prettyName: String,
override val fullName: String,
@ -15,3 +18,15 @@ enum class SearchType(
const val staticParamName = "ot"
}
}
fun RcdbQueryParamsBuilder.searchType(searchType: SearchType) {
if (SearchType.staticParamName !in this.keys()) {
this[SearchType.staticParamName] = listOf(searchType)
} else {
error(
"""Only one search type must ever be set
| Old type: ${this[SearchType.staticParamName]!![0].prettyName}
| New type: ${searchType.prettyName}""".trimMargin()
)
}
}

View file

@ -1,5 +1,8 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
@Suppress("unused")
enum class Vendor(
override val prettyName: String,
override val fullName: String,
@ -223,9 +226,6 @@ enum class Vendor(
YEISK_PLANT_ATTRACTION("Yeisk Plant Attraction", 18040),
HEBEI_H_AND_S_AMUSEMENT_EQUIPMENT("Hebei H&S Amusement Equipment Co, LTD.", 18205),
GOLDEN_DRAGON_AMUSEMENT("Golden Dragon Amusement Group", 18224),
INTAMIN("Intamin", "Intamin Amusement Rides", 6837),
VEKOMA("Vekoma", 6836),
MACK("Mack Rides", "Mack Rides GmbH & Co KG", 6856),
@ -253,3 +253,10 @@ enum class Vendor(
}
}
}
fun RcdbQueryParamsBuilder.vendors(body: VendorBuilder.() -> VendorBuilder): RcdbQueryParamsBuilder {
val builder = VendorBuilder()
builder.body()
this[Vendor.staticParamName] = builder.build()
return this
}

View file

@ -0,0 +1,20 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dsl.RcdbQueryParamsBuilder
//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 =
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")
@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(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

@ -1,19 +1,25 @@
package de.pheerai.rcdbquery.dsl
import de.pheerai.rcdbquery.dataMappings.*
import de.pheerai.rcdbquery.dataMappings.RcdbParamOption
import de.pheerai.rcdbquery.dataMappings.SearchType
inline class RcdbQueryParams(val params: Map<String, List<String>>)
class RcdbQueryParams(val params: Map<String, List<String>>)
class RcdbQueryParamsBuilder {
// TODO: Make way for single-valued params as well
private val params: MutableMap<String, List<RcdbParamOption<Any>>> = mutableMapOf()
// TODO: Validate that search type is set
fun build() = RcdbQueryParams(
params
.filter { it.value.isNotEmpty() }
.mapValues { e -> e.value.map { o -> o.paramValue.toString() } }
)
/**
* Creates the Query Params from this builder.
* If no Search type has been set, this will default to "Coaster"
*/
fun build() = params.apply {
putIfAbsent(SearchType.staticParamName, listOf(SearchType.COASTER))
}
.filter { it.value.isNotEmpty() }
.mapValues { e -> e.value.map { o -> o.paramValue.toString() } }
.let { RcdbQueryParams(it) }
operator fun set(paramName: String, options: List<RcdbParamOption<Any>>) {
this.params[paramName] = options
@ -21,9 +27,6 @@ class RcdbQueryParamsBuilder {
operator fun get(paramName: String) = this.params[paramName]
private fun List<RcdbParamOption<Any>>?.append(options: List<RcdbParamOption<Any>>) =
this?.toMutableList()?.also { it.addAll(options) } ?: options
fun keys() = this.params.keys.toSet()
}
@ -40,110 +43,12 @@ 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()
builder.body()
return builder.build()
}
// TODO: Find way to only have one generic parameter
sealed class RcdbParamOptionBuilder<out U : Any, T : RcdbParamOption<U>> {
private val paramList: MutableList<T> = mutableListOf()
open fun add(param: T): RcdbParamOptionBuilder<U, T> {
paramList.add(param)
return this
}
fun build() = paramList.toList()
}
//region Vendors
class VendorBuilder : RcdbParamOptionBuilder<Int, Vendor>() {
override fun add(param: Vendor): VendorBuilder {
super.add(param)
return this
}
fun of(vendor: Vendor) = this.add(vendor)
fun vekoma() = this.add(Vendor.VEKOMA)
fun intamin() = this.add(Vendor.INTAMIN)
fun mack() = this.add(Vendor.MACK)
fun bandm() = this.add(Vendor.B_AND_M)
fun gerstlauer() = this.add(Vendor.GERSTLAUER)
}
fun RcdbQueryParamsBuilder.vendors(body: VendorBuilder.() -> VendorBuilder): RcdbQueryParamsBuilder {
val builder = VendorBuilder()
builder.body()
this[Vendor.staticParamName] = builder.build()
return this
}
//endregion
//region Order
class OrderBuilder : RcdbParamOptionBuilder<Int, Order>() {
override fun add(param: Order): OrderBuilder {
super.add(param)
return this
}
fun of(order: Order) = this.add(order)
fun manufacturer() = this.add(Order.MANUFACTURER)
fun inversion() = this.add(Order.INVERSIONS)
}
fun RcdbQueryParamsBuilder.sortBy(body: OrderBuilder.() -> OrderBuilder): RcdbQueryParamsBuilder {
val builder = OrderBuilder()
builder.body()
this[Order.staticParamName] = builder.build()
return this
}
//endregion
//region Search type
fun RcdbQueryParamsBuilder.searchFor(searchType: SearchType) {
if (SearchType.staticParamName !in this.keys()) {
this[SearchType.staticParamName] = listOf(searchType)
} else {
error(
"""Only one search type must ever be set
| Old type: ${this[SearchType.staticParamName]!![0].prettyName}
| New type: ${searchType.prettyName}""".trimMargin()
)
}
}
//endregion
//region Search Term
fun RcdbQueryParamsBuilder.searchTerm(term: String) {
if (SearchTerm.staticParamName !in this.keys()) {
this[SearchTerm.staticParamName] = listOf(SearchTerm(term))
} else {
error(
"""Only one search term must ever be set
| Old term: ${this[SearchTerm.staticParamName]!![0].paramValue}
| New term: $term""".trimMargin()
)
}
}
//endregion
//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 =
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")
@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(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)")
//endregion

View file

@ -1,11 +0,0 @@
package de.pheerai.rcdbquery
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTests {
@Test
fun testMe() {
assertTrue(true)
}
}

View file

@ -1,11 +0,0 @@
package de.pheerai.rcdbquery
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTestsJVM {
@Test
fun testHello() {
assertTrue(true)
}
}

View file

@ -1,7 +1,8 @@
package de.pheerai.rcdbquery.dsl
import de.pheerai.rcdbquery.dataMappings.Order
import de.pheerai.rcdbquery.dataMappings.Vendor
import de.pheerai.rcdbquery.dataMappings.sortBy
import de.pheerai.rcdbquery.dataMappings.vendors
import kotlin.test.Test
import kotlin.test.assertEquals
@ -9,7 +10,7 @@ class RcdbQueryDslTest {
@Test
fun testQueryParamBuilder() {
assertEquals(
"https://www.rcdb.com/r.htm?mk=6836,6856,6905&order=24,30",
"https://www.rcdb.com/r.htm?mk=6836,6856,6905&order=24,30&ot=2",
rcdbQuery {
vendors {
vekoma()