Skip to content

kronst/kenv

Repository files navigation

kenv

Type-safe environment variables configuration library for Kotlin.

Maven Central License: MIT

Features

  • Type-safe access to environment variables
  • Validation of required variables at startup
  • Support for default values
  • Flexible type conversion system
  • Support for different environments (profiles)

Installation

dependencies {
    implementation("io.github.kronst:kenv:1.0.0")
}

Quick Start

  1. Create your configuration class:
@EnvConfiguration
data class AppConfig(
    @EnvProperty("APP_NAME", required = true)
    val name: String,
    
    @EnvProperty("APP_PORT")
    val port: Int = 8080,
    
    @EnvProperty("ALLOWED_ORIGINS")
    val allowedOrigins: List<String> = emptyList(),
    
    @EnvProperty("FEATURE_FLAGS")
    val featureFlags: Map<String, Boolean> = emptyMap()
)
  1. Create an environment file (.env):
APP_NAME=MyApp
ALLOWED_ORIGINS=http://localhost:3000,https://example.com
FEATURE_FLAGS=dark_mode=true;beta_features=false
  1. Load configuration:
val config = Kenv.load<AppConfig>()

Supported Types

  • Primitives: String, Byte, Short, Int, Long, Float, Double, Boolean, Char
  • Collections: List, Set
  • Maps with any supported types as keys and values
  • Nullable types

Next Steps

  • Objects (including nested objects)
  • Custom converters

Advanced Usage

Custom Environment Files

val config = Kenv.load<AppConfig>(
    KenvConfig(
        fileName = ".env",
        path = "config",
        profile = "dev"  // Will load from config/.env.dev
    )
)

Collection Separators

val config = Kenv.load<AppConfig>(
    KenvConfig(
        collectionItemSeparator = ",",
        mapItemSeparator = ";",
        mapKeyValueSeparator = "="
    )
)

Empty Value Handling

val config = Kenv.load<AppConfig>(
    KenvConfig(
        emptyValueStrategy = EmptyValueStrategy.DEFAULT  // or EmptyValueStrategy.NULL
    )
)

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published