-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hocon module with hocon parsing and update all the gradle
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
konfy-hocon/src/main/kotlin/tanvd/konfy/hocon/HoconProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
konfy-hocon/src/test/kotlin/tanvd/konfy/hocon/HoconProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters