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

run.qontract.core.pattern.DateTimePattern.kt Maven / Gradle / Ivy

Go to download

A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner

There is a newer version: 0.23.1
Show newest version
package run.qontract.core.pattern

import run.qontract.core.Resolver
import run.qontract.core.Result
import run.qontract.core.value.JSONArrayValue
import run.qontract.core.value.StringValue
import run.qontract.core.value.Value
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

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

    override fun generate(resolver: Resolver): StringValue =
            StringValue(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME))

    override fun newBasedOn(row: Row, resolver: Resolver): List = listOf(this)

    override fun parse(value: String, resolver: Resolver): StringValue =
            attempt {
                DateTimeFormatter.ISO_DATE_TIME.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 = "datetime"

    override val pattern = "(datetime)"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy