Skip to content
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
15 changes: 15 additions & 0 deletions konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tanvd.konfy.hocon

import com.typesafe.config.Config
import tanvd.konfy.provider.ConfigProvider
import tanvd.konfy.conversion.ConversionService
import java.lang.reflect.Type

class HoconProvider(private val config: Config, private val convert: (String, Type) -> Any? = ConversionService::convert) : ConfigProvider() {

@Suppress("UNCHECKED_CAST")
override fun <N : Any> fetch(key: String, type: Type): N? {
val value = config.getString(key) ?: return null
return convert(value, type) as N?
}
}
15 changes: 15 additions & 0 deletions konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/utils/Conversion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tanvd.konfy.hocon.utils

import com.typesafe.config.ConfigValue
import tanvd.konfy.conversion.ConversionService
import java.lang.reflect.Type

object Conversion {
fun convert(value: ConfigValue, type: Type): Any? {
return when (value.valueType()) {
ConfigValueType.OBJECT -> value.unwrapped()
ConfigValueType.LIST -> (value.unwrapped() as List<*>).map { ConversionService.convert(it.toString(), type) }
else -> ConversionService.convert(value.unwrapped().toString(), type)
}
}
}
47 changes: 47 additions & 0 deletions konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tanvd.konfy.hocon

import com.typesafe.config.ConfigFactory
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.io.File

class HoconProviderTest {
private val configProvider
get() = HoconProvider(ConfigFactory.parseFile(File("src/test/resources/config.conf")))

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

@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_nestedArrayValue_gotValue() {
val value: Array<Array<String>> = configProvider.get("section.array-in-array")
assertThat(value).isDeepEqualTo(arrayOf(arrayOf("123"), arrayOf("456")))
}

@Test
fun get_nestedTable_gotValue() {
val value: String = configProvider.get("section.nested.best-bike-ever")
assertThat(value).isEqualTo("KTM Duke 390")
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ include(":konfy-keepass")
include(":konfy-ssm")
include(":konfy-kara")
include(":konfy-k8s")

include(":konfy-hocon")