Type-safe environment variables configuration library for Kotlin.
- 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)
dependencies {
implementation("io.github.kronst:kenv:1.0.0")
}
- 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()
)
- Create an environment file (.env):
APP_NAME=MyApp
ALLOWED_ORIGINS=http://localhost:3000,https://example.com
FEATURE_FLAGS=dark_mode=true;beta_features=false
- Load configuration:
val config = Kenv.load<AppConfig>()
- Primitives: String, Byte, Short, Int, Long, Float, Double, Boolean, Char
- Collections: List, Set
- Maps with any supported types as keys and values
- Nullable types
- Objects (including nested objects)
- Custom converters
val config = Kenv.load<AppConfig>(
KenvConfig(
fileName = ".env",
path = "config",
profile = "dev" // Will load from config/.env.dev
)
)
val config = Kenv.load<AppConfig>(
KenvConfig(
collectionItemSeparator = ",",
mapItemSeparator = ";",
mapKeyValueSeparator = "="
)
)
val config = Kenv.load<AppConfig>(
KenvConfig(
emptyValueStrategy = EmptyValueStrategy.DEFAULT // or EmptyValueStrategy.NULL
)
)
This project is licensed under the MIT License - see the LICENSE file for details.