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

tech.codingzen.kata.konfig.vault.extensions.kt Maven / Gradle / Ivy

package tech.codingzen.kata.konfig.vault

import tech.codingzen.kata.konfig.*
import tech.codingzen.kata.konfig.StringParsers.parser

inline fun  KataKonf.vaultMapParser(
  path: String,
  crossinline parser: ContextualParser, V>
) = propertyDelegateParser { thisRef, property ->
  val root = KataKonfVaultLens[this@vaultMapParser].value()
  val src = catch { root.logical().read(path).data } context { "Unable to retrieve vault map for path: $path" }
  parser(thisRef, property, src)
}

inline fun  KataKonf.vaultMap(path: String, crossinline parser: ValueParser, V>) =
  vaultMapParser(path) { thisRef, property, src ->
    src?.let { parser(it) } ?: run {
      if (property.returnType.isMarkedNullable) null as V
      else err("vault: src for path: $path is null yet the return type is not marked as nullable!!")
    }
  }

inline fun  KataKonf.vaultParser(
  path: String,
  field: String,
  crossinline parser: ContextualParser
) =
  vaultMapParser(path) { thisRef, property, src ->
    parser(thisRef, property, src?.get(field))
  }

inline fun  KataKonf.vault(path: String, field: String, crossinline parser: ValueParser) =
  vaultMapParser(path) { thisRef, property, src ->
    src?.get(field)?.let { parser(it) } ?: run {
      if (property.returnType.isMarkedNullable) null as V
      else err("vault: src for path: $path is null yet the return type is not marked as nullable!!")
    }
  }

inline fun  KataKonf.vault(path: String, field: String) = vault(path, field, parser())




© 2015 - 2025 Weber Informatics LLC | Privacy Policy