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

in.specmatic.core.URLPathSegmentPattern.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

import `in`.specmatic.core.pattern.*
import `in`.specmatic.core.value.JSONArrayValue
import `in`.specmatic.core.value.NullValue
import `in`.specmatic.core.value.StringValue
import `in`.specmatic.core.value.Value

data class URLPathSegmentPattern(override val pattern: Pattern, override val key: String? = null, override val typeAlias: String? = null) : Pattern, Keyed {
    override fun matches(sampleData: Value?, resolver: Resolver): Result =
            resolver.matchesPattern(key, pattern, sampleData ?: NullValue)

    override fun generate(resolver: Resolver): Value {
        return resolver.withCyclePrevention(pattern) { cyclePreventedResolver ->
            if (key != null)
                cyclePreventedResolver.generate(key, pattern)
            else pattern.generate(cyclePreventedResolver)
        }
    }

    override fun newBasedOn(row: Row, resolver: Resolver): Sequence =
        resolver.withCyclePrevention(pattern) { cyclePreventedResolver ->
            pattern.newBasedOn(row, cyclePreventedResolver).map { URLPathSegmentPattern(it, key) }
        }

    override fun newBasedOn(resolver: Resolver): Sequence =
        resolver.withCyclePrevention(pattern) { cyclePreventedResolver ->
            pattern.newBasedOn(cyclePreventedResolver).map { URLPathSegmentPattern(it, key) }
        }

    override fun negativeBasedOn(row: Row, resolver: Resolver): Sequence> {
        if(pattern is ExactValuePattern)
            return emptySequence()

        if(pattern is StringPattern)
            return emptySequence()

        return resolver.withCyclePrevention(pattern) { cyclePreventedResolver ->
            pattern.negativeBasedOn(row, cyclePreventedResolver).map { it.breadCrumb(key).breadCrumb("PATH") }
                .filterValueIsNot {
                    it is NullPattern
                }.map {
                    it.ifValue {  URLPathSegmentPattern(it, key) }
                }
        }
    }

    override fun parse(value: String, resolver: Resolver): Value = pattern.parse(value, resolver)

    override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
        if(otherPattern !is URLPathSegmentPattern)
            return Result.Failure("Expected url type, got ${otherPattern.typeName}")

        return otherPattern.pattern.fitsWithin(patternSet(thisResolver), otherResolver, thisResolver, typeStack)
    }

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

    fun tryParse(token: String, resolver: Resolver): Value {
        return try {
            this.pattern.parse(token, resolver)
        } catch (e: Throwable) {
            if (isPatternToken(token) && token.contains(":"))
                StringValue(withPatternDelimiters(withoutPatternDelimiters(token).split(":".toRegex(), 2)[1]))
            else
                StringValue(token)
        }
    }

    override val typeName: String = "url path"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy