Skip to content

Commit

Permalink
chore(core): cleanup layer properties
Browse files Browse the repository at this point in the history
  • Loading branch information
LizAinslie committed Nov 2, 2024
1 parent 4121c6d commit c49da2e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
25 changes: 4 additions & 21 deletions core/src/main/kotlin/sh/illumi/kraft/layer/LayerProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,16 @@ interface LayerProperty<TLayer : ApplicationLayer<*>, TProperty> {
operator fun setValue(thisRef: TLayer, property: KProperty<*>?, value: TProperty)
}

fun <TLayer : ApplicationLayer<TLayer>, TProperty> lazyProperty(initializer: TLayer.() -> TProperty) =
object : LayerProperty<TLayer, TProperty> {
fun <TProperty> lazyProperty(initializer: ApplicationLayer<*>.() -> TProperty) =
object : LayerProperty<ApplicationLayer<*>, TProperty> {
private var value: TProperty? = null

override fun getValue(thisRef: TLayer, property: KProperty<*>?): TProperty {
override fun getValue(thisRef: ApplicationLayer<*>, property: KProperty<*>?): TProperty {
if (value == null) value = thisRef.initializer()
return value!!
}

override fun setValue(thisRef: TLayer, property: KProperty<*>?, value: TProperty) {
this.value = value
}
}

fun <TLayer : RootLayer<TLayer>, TProperty> lazyProperty(initializer: TLayer.() -> TProperty) =
// fixme: remove duplicate. there's some weird issue with the kotlin type
// system that makes this necessary if i pass a RootLayer type to something
// that expects an ApplicationLayer type, it doesn't compile
object : LayerProperty<TLayer, TProperty> {
private var value: TProperty? = null

override fun getValue(thisRef: TLayer, property: KProperty<*>?): TProperty {
if (value == null) value = thisRef.initializer()
return value!!
}

override fun setValue(thisRef: TLayer, property: KProperty<*>?, value: TProperty) {
override fun setValue(thisRef: ApplicationLayer<*>, property: KProperty<*>?, value: TProperty) {
this.value = value
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package sh.illumi.kraft.x.http.extensions

import sh.illumi.kraft.layer.ApplicationLayer
import sh.illumi.kraft.layer.RootLayer
import sh.illumi.kraft.layer.lazyProperty
import sh.illumi.kraft.service.Service

import sh.illumi.kraft.x.http.HttpServer
import sh.illumi.kraft.x.http.routing.Route

var <TLayer : ApplicationLayer<TLayer>> TLayer.httpServer: HttpServer by lazyProperty<TLayer, HttpServer> {
if (this !is RootLayer<*>) throw IllegalStateException("Cannot create HttpServer on non-root layer")
HttpServer(this)
val RootLayer<*>.httpServer: HttpServer by lazyProperty {
HttpServer(this as RootLayer<*>) // i am going to kill myself!!!!
}

fun Service.httpRouting(block: Route.() -> Unit) {
Expand Down

0 comments on commit c49da2e

Please sign in to comment.