Skip to content

A very small boilerplate-free kotlin library to read values from environment variables in a typesafe way

License

Notifications You must be signed in to change notification settings

breucode/envkeg

Repository files navigation

GitHub release (latest by date) Maven central release (latest) License License

envkeg

A very small boilerplate-free kotlin library to read values from environment variables in a typesafe way.

Usage

Single values

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.

Lists

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", ';').

Install dependency

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.

Gradle

dependencies {
    ...
    implementation("de.breuco:envkeg:0.5.1")
    ...
}

Maven

<?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>

Supported types

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

About

A very small boilerplate-free kotlin library to read values from environment variables in a typesafe way

Resources

License

Stars

Watchers

Forks

Languages