feat: enriched enrichments (#566)

This commit is contained in:
Ryan Brink
2024-01-21 14:15:21 -05:00
committed by GitHub
parent c7545b1072
commit ac464ea9ca
49 changed files with 1371 additions and 724 deletions

View File

@ -0,0 +1,16 @@
package io.bkbn.kompendium.enrichment
class BooleanEnrichment(override val id: String) : Enrichment {
override var deprecated: Boolean? = null
override var description: String? = null
companion object {
inline operator fun invoke(
id: String,
init: BooleanEnrichment.() -> Unit
): BooleanEnrichment {
val builder = BooleanEnrichment(id)
return builder.apply(init)
}
}
}

View File

@ -0,0 +1,24 @@
package io.bkbn.kompendium.enrichment
class CollectionEnrichment<T>(override val id: String) : TypeEnrichment<T> {
override var deprecated: Boolean? = null
override var description: String? = null
var maxItems: Int? = null
var minItems: Int? = null
var uniqueItems: Boolean? = null
// TODO How to handle contains, minContains, maxContains?
var itemEnrichment: TypeEnrichment<*>? = null
companion object {
inline operator fun <reified T> invoke(
id: String,
init: CollectionEnrichment<T>.() -> Unit
): CollectionEnrichment<T> {
val builder = CollectionEnrichment<T>(id)
return builder.apply(init)
}
}
}

View File

@ -1,3 +1,7 @@
package io.bkbn.kompendium.enrichment
sealed interface Enrichment
sealed interface Enrichment {
val id: String
var deprecated: Boolean?
var description: String?
}

View File

@ -0,0 +1,23 @@
package io.bkbn.kompendium.enrichment
class MapEnrichment<V>(override val id: String) : TypeEnrichment<V> {
override var deprecated: Boolean? = null
override var description: String? = null
var maxProperties: Int? = null
var minProperties: Int? = null
lateinit var keyEnrichment: StringEnrichment
lateinit var valueEnrichment: TypeEnrichment<*>
companion object {
inline operator fun <reified V> invoke(
id: String,
init: MapEnrichment<V>.() -> Unit
): MapEnrichment<V> {
val builder = MapEnrichment<V>(id)
return builder.apply(init)
}
}
}

View File

@ -0,0 +1,19 @@
package io.bkbn.kompendium.enrichment
class NumberEnrichment(override val id: String) : Enrichment {
override var deprecated: Boolean? = null
override var description: String? = null
var multipleOf: Number? = null
var maximum: Number? = null
var exclusiveMaximum: Number? = null
var minimum: Number? = null
var exclusiveMinimum: Number? = null
companion object {
inline operator fun invoke(id: String, init: NumberEnrichment.() -> Unit): NumberEnrichment {
val builder = NumberEnrichment(id)
return builder.apply(init)
}
}
}

View File

@ -0,0 +1,27 @@
package io.bkbn.kompendium.enrichment
import kotlin.reflect.KProperty1
class ObjectEnrichment<T>(override val id: String) : TypeEnrichment<T> {
override var deprecated: Boolean? = null
override var description: String? = null
private val _propertyEnrichments: MutableMap<KProperty1<*, *>, Enrichment> = mutableMapOf()
val propertyEnrichment: Map<KProperty1<*, *>, Enrichment>
get() = _propertyEnrichments.toMap()
operator fun <R> KProperty1<T, R>.invoke(init: () -> Enrichment) {
require(!_propertyEnrichments.containsKey(this)) { "${this.name} has already been registered" }
val enrichment = init.invoke()
_propertyEnrichments[this] = enrichment
}
companion object {
inline operator fun <reified T> invoke(id: String, init: ObjectEnrichment<T>.() -> Unit): ObjectEnrichment<T> {
val builder = ObjectEnrichment<T>(id)
return builder.apply(init)
}
}
}

View File

@ -1,36 +0,0 @@
package io.bkbn.kompendium.enrichment
/**
* Reference https://json-schema.org/draft/2020-12/json-schema-validation.html#name-multipleof
*/
class PropertyEnrichment : Enrichment {
// Metadata
var deprecated: Boolean? = null
var description: String? = null
var typeEnrichment: TypeEnrichment<*>? = null
// Number and Integer Constraints
var multipleOf: Number? = null
var maximum: Number? = null
var exclusiveMaximum: Number? = null
var minimum: Number? = null
var exclusiveMinimum: Number? = null
// String constraints
var maxLength: Int? = null
var minLength: Int? = null
var pattern: String? = null
var contentEncoding: String? = null
var contentMediaType: String? = null
// TODO how to handle contentSchema?
// Array constraints
var maxItems: Int? = null
var minItems: Int? = null
var uniqueItems: Boolean? = null
// TODO How to handle contains, minContains, maxContains?
// Object constraints
var maxProperties: Int? = null
var minProperties: Int? = null
}

View File

@ -0,0 +1,20 @@
package io.bkbn.kompendium.enrichment
class StringEnrichment(override val id: String) : Enrichment {
override var deprecated: Boolean? = null
override var description: String? = null
var maxLength: Int? = null
var minLength: Int? = null
var pattern: String? = null
var contentEncoding: String? = null
var contentMediaType: String? = null
// TODO how to handle contentSchema?
companion object {
inline operator fun invoke(id: String, init: StringEnrichment.() -> Unit): StringEnrichment {
val builder = StringEnrichment(id)
return builder.apply(init)
}
}
}

View File

@ -1,25 +1,3 @@
package io.bkbn.kompendium.enrichment
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
class TypeEnrichment<T>(val id: String) : Enrichment {
private val enrichments: MutableMap<KProperty1<*, *>, Enrichment> = mutableMapOf()
fun getEnrichmentForProperty(property: KProperty<*>): Enrichment? = enrichments[property]
operator fun <R> KProperty1<T, R>.invoke(init: PropertyEnrichment.() -> Unit) {
require(!enrichments.containsKey(this)) { "${this.name} has already been registered" }
val propertyEnrichment = PropertyEnrichment()
init.invoke(propertyEnrichment)
enrichments[this] = propertyEnrichment
}
companion object {
inline operator fun <reified T> invoke(id: String, init: TypeEnrichment<T>.() -> Unit): TypeEnrichment<T> {
val builder = TypeEnrichment<T>(id)
return builder.apply(init)
}
}
}
sealed interface TypeEnrichment<T> : Enrichment