A very small boilerplate-free kotlin library to read values from environment variables in a typesafe way.
To read an environment variable simply call:
parseFromEnv("myEnvInt", 30)
to get myEnvInt
from the environment as an Int
or
val nullableInt: Int? = parseFromEnv("myNullableEnvInt")
to get myNullableEnvInt
from the environment as an Int?
.
If the variable is not present, can't be parsed or the target type is not supported, 30
is returned in the first case, whereas null
is returned in
the second case.
You can also convert an environment variable to a List
. Let's say the variable is named myIntList
and the content is "1,2,3"
.
val intList: List<Int> = parseListFromEnv("myIntList")
returns a List<Int>
of [1,2,3]
.
You can also define your own separator by calling for example parseListFromEnv("myOtherIntList", ';')
.
This dependency is available at Maven Central.
Note: Prior to version 0.5.0.1, envkeg is only available at jcenter. After 0.5.0, envkeg moved to maven central.
dependencies {
...
implementation("de.breuco:envkeg:0.5.1")
...
}
<?xml version="1.0" encoding="UTF-8"?>
<project>
...
<dependencies>
<dependency>
<groupId>de.breuco</groupId>
<artifactId>envkeg</artifactId>
<version>0.5.1</version>
</dependency>
</dependencies>
...
</project>
Type | Parsing function |
---|---|
Byte | String.toByte() |
Short | String.toShort() |
Int | String.toInt() |
Long | String.toLong() |
Float | String.toFloat() |
Double | String.toDouble() |
Boolean | String.toBoolean() |
LocalDate | LocalDate.parse(...) |
LocalDateTime | LocalDateTime.parse(...) |
OffsetTime | OffsetTime.parse(...) |
OffsetDateTime | OffsetDateTime.parse(...) |
ZonedDateTime | ZonedDateTime.parse(...) |
Instant | Instant.parse(...) |
String | none |