From c7423ee7444b320ffef7f5a36f94b682a4fea919 Mon Sep 17 00:00:00 2001 From: Vladislav Tankov Date: Thu, 18 Jul 2024 12:18:14 +0200 Subject: [PATCH] Add hocon module with hocon parsing and update all the gradle --- README.md | 1 + .../kotlin/tanvd/konfy/hocon/HoconProvider.kt | 20 ++++++++ .../tanvd/konfy/hocon/HoconProviderTest.kt | 46 +++++++++++++++++++ settings.gradle.kts | 2 +- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt create mode 100644 konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt diff --git a/README.md b/README.md index 7907e48..b6a562d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Konfy supports plenty of formats with corresponding providers: * SSM (konfy-ssm) - support of AWS Simple System Manager parameters * KeePass (konfy-keepass) - support of parameters stored in kdbx encrypted files * Kara config (konfy-kara) - support of properties config with includes (Kara framework config) +* HOCON (konfy-hocon) - support of HOCON configuration files ## Setup 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..61597a7 --- /dev/null +++ b/konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt @@ -0,0 +1,20 @@ +package tanvd.konfy.hocon + +import com.typesafe.config.Config +import com.typesafe.config.ConfigFactory +import tanvd.konfy.provider.ConfigProvider +import java.io.File +import java.lang.reflect.Type + +class HoconProvider(private val file: File) : ConfigProvider() { + private val config: Config = ConfigFactory.parseFile(file) + + @Suppress("UNCHECKED_CAST") + override fun fetch(key: String, type: Type): N? { + return try { + config.getAnyRef(key) as N? + } catch (e: Exception) { + null + } + } +} 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..f3c1b3d --- /dev/null +++ b/konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt @@ -0,0 +1,46 @@ +package tanvd.konfy.hocon + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.io.File + +class HoconProviderTest { + private val configProvider + get() = HoconProvider(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")