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

org.http4k.contract.openapi.v3.values4kExt.kt Maven / Gradle / Ivy

There is a newer version: 5.31.0.0
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy