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

io.specmatic.core.pattern.DatePattern.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.pattern

import io.specmatic.core.Resolver
import io.specmatic.core.Result
import io.specmatic.core.pattern.config.NegativePatternConfiguration
import io.specmatic.core.value.JSONArrayValue
import io.specmatic.core.value.StringValue
import io.specmatic.core.value.Value

object DatePattern : Pattern, ScalarType {
    override fun matches(sampleData: Value?, resolver: Resolver): Result = when (sampleData) {
        is StringValue -> resultOf {
            parse(sampleData.string, resolver)
            Result.Success()
        }
        else -> Result.Failure("Date types can only be represented using strings")
    }

    override fun generate(resolver: Resolver): StringValue = StringValue(RFC3339.currentDate())

    override fun newBasedOn(row: Row, resolver: Resolver): Sequence> = sequenceOf(HasValue(this))

    override fun newBasedOn(resolver: Resolver): Sequence = sequenceOf(this)

    override fun negativeBasedOn(row: Row, resolver: Resolver, config: NegativePatternConfiguration): Sequence> {
        return scalarAnnotation(this, sequenceOf(NullPattern))
    }

    override fun parse(value: String, resolver: Resolver): StringValue =
        attempt {
            RFC3339.dateFormatter.parse(value)
            StringValue(value)
        }

    override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
        return encompasses(this, otherPattern, thisResolver, otherResolver, typeStack)
    }

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

    override val typeAlias: String?
        get() = null

    override val typeName: String = "date"

    override val pattern = "(date)"

    override fun toString() = pattern
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy