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

org.http4k.config.MapEnvironment.kt Maven / Gradle / Ivy

The newest version!
package org.http4k.config

import org.http4k.lens.LensExtractor
import java.io.Reader
import java.util.Properties

class MapEnvironment private constructor(
    private val contents: Map,
    override val separator: String = ","
) : Environment {
    override operator fun  get(key: LensExtractor) = key(this)
    override operator fun get(key: String): String? = contents[key.convertFromKey()]
    override operator fun set(key: String, value: String) =
        MapEnvironment(contents + (key.convertFromKey() to value), separator)

    override fun minus(key: String): Environment = MapEnvironment(contents - key.convertFromKey(), separator)
    override fun keys() = contents.keys

    companion object {
        fun from(properties: Properties, separator: String = ","): Environment = MapEnvironment(
            properties.entries
                .fold(emptyMap()) { acc, (k, v) -> acc + (k.toString().convertFromKey() to v.toString()) }, separator
        )

        fun from(reader: Reader, separator: String = ","): Environment =
            from(Properties().apply { load(reader) }, separator)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy