feat: type enrichment (#408)
This commit is contained in:
34
enrichment/build.gradle.kts
Normal file
34
enrichment/build.gradle.kts
Normal file
@ -0,0 +1,34 @@
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("io.bkbn.sourdough.library.jvm")
|
||||
id("io.gitlab.arturbosch.detekt")
|
||||
id("com.adarshr.test-logger")
|
||||
id("maven-publish")
|
||||
id("java-library")
|
||||
id("signing")
|
||||
id("org.jetbrains.kotlinx.kover")
|
||||
}
|
||||
|
||||
sourdoughLibrary {
|
||||
libraryName.set("Kompendium Type Enrichment")
|
||||
libraryDescription.set("Utility library for creating portable type enrichments")
|
||||
compilerArgs.set(listOf("-opt-in=kotlin.RequiresOptIn"))
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Versions
|
||||
val detektVersion: String by project
|
||||
|
||||
// Formatting
|
||||
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:$detektVersion")
|
||||
|
||||
testImplementation(testFixtures(projects.kompendiumCore))
|
||||
}
|
||||
|
||||
testing {
|
||||
suites {
|
||||
named("test", JvmTestSuite::class) {
|
||||
useJUnitJupiter()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package io.bkbn.kompendium.enrichment
|
||||
|
||||
sealed interface Enrichment
|
@ -0,0 +1,7 @@
|
||||
package io.bkbn.kompendium.enrichment
|
||||
|
||||
class PropertyEnrichment : Enrichment {
|
||||
var deprecated: Boolean? = null
|
||||
var description: String? = null
|
||||
var typeEnrichment: TypeEnrichment<*>? = null
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user