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

tech.codingzen.kata.konfig.KataKonfLens.kt Maven / Gradle / Ivy

package tech.codingzen.kata.konfig

import tech.codingzen.kata.result.Res
import tech.codingzen.kata.result.res
import java.util.*
import kotlin.reflect.KClass

/**
 * Accessor for a ROOT instance associated inside a [KataKonf] instance
 */
interface KataKonfLens {
  val id: String
  operator fun get(konf: KataKonf): Res

  companion object {
    inline operator fun  invoke(alias: String): KataKonfLens = Impl(alias, ROOT::class)
  }

  interface Delegate : KataKonfLens {
    val lens: KataKonfLens

    override val id: String
      get() = lens.id

    override fun get(konf: KataKonf): Res = lens[konf]
  }

  class Impl(private val alias: String, private val rootClass: KClass<*>) : KataKonfLens {
    override val id: String = alias + UUID.randomUUID().toString()

    override fun get(konf: KataKonf): Res = res {
      val root = konf.getRoot(id) ?: err("KataKonf aliased by $alias needs a source added for ${rootClass.qualifiedName} into KataKonf")
      @Suppress("UNCHECKED_CAST")
      root as ROOT
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy