Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add konfy-properties module with properties file support #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,24 @@
package tanvd.konfy.properties

import tanvd.konfy.properties.utils.Conversion
import tanvd.konfy.provider.ConfigProvider
import java.lang.reflect.Type
import java.util.*

class PropertiesProvider(private val properties: Properties) : ConfigProvider() {
override fun <N : Any> fetch(key: String, type: Type): N? {
val value = properties[key]?.toString() ?: return null
return Conversion.convert(value, type) as N?
}

companion object {
/**
* Read properties from [value] string and return corresponding [PropertiesProvider]
*/
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,21 @@
package tanvd.konfy.properties.utils

import tanvd.konfy.conversion.ConversionService
import tanvd.konfy.utils.toTypedArray
import java.lang.reflect.Type

object Conversion {
fun <N : Any> convert(value: String, type: Type): N? {
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 -> {
return ConversionService.convert(value, type) as N?
}
}
}
}
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
Loading