Make DSL simpler.

- Still needs some fixes, e.g. how to avoid confusion (property setter/getter?)
This commit is contained in:
Oliver Rümpelein 2020-08-23 23:11:41 +02:00
parent 72ea481bfd
commit 6d37da56b6
38 changed files with 202 additions and 239 deletions

View File

@ -1,5 +1,15 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
extra.apply {
set("kotestVersion", "4.2.0")
set("kotlinApiVersion", "1.4")
set("javaVersion", JavaVersion.VERSION_11)
}
}
inline fun <reified T> getFromExtra(prop: String) = project.extra[prop] as T
// TODO: Documentation
// TODO: Source Jar
// TODO: Doc Jar
@ -13,29 +23,36 @@ repositories {
mavenCentral()
}
project.version = "0.0.1"
project.version = "0.0.2"
project.group = "de.pheerai.rcdb"
java {
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = getFromExtra("javaVersion")
sourceCompatibility = getFromExtra("javaVersion")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation("io.kotest:kotest-runner-junit5-jvm:4.0.5")
testImplementation("io.kotest:kotest-assertions-core-jvm:4.0.5")
testImplementation(
group = "io.kotest",
name = "kotest-runner-junit5-jvm",
version = getFromExtra("kotestVersion")
)
testImplementation(
group = "io.kotest",
name = "kotest-assertions-core-jvm",
version = getFromExtra("kotestVersion")
)
}
tasks {
withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs = listOf("-XXLanguage:+InlineClasses")
apiVersion = "1.3"
languageVersion = "1.3"
jvmTarget = JavaVersion.VERSION_11.toString()
// freeCompilerArgs = listOf("-XXLanguage:+InlineClasses")
apiVersion = getFromExtra("kotlinApiVersion")
languageVersion = getFromExtra("kotlinApiVersion")
jvmTarget = getFromExtra<JavaVersion>("javaVersion").toString()
}
}
@ -48,7 +65,7 @@ publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group as String
artifactId = project.name as String
artifactId = project.name
version = project.version as String
from(components["java"])

Binary file not shown.

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

2
gradlew vendored
View File

@ -82,6 +82,7 @@ esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@ -129,6 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath

1
gradlew.bat vendored
View File

@ -84,6 +84,7 @@ set CMD_LINE_ARGS=%*
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

View File

@ -1,6 +1,6 @@
pluginManagement {
plugins {
kotlin("jvm") version "1.3.72"
kotlin("jvm") version "1.4.0"
}
}

View File

@ -1,11 +1,12 @@
package de.pheerai.rcdbquery
import de.pheerai.rcdbquery.dataMappings.order.Order
import de.pheerai.rcdbquery.dataMappings.order.sortBy
import de.pheerai.rcdbquery.dataMappings.page.page
import de.pheerai.rcdbquery.dataMappings.searchTerm.searchTerm
import de.pheerai.rcdbquery.dataMappings.searchType.SearchType
import de.pheerai.rcdbquery.dataMappings.searchType.searchType
import de.pheerai.rcdbquery.dataMappings.vendor.Vendor
import de.pheerai.rcdbquery.dataMappings.vendor.Vendor.*
import de.pheerai.rcdbquery.dataMappings.vendor.vendors
import de.pheerai.rcdbquery.dsl.rcdbQuery
@ -18,14 +19,13 @@ fun generateNameQueryUrl() = rcdbQuery {
searchType(SearchType.COASTER)
searchTerm("Dragon")
vendors {
vekoma()
intamin()
mack()
of(Vendor.PRESTON_AND_BARBIERI)
VEKOMA()
INTAMIN()
MACK()
}
sortBy {
manufacturer()
inversions()
Order.MANUFACTURER()
Order.INVERSIONS()
}
}
@ -33,6 +33,6 @@ fun generatePageQueryUrl() = rcdbQuery {
page(3)
searchType(SearchType.COASTER)
vendors {
intamin()
INTAMIN()
}
}

View File

@ -2,36 +2,11 @@ package de.pheerai.rcdbquery.dataMappings.category
import de.pheerai.rcdbquery.dataMappings.internal.MultiParamBuilder
class CategoryBuilder: MultiParamBuilder<Long, Category>() {
class CategoryBuilder : MultiParamBuilder<Long, Category>() {
override fun add(param: Category): CategoryBuilder {
super.add(param)
return this
}
fun fourthDimension() = this.add(Category.FOURTH_DIMENSION)
fun brakeman() = this.add(Category.BRAKEMAN)
fun builtInhouse() = this.add(Category.BUILT_INHOUSE)
fun darkride() = this.add(Category.DARK_RIDE)
fun dualStation() = this.add(Category.DUAL_STATION)
fun enclosed() = this.add(Category.ENCLOSED)
fun floorless() = this.add(Category.FLOORLESS)
fun hybrid() = this.add(Category.HYBRID)
fun indoor() = this.add(Category.INDOOR)
fun mirror() = this.add(Category.MIRROR)
fun moebius() = this.add(Category.MOEBIUS)
fun onboardSound() = this.add(Category.ONBOARD_SOUND)
fun pendulum() = this.add(Category.PENDULUM)
fun quasiMoebius() = this.add(Category.QUASI_MOEBIUS)
fun rockingCars() = this.add(Category.ROCKING_CARS)
fun scenicRailway() = this.add(Category.SCENIC_RAILWAY)
fun shuttle() = this.add(Category.SHUTTLE)
fun sideFriction() = this.add(Category.SIDE_FRICTION)
fun singleRail() = this.add(Category.SINGLE_RAIL)
fun slidingStation() = this.add(Category.SLIDING_STATION)
fun spinningCars() = this.add(Category.SPINNING_CARS)
fun stackedStorage() = this.add(Category.STACKED_STORAGE)
fun turntableStation() = this.add(Category.TURNTABLE_STATION)
fun twin() = this.add(Category.TWIN)
fun virtualReality() = this.add(Category.VIRTUAL_REALITY)
fun waterCoaster() = this.add(Category.WATER_COASTER)
override fun Category.invoke() = add(this)
}

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.category
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.category(body: CategoryBuilder.() -> CategoryBuilder): ParamsCollector {
val builder = CategoryBuilder()

View File

@ -8,7 +8,5 @@ class ClassificationBuilder : MultiParamBuilder<Long, Classification>() {
return this
}
fun rollerCoaster() = this.add(Classification.ROLLER_COASTER)
fun poweredCoaster() = this.add(Classification.POWERED_COASTER)
fun mountainCoaster() = this.add(Classification.MOUNTAIN_COASTER)
override fun Classification.invoke() = add(this)
}

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.classification
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.classification(body: ClassificationBuilder.() -> ClassificationBuilder): ParamsCollector {
val builder = ClassificationBuilder()

View File

@ -8,12 +8,5 @@ class DesignBuilder : MultiParamBuilder<Long, Design>() {
return this
}
fun sitDown() = Design.SIT_DOWN
fun inverted() = Design.INVERTED
fun suspended() = Design.SUSPENDED
fun wing() = Design.WING
fun flying() = Design.FLYING
fun standUp() = Design.STAND_UP
fun bobsled() = Design.BOBSLED
fun pipeline() = Design.PIPELINE
override fun Design.invoke() = add(this)
}

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.design
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.design(body: DesignBuilder.() -> DesignBuilder): ParamsCollector {
val builder = DesignBuilder()

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.internal
open class MultiParamBuilder<out U : Any, T : RcdbParamOption<U>> {
abstract class MultiParamBuilder<out U : Any, T : RcdbParamOption<U>> {
private val paramList: MutableList<T> = mutableListOf()
open fun add(param: T): MultiParamBuilder<U, T> {
@ -19,5 +19,6 @@ open class MultiParamBuilder<out U : Any, T : RcdbParamOption<U>> {
}
fun build() = paramList.toList()
}
abstract operator fun T.invoke(): MultiParamBuilder<U, T>
}

View File

@ -8,32 +8,5 @@ class LayoutBuilder : MultiParamBuilder<Long, Layout>() {
return this
}
fun bigApple() = this.add(Layout.BIG_APPLE)
fun wackyWorm() = this.add(Layout.BIG_APPLE)
fun circleDip() = this.add(Layout.CIRCLE_DIP)
fun circularGravityRailway() = this.add(Layout.CIRCULAR_GRAVIY_RAILWAY)
fun cyclone() = this.add(Layout.CYCLONE)
fun doubleFigureEight() = this.add(Layout.DOUBLE_FIGURE_EIGHT)
fun doubleOutAndBack() = this.add(Layout.DOUBLE_OUT_AND_BACK)
fun figureEight() = this.add(Layout.FIGURE_EIGHT)
fun jungleMouse() = this.add(Layout.JUNGLE_MOUSE)
fun lShapedOutAndBack() = this.add(Layout.L_SHAPED_OUT_AND_BACK)
fun miteMouse() = this.add(Layout.MITE_MOUSE)
fun outAndBack() = this.add(Layout.OUT_AND_BACK)
fun oval() = this.add(Layout.OVAL)
fun shuttleLoop() = this.add(Layout.SHUTTLE_LOOP)
fun singleHelixCenter() = this.add(Layout.SINGLE_HELIX_CENTER)
fun singleHelixLeft() = this.add(Layout.SINGLE_HELIX_LEFT)
fun singleHelixRear() = this.add(Layout.SINGLE_HELIX_REAR)
fun singleHelixRight() = this.add(Layout.SINGLE_HELIX_RIGHT)
fun terrain() = this.add(Layout.TERRAIN)
fun triangle() = this.add(Layout.TRIANGLE)
fun tripleOutAndBack() = this.add(Layout.TRIPLE_OUT_AND_BACK)
fun twinHelix() = this.add(Layout.TWIN_HELIX)
fun twister() = this.add(Layout.TWISTER)
fun uShuttle() = this.add(Layout.U_SHUTTLE)
fun wildMouse() = this.add(Layout.WILD_MOUSE)
fun zyklon() = this.add(Layout.ZYKLON)
fun galaxy() = this.add(Layout.ZYKLON)
override operator fun Layout.invoke() = add(this)
}

View File

@ -1,10 +1,12 @@
package de.pheerai.rcdbquery.dataMappings.layout
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.layout(body: LayoutBuilder.() -> LayoutBuilder): ParamsCollector {
val builder = LayoutBuilder()
builder.body()
with(Layout) {
builder.body()
}
this[Layout.paramKey] = builder.build()
return this
}

View File

@ -18,30 +18,6 @@ class OrderBuilder : MultiParamBuilder<Long, Order>() {
return this
}
fun of(order: Order) = this.add(order)
fun manufacturer() = this.add(Order.MANUFACTURER)
fun inversions() = this.add(Order.INVERSIONS)
fun name() = this.add(Order.NAME)
fun location() = this.add(Order.LOCATION)
fun park() = this.add(Order.PARK)
fun opened() = this.add(Order.OPENED)
fun closed() = this.add(Order.CLOSED)
fun entered() = this.add(Order.ENTERED)
fun type() = this.add(Order.TYPE)
fun design() = this.add(Order.DESIGN)
fun classification() = this.add(Order.CLASSIFICATION)
fun layout() = this.add(Order.LAYOUT)
fun model() = this.add(Order.MODEL)
fun modelLine() = this.add(Order.MODEL_LINE)
fun speed() = this.add(Order.SPEED)
fun height() = this.add(Order.HEIGHT)
fun drop() = this.add(Order.DROP)
fun length() = this.add(Order.LENGTH)
fun angle() = this.add(Order.ANGLE)
fun serialNo() = this.add(Order.SERIAL_NO)
fun scale() = this.add(Order.SCALE)
fun rideTime() = this.add(Order.RIDE_TIME)
fun state() = this.add(Order.STATUS)
fun closing() = this.add(Order.CLOSING)
override fun Order.invoke() = add(this)
fun allRelevant() = this.addAll(Order.values().filter { it.relevantForAll })
}

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.order
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.sortBy(body: OrderBuilder.() -> OrderBuilder): ParamsCollector {
val builder = OrderBuilder()

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.page
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.page(page: Long) = also {
this[Page.paramKey] =

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.searchTerm
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.searchTerm(term: String) = also {
it[SearchTerm.paramKey] = listOf(

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.searchType
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.searchType(searchType: SearchType) = also {
it[SearchType.paramKey] = searchType

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.startsWith
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.startsWith(term: String) = also {
this[StartsWith.paramKey] =

View File

@ -8,8 +8,5 @@ class StatusBuilder : MultiParamBuilder<Long, Status>() {
return this
}
fun sbno() = this.add(Status.SBNO)
fun operating() = this.add(Status.OPERATING)
fun underConstruction() = this.add(Status.UNDER_CONSTRUCTION)
fun stored() = this.add(Status.STORED)
override fun Status.invoke() = add(this)
}

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.status
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.status(body: StatusBuilder.() -> StatusBuilder): ParamsCollector {
val builder = StatusBuilder()

View File

@ -8,8 +8,5 @@ class ThrillBuilder : MultiParamBuilder<Long, Thrill>() {
return this
}
fun kiddie() = this.add(Thrill.KIDDIE)
fun family() = this.add(Thrill.FAMILY)
fun thrill() = this.add(Thrill.THRILL)
fun extreme() = this.add(Thrill.EXTREME)
override fun Thrill.invoke() = add(this)
}

View File

@ -1,8 +1,8 @@
package de.pheerai.rcdbquery.dataMappings.thrill
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.type(body: ThrillBuilder.() -> ThrillBuilder): ParamsCollector {
fun ParamsCollector.thrill(body: ThrillBuilder.() -> ThrillBuilder): ParamsCollector {
val builder = ThrillBuilder()
builder.body()
this[Thrill.paramKey] = builder.build()

View File

@ -8,6 +8,5 @@ class TypeBuilder : MultiParamBuilder<Long, Type>() {
return this
}
fun steel() = this.add(Type.STEEL)
fun wood() = this.add(Type.WOOD)
override fun Type.invoke() = add(this)
}

View File

@ -1,12 +1,10 @@
package de.pheerai.rcdbquery.dataMappings.type
import de.pheerai.rcdbquery.dataMappings.thrill.Thrill
import de.pheerai.rcdbquery.dataMappings.thrill.ThrillBuilder
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
fun ParamsCollector.thrill(body: ThrillBuilder.() -> ThrillBuilder): ParamsCollector {
val builder = ThrillBuilder()
fun ParamsCollector.type(body: TypeBuilder.() -> TypeBuilder): ParamsCollector {
val builder = TypeBuilder()
builder.body()
this[Thrill.paramKey] = builder.build()
this[Type.paramKey] = builder.build()
return this
}

View File

@ -1,20 +1,23 @@
package de.pheerai.rcdbquery.dataMappings
import de.pheerai.rcdbquery.dataMappings.order.Order
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
const val EXTRA_COLUMNS_ERROR_MESSAGE =
"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)",
EXTRA_COLUMNS_ERROR_MESSAGE,
ReplaceWith("sortBy"), DeprecationLevel.ERROR
)
fun ParamsCollector.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)")
error(EXTRA_COLUMNS_ERROR_MESSAGE)
@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)",
EXTRA_COLUMNS_ERROR_MESSAGE,
ReplaceWith("sortBy"), DeprecationLevel.ERROR
)
fun ParamsCollector.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)")
error(EXTRA_COLUMNS_ERROR_MESSAGE)

View File

@ -8,7 +8,7 @@ import de.pheerai.rcdbquery.dataMappings.internal.StringGeneratable
enum class Vendor(
override val prettyName: String,
override val fullName: String,
override val paramValue: Long
override val paramValue: Long,
) : RcdbParamOption<Long>, RcdbItem {
YAQIAO_MACHINE("Yaqiao", "Yaqiao Machine", 6471),
CREDIBLE("Credible", 6584),
@ -118,7 +118,7 @@ enum class Vendor(
TAKAHASHI_KIKAI_SANGYO("Takahashi Kikai Sangyo", 7472),
DYNAMIC("Dynamic Attractions", 7489),
GOLDEN_HORSE("Golden Horse", 7492),
WIEGAND("Wiegand", "Josef Wiegand GmbH & Co. KG", 7514),
WIEGAND("Wiegand", 7514),
ASCOT("Ascot Design", 7530),
DAL_AMUSEMENT_RIDES("DAL Amusement Rides Company", 7536),
CHILDRESS_COASTER("Childress Coaster", 7550),
@ -255,9 +255,12 @@ enum class Vendor(
fun searchByName(name: String): List<Vendor> {
val searchName = name.toLowerCase()
return values().filter {
it.fullName.toLowerCase().contains(searchName)
}.toList()
return values()
.filter {
it.fullName
.toLowerCase()
.contains(searchName)
}.toList()
}
}
}

View File

@ -8,10 +8,5 @@ class VendorBuilder : MultiParamBuilder<Long, Vendor>() {
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)
override fun Vendor.invoke() = add(this)
}

View File

@ -1,6 +1,6 @@
package de.pheerai.rcdbquery.dataMappings.vendor
import de.pheerai.rcdbquery.dsl.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
// TODO: Try to generify. This is currently lacking some sort of "This interface requires a companion implementing that interface"
fun ParamsCollector.vendors(body: VendorBuilder.() -> VendorBuilder): ParamsCollector {

View File

@ -0,0 +1,48 @@
package de.pheerai.rcdbquery.dsl.params
import de.pheerai.rcdbquery.dataMappings.internal.RcdbParamOption
import de.pheerai.rcdbquery.dataMappings.searchType.SearchType
import de.pheerai.rcdbquery.dsl.RcdbParams
class ParamsCollector {
private val multiParams: MutableMap<String, List<RcdbParamOption<Any>>> = mutableMapOf()
private val singleParams: MutableMap<String, RcdbParamOption<Any>> = mutableMapOf()
fun build() = RcdbParams(buildMulti(), buildSingle())
/**
* Creates the Query Params from this builder.
* If no Search type has been set, this will default to "Coaster"
*/
private fun buildMulti() = multiParams.apply {
// Defaults go here, if any
}
.filter { it.value.isNotEmpty() }
.mapValues { e -> e.value.map { o -> o.paramValue.toString() } }
.let { MultiParams(it) }
private fun buildSingle() = singleParams.apply {
putIfAbsent(SearchType.paramKey, SearchType.COASTER)
}
.mapValues { e -> e.value.paramValue.toString() }
.let { SingleParams(it) }
operator fun set(paramName: String, options: List<RcdbParamOption<Any>>) {
this.multiParams[paramName] = options
}
operator fun set(paramName: String, option: RcdbParamOption<Any>) {
if (paramName !in singleParams && paramName !in multiParams) {
this.singleParams[paramName] = option
} else {
throw ParamAlreadySetException("Parameter $paramName already set.")
}
}
fun getSingle(paramName: String) = this.multiParams[paramName] ?: this.singleParams[paramName]
fun getMulti(paramName: String) = this.multiParams[paramName]
fun keys() = this.multiKeys().plus(singleKeys())
private fun multiKeys() = this.multiParams.keys.toSet()
private fun singleKeys() = this.singleParams.keys.toSet()
}

View File

@ -0,0 +1,8 @@
package de.pheerai.rcdbquery.dsl.params
sealed class BaseParams<T> {
abstract val params: Map<String, T>
}
class MultiParams(override val params: Map<String, List<String>>) : BaseParams<List<String>>()
class SingleParams(override val params: Map<String, String>) : BaseParams<String>()

View File

@ -0,0 +1,3 @@
package de.pheerai.rcdbquery.dsl.params
class ParamAlreadySetException(message: String) : IllegalArgumentException(message)

View File

@ -1,19 +1,19 @@
package de.pheerai.rcdbquery.dsl
import de.pheerai.rcdbquery.dataMappings.internal.RcdbParamOption
import de.pheerai.rcdbquery.dataMappings.searchType.SearchType
// TODO: Restructure
import de.pheerai.rcdbquery.dsl.params.MultiParams
import de.pheerai.rcdbquery.dsl.params.ParamsCollector
import de.pheerai.rcdbquery.dsl.params.SingleParams
data class RcdbParams(
val multiParams: MultiParams,
val singleParams: SingleParams
val singleParams: SingleParams,
) {
fun toStrings(): List<String> {
val multiParamSequence: Sequence<Pair<String, String>> = this.multiParams
.params
.mapValues { it.value.joinToString(separator = ",") }
.map { it.toPair() }
.asSequence()
.map { e -> e.key to e.value.joinToString(separator = ",") }
val singleParamSequence: Sequence<Pair<String, String>> = this.singleParams
.params
.asSequence()
@ -24,58 +24,6 @@ data class RcdbParams(
}
}
sealed class Params<T> {
abstract val params: Map<String, T>
}
class ParamAlreadySetException(message: String) : IllegalArgumentException(message)
class MultiParams(override val params: Map<String, List<String>>) : Params<List<String>>()
class SingleParams(override val params: Map<String, String>) : Params<String>()
class ParamsCollector {
private val multiParams: MutableMap<String, List<RcdbParamOption<Any>>> = mutableMapOf()
private val singleParams: MutableMap<String, RcdbParamOption<Any>> = mutableMapOf()
fun build() = RcdbParams(buildMulti(), buildSingle())
/**
* Creates the Query Params from this builder.
* If no Search type has been set, this will default to "Coaster"
*/
private fun buildMulti() = multiParams.apply {
// Defaults go here, if any
}
.filter { it.value.isNotEmpty() }
.mapValues { e -> e.value.map { o -> o.paramValue.toString() } }
.let { MultiParams(it) }
private fun buildSingle() = singleParams.apply {
putIfAbsent(SearchType.paramKey, SearchType.COASTER)
}
.mapValues { e -> e.value.paramValue.toString() }
.let { SingleParams(it) }
operator fun set(paramName: String, options: List<RcdbParamOption<Any>>) {
this.multiParams[paramName] = options
}
operator fun set(paramName: String, option: RcdbParamOption<Any>) {
if (paramName !in singleParams && paramName !in multiParams) {
this.singleParams[paramName] = option
} else {
throw ParamAlreadySetException("Parameter $paramName already set.")
}
}
fun getSingle(paramName: String) = this.multiParams[paramName] ?: this.singleParams[paramName]
fun getMulti(paramName: String) = this.multiParams[paramName]
fun keys() = this.multiKeys().plus(singleKeys())
private fun multiKeys() = this.multiParams.keys.toSet()
private fun singleKeys() = this.singleParams.keys.toSet()
}
fun rcdbQuery(body: ParamsCollector.() -> ParamsCollector) =
RcdbUrlQuery("https://www.rcdb.com/r.htm?", rcdbQueryParams(body)).toString()

View File

@ -0,0 +1,7 @@
package de.pheerai.rcdbquery
import io.kotest.core.config.AbstractProjectConfig
class TestConfig : AbstractProjectConfig() {
override val globalAssertSoftly = true
}

View File

@ -1,38 +1,57 @@
package de.pheerai.rcdbquery.dsl
import de.pheerai.rcdbquery.dataMappings.vendor.Vendor
import de.pheerai.rcdbquery.dataMappings.order.Order
import de.pheerai.rcdbquery.dataMappings.order.sortBy
import de.pheerai.rcdbquery.dataMappings.searchType.SearchType
import de.pheerai.rcdbquery.dataMappings.searchType.searchType
import de.pheerai.rcdbquery.dataMappings.type.Type
import de.pheerai.rcdbquery.dataMappings.type.type
import de.pheerai.rcdbquery.dataMappings.vendor.Vendor
import de.pheerai.rcdbquery.dataMappings.vendor.vendors
import de.pheerai.rcdbquery.dsl.params.ParamAlreadySetException
import io.kotest.assertions.asClue
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldHaveLength
import org.junit.jupiter.api.assertThrows
class RcdbQueryDslTest : StringSpec({
"should generate a proper URL" {
val actual = rcdbQuery {
rcdbQuery {
vendors {
vekoma()
mack()
of(Vendor.MAURER)
Vendor.VEKOMA()
Vendor.MACK()
Vendor.MAURER()
}
sortBy {
inversions()
manufacturer()
Order.INVERSIONS()
Order.MANUFACTURER()
}
}.asClue {
it shouldHaveLength 61
it shouldContain "https://www.rcdb.com/r.htm?"
it shouldContain "mk="
it shouldContain "6836"
it shouldContain "6856"
it shouldContain "6905"
it shouldContain "order="
it shouldContain "24"
it shouldContain "30"
it shouldContain "ot=2"
it.filter { c -> c == '&' } shouldHaveLength 2
}
}
"Should throw if unique value is reset" {
assertThrows<ParamAlreadySetException> {
rcdbQuery {
searchType(SearchType.COASTER)
type {
Type.WOOD()
}
searchType(SearchType.AMUSEMENT_PARK)
}
}
actual.length shouldBe 61
actual.shouldContain("https://www.rcdb.com/r.htm?")
actual.shouldContain("mk=")
actual.shouldContain("6836")
actual.shouldContain("6856")
actual.shouldContain("6905")
actual.shouldContain("order=")
actual.shouldContain("24")
actual.shouldContain("30")
actual.shouldContain("ot=2")
actual.filter { it == '&' }.length shouldBe 2
}
})