org.http4k.contract.jsonschema.v3.values4kExt.kt Maven / Gradle / Ivy
The newest version!
package org.http4k.contract.jsonschema.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() = (this as? Value<*>)?.value?.javaClass == T::class.java
© 2015 - 2025 Weber Informatics LLC | Privacy Policy