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

in.specmatic.core.pattern.QueryParameterScalarPattern.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends. Deprecation Notice for group ID "in.specmatic" ****************************************************************************************************** Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic". Please update your dependencies to use the new group ID "io.specmatic". ******************************************************************************************************

There is a newer version: 1.3.39
Show newest version
package `in`.specmatic.core.pattern

import `in`.specmatic.core.Resolver
import `in`.specmatic.core.Result
import `in`.specmatic.core.utilities.exceptionCauseMessage
import `in`.specmatic.core.value.*

data class QueryParameterScalarPattern(override val pattern: Pattern): Pattern by pattern {
    override fun matches(sampleData: Value?, resolver: Resolver): Result {
        if(sampleData == null){
            return Result.Failure("Null found, expected a scalar value")
        }
        val sampleDataString = when (sampleData) {
            is ListValue -> {
                if (sampleData.list.size > 1) return Result.Failure("Multiple values $sampleData found. Expected a single value")
                sampleData.list.single().toStringLiteral()
            }
            else -> sampleData.toStringLiteral()
        }

        return try {
            val parsedValue = if(isPatternToken(sampleDataString)) {
                StringValue(sampleDataString)
            }
            else {
                pattern.parse(sampleDataString, resolver)
            }
            resolver.matchesPattern(null, pattern, parsedValue)
        } catch (e: Throwable) {
            Result.Failure(exceptionCauseMessage(e))
        }
    }

    override fun generate(resolver: Resolver): Value {
        return pattern.generate(resolver)
    }

    override fun parse(value: String, resolver: Resolver): Value {
        return  pattern.parse(value, resolver)
    }

    override fun encompasses(
        otherPattern: Pattern,
        thisResolver: Resolver,
        otherResolver: Resolver,
        typeStack: TypeStack
    ): Result {
        if(otherPattern !is QueryParameterScalarPattern)
            return Result.Failure(thisResolver.mismatchMessages.mismatchMessage(this.typeName, otherPattern.typeName))

        return this.pattern.encompasses(otherPattern.pattern, thisResolver, otherResolver, typeStack)
    }

    override fun listOf(valueList: List, resolver: Resolver): Value {
        return JSONArrayValue(valueList)
    }

    override val typeAlias: String?
        get() = null

    override val typeName: String
        get() = "(queryParameterScalar/${pattern.typeName})"

    override fun parseToType(valueString: String, resolver: Resolver): Pattern {
        return pattern.parse(valueString, resolver).exactMatchElseType()
    }

    override fun equals(other: Any?): Boolean {
        if (other !is QueryParameterScalarPattern) return false
        return pattern == other.pattern
    }

    override fun hashCode(): Int {
        return pattern.hashCode()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy