Skip to content

Commit

Permalink
Add konfy-properties module with properties file support
Browse files Browse the repository at this point in the history
Introduced a new konfy-properties module to handle configurations using properties files. Implemented PropertiesProvider and added tests for various scenarios including value conversions and nested keys. Also, updated settings.gradle.kts to include the new module, and made toTypedArray function public.
  • Loading branch information
TanVD committed Jul 24, 2024
1 parent 499a27e commit 278cebe
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
21 changes: 21 additions & 0 deletions konfy-properties/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
group = rootProject.group
version = rootProject.version

dependencies {
api(project(":konfy"))

api("org.tomlj", "tomlj", "1.1.1")

testImplementation("org.assertj", "assertj-core", "3.24.2")
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.9.2")
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.9.2")
}


tasks.withType<Test> {
useJUnitPlatform()

testLogging {
events("passed", "skipped", "failed")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tanvd.konfy.properties

import tanvd.konfy.conversion.ConversionService
import tanvd.konfy.provider.ConfigProvider
import tanvd.konfy.utils.toTypedArray
import java.lang.reflect.Type
import java.util.Properties

class PropertiesProvider(private val properties: Properties) : ConfigProvider() {
override fun <N : Any> fetch(key: String, type: Type): N? {
val value = properties[key]?.toString() ?: return null
when {
(type is Class<*> && type.isArray) -> {
val values = value.split(",").map { it.trim() }
val converted = values.map { ConversionService.convert(it, type.componentType) }
return converted.toTypedArray(type.componentType) as N?
}

else -> {
val converted = ConversionService.convert(value, type)
return converted as N?
}
}
}

companion object {
fun from(value: String): PropertiesProvider {
val properties = Properties()
properties.load(value.byteInputStream())
return PropertiesProvider(properties)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tanvd.konfy.properties

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.io.File

class PropertiesConfigTest {
private val configProvider
get() = PropertiesProvider.from(File("src/test/resources/config.properties").readText())

@Test
fun get_keyExists_gotValue() {
val value: String = configProvider.get("first_test")
assertThat(value).isEqualTo("test-pass")
}

@Test
fun get_keyExists_gotIntValue() {
val value: Int = configProvider.get("second_test")
assertThat(value).isEqualTo(42)
}

@Test
fun tryGet_keyDoesNotExist_noValue() {
val value: String? = configProvider.tryGet("third_test")
assertThat(value).isNull()
}

@Test
fun get_keyInTable_gotValue() {
val value: String = configProvider.get("section.fourth_test")
assertThat(value).isEqualTo("test-pass")
}

@Test
fun get_arrayValue_gotValue() {
val value: Array<String> = configProvider.get("section.arena-letters")
assertThat(value).containsAll(listOf("All", "King", "Edwards", "Horses", "Can", "Move", "Bloody", "Fast"))
}

@Test
fun get_valueWithSpaces_gotValue() {
val value: String = configProvider.get("section.nested.best-bike-ever")
assertThat(value).isEqualTo("KTM Duke 390")
}
}
6 changes: 6 additions & 0 deletions konfy-properties/src/test/resources/config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
first_test = "test-pass"
second_test = 42

section.fourth_test = "test-pass"
section.arena-letters = All,King,Edwards,Horses,Can,Move,Bloody,Fast
section.nested.best-bike-ever = KTM Duke 390
2 changes: 1 addition & 1 deletion konfy/src/main/kotlin/tanvd/konfy/utils/Conversion.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tanvd.konfy.utils

internal fun Collection<*>.toTypedArray(type: Class<*>): Array<*> {
fun Collection<*>.toTypedArray(type: Class<*>): Array<*> {
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
return (this as java.util.Collection<*>).toArray(java.lang.reflect.Array.newInstance(type, size) as Array<*>)
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ rootProject.name = "konfy-root"

include(":konfy")
include(":konfy-toml")
include(":konfy-properties")
include(":konfy-keepass")
include(":konfy-ssm")
include(":konfy-kara")
Expand Down

0 comments on commit 278cebe

Please sign in to comment.