Skip to content

Commit

Permalink
Add hocon module with hocon parsing and update all the gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
TanVD committed Jul 18, 2024
1 parent 499a27e commit c7423ee
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 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,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 <N : Any> fetch(key: String, type: Type): N? {
return try {
config.getAnyRef(key) as N?
} catch (e: Exception) {
null
}
}
}
46 changes: 46 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,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<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")

0 comments on commit c7423ee

Please sign in to comment.