Created
January 20, 2020 23:34
-
-
Save sherl0cks/52c40fa3b7477f5647ad3a381fb7de33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.github.cdimascio.dotenv.dotenv | |
import org.eclipse.microprofile.config.spi.ConfigSource | |
/** | |
* A bridge from https://github.com/cdimascio/java-dotenv to Quarkus/MicroProfile Config. Should only be used in test code, as these configs should be | |
* provided to lambda via env vars in prod. | |
* | |
* @see <a href="https://quarkus.io/guides/config#custom-configuration">Custom Config Source Quarkus Docs</a> | |
* Note the service file at test/resources/META-INF/SERVICES/org.eclipse.microprofile.config.spi.ConfigSource | |
*/ | |
class DotEnvConfigSource : ConfigSource { | |
private val config = dotenv().entries() | |
.map { toJavaStyleKey(it.key) to it.value } | |
.toMap() | |
override fun getName() : String { | |
return "DotEnv" | |
} | |
override fun getValue(javaStyleKey: String) : String? { | |
return config[javaStyleKey] | |
} | |
// priority is below application.properties | |
override fun getOrdinal(): Int { | |
return 100 | |
} | |
override fun getProperties() : Map<String,String> = config | |
private fun toJavaStyleKey(envStyleKey: String) = envStyleKey.replace('_','.').toLowerCase() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment