org.http4k.contract.openapi.v3.values4kExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4k-contract Show documentation
Show all versions of http4k-contract Show documentation
http4k typesafe HTTP contracts and OpenApi support
package org.http4k.contract.openapi.v3
import dev.forkhandles.values.Value
import org.http4k.core.Uri
import java.time.Instant
import java.time.LocalDate
import java.util.UUID
import kotlin.reflect.full.declaredMembers
/**
* Set format values for OpenApi descriptions for fields of this type
*/
object Values4kFieldMetadataRetrievalStrategy : FieldMetadataRetrievalStrategy {
override fun invoke(target: Any, fieldName: String): FieldMetadata {
val value = target::class.declaredMembers.find { it.name == fieldName }?.call(target)
return when {
value.isAValue() -> FieldMetadata("format" to "int32")
value.isAValue() -> FieldMetadata("format" to "int64")
value.isAValue() -> FieldMetadata("format" to "double")
value.isAValue() -> FieldMetadata("format" to "float")
value.isAValue() -> FieldMetadata("format" to "date-time")
value.isAValue() -> FieldMetadata("format" to "date")
value.isAValue() -> FieldMetadata("format" to "uuid")
value.isAValue() -> FieldMetadata("format" to "uri")
else -> FieldMetadata()
}
}
}
private inline fun Any?.isAValue(): Boolean {
return (this as? Value<*>)?.value?.javaClass == T::class.java
}