diff --git a/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt b/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt new file mode 100644 index 0000000..0a8da9f --- /dev/null +++ b/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt @@ -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 fetch(key: String, type: Type): N? { + val value = config.getString(key) ?: return null + return convert(value, type) as N? + } +} diff --git a/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/utils/Conversion.kt b/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/utils/Conversion.kt new file mode 100644 index 0000000..2a4c256 --- /dev/null +++ b/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/utils/Conversion.kt @@ -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) + } + } +} diff --git a/konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt b/konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt new file mode 100644 index 0000000..a9a35e5 --- /dev/null +++ b/konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt @@ -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 = 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> = 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") + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 980e987..11feca5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -6,4 +6,4 @@ include(":konfy-keepass") include(":konfy-ssm") include(":konfy-kara") include(":konfy-k8s") - +include(":konfy-hocon")