io.specmatic.core.QueryParameters.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
package io.specmatic.core
import io.specmatic.core.pattern.Pattern
import io.specmatic.core.pattern.parsedJSONArray
import io.specmatic.core.value.JSONArrayValue
import io.specmatic.core.value.Value
import kotlin.collections.Map
data class QueryParameters(val paramPairs: List> = emptyList()) {
constructor(map: Map) : this(mapToListOfPairs(map))
val keys = paramPairs.map { it.first }.toSet()
fun plus(map: Map): QueryParameters {
val newListOfPairs = mapToListOfPairs(map)
return QueryParameters( paramPairs + newListOfPairs)
}
fun plus(pair: Pair): QueryParameters {
val newListOfPairs = pairToListOfPairs(pair)
return QueryParameters( paramPairs + newListOfPairs)
}
fun minus(name: String): QueryParameters {
return QueryParameters( paramPairs.filterNot { it.first == name })
}
fun asMap(): Map {
return paramPairs.groupBy { it.first }.map { (parameterName, parameterValues) ->
if (parameterValues.size > 1) {
parameterName to parameterValues.map { it.second }.toString()
} else {
parameterName to parameterValues.single().second
}
}.toMap()
}
fun getValues(key: String): List {
return paramPairs.filter { it.first == key }.map { it.second }
}
fun containsKey(key: String): Boolean {
return paramPairs.any { it.first == key }
}
fun isNotEmpty(): Boolean {
return paramPairs.isNotEmpty()
}
fun containsEntry(key:String, value: String): Boolean {
return paramPairs.any { it.first == key && it.second == value }
}
fun getOrElse(key: String, defaultValue: () -> String): String {
return when {
containsKey(key) -> {
getValues(key).first()
}
else -> defaultValue()
}
}
fun getOrDefault(key: String, defaultValue: String): String {
return when {
containsKey(key) -> {
getValues(key).first()
}
else -> defaultValue
}
}
fun toLine(): String {
return paramPairs.joinToString(" "){ (key, value) ->
"$key=$value"
}
}
fun withoutMatching(patternKeys: Set, additionalProperties: Pattern, resolver: Resolver): QueryParameters {
return QueryParameters(paramPairs.filterNot { (key, value) ->
if(key in patternKeys)
return@filterNot false
try {
val parsedValue: Value = additionalProperties.parse(value, resolver)
additionalProperties.matches(parsedValue, resolver) is Result.Success
} catch(e: Throwable) {
false
}
})
}
}
fun paramPairsExpanded(inputList: List>): List> {
return inputList.flatMap { (key, value) ->
toListOfPairs(value, key)
}
}
fun mapToListOfPairs(inputMap: Map): List> {
return inputMap.flatMap { (key, value) ->
toListOfPairs(value, key)
}
}
fun pairToListOfPairs(pair: Pair): List> {
val key = pair.first
val value = pair.second
return toListOfPairs(value, key)
}
private fun toListOfPairs(
value: String,
key: String
): List> {
return if (isJsonArrayString(value)) {
convertJsonArrayStringToListOfPairs(value, key)
} else {
listOf(key to value)
}
}
private fun convertJsonArrayStringToListOfPairs(
value: String,
key: String
) = parsedJSONArray(value)
.list
.map { valueItem ->
key to valueItem.toString().trim()
}
private fun isJsonArrayString(value: String) = value.startsWith("[") && value.endsWith("]")