All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.specmatic.core.QueryParameters.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.

There is a newer version: 2.0.37
Show newest version
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 substituteDictionaryValues(dictionary: Dictionary, forceSubstitution: Boolean = false): QueryParameters {
        val updatedQueryParameters = dictionary.substituteDictionaryValues(listOfPairsToMap(this.paramPairs), forceSubstitution)
        return this.copy(paramPairs = mapToListOfPairs(updatedQueryParameters))
    }
}

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 listOfPairsToMap(inputList: List>): Map {
    return inputList.toMap()
}

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("]")




© 2015 - 2024 Weber Informatics LLC | Privacy Policy