in.specmatic.core.pattern.DateTimePattern.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
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".
******************************************************************************************************
package `in`.specmatic.core.pattern
import `in`.specmatic.core.Resolver
import `in`.specmatic.core.Result
import `in`.specmatic.core.value.JSONArrayValue
import `in`.specmatic.core.value.StringValue
import `in`.specmatic.core.value.Value
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(RFC3339.currentDateTime())
override fun newBasedOn(row: Row, resolver: Resolver): Sequence = sequenceOf(this)
override fun newBasedOn(resolver: Resolver): Sequence = sequenceOf(this)
override fun negativeBasedOn(row: Row, resolver: Resolver): Sequence> {
return scalarAnnotation(this, sequenceOf(NullPattern))
}
override fun parse(value: String, resolver: Resolver): StringValue =
attempt {
RFC3339.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)"
override fun toString() = pattern
}