Skip to content

Commit

Permalink
test without reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
rtc11 committed Apr 22, 2022
1 parent 4b59dd6 commit 32f79e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ subprojects {

kotlin.sourceSets["main"].kotlin.srcDirs("main")
kotlin.sourceSets["test"].kotlin.srcDirs("test")
sourceSets["main"].resources.srcDirs("main")
sourceSets["test"].resources.srcDirs("test")
}
2 changes: 0 additions & 2 deletions ktor-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ dependencies {

testImplementation(kotlin("test"))
}

sourceSets["test"].resources.srcDirs("test")
40 changes: 18 additions & 22 deletions ktor-utils/main/no/nav/aap/ktor/config/Config.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package no.nav.aap.ktor.config

import com.sksamuel.hoplite.ConfigLoader
import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.MapPropertySource
import com.sksamuel.hoplite.yaml.YamlParser
import io.ktor.server.application.*
import io.ktor.server.config.*


inline fun <reified T : Any> Application.loadConfig(vararg resources: String = arrayOf("/application.yml")): T =
ConfigLoader.builder()
.addFileExtensionMapping("yml", YamlParser())
.addKtorConfig(environment.config)
.build()
.loadConfigOrThrow(*resources)
//inline fun <reified T : Any> Application.loadConfig(vararg resources: String = arrayOf("/application.yml")): T =
// ConfigLoader.builder()
// .addFileExtensionMapping("yml", YamlParser())
// .addKtorConfig(environment.config)
// .build()
// .loadConfigOrThrow(*resources)

inline fun <reified T : Any> loadConfig(resource: String = "/application.yml"): T =
ConfigLoader.builder()
Expand All @@ -25,15 +21,15 @@ inline fun <reified T : Any> loadConfig(resource: String = "/application.yml"):
* Add Ktors MapApplicationConfig as PropertySource,
* this allows the MapApplicationConfig to override config values in tests
*/
fun ConfigLoaderBuilder.addKtorConfig(config: ApplicationConfig) = apply {
if (config is MapApplicationConfig) {
// get access to the protected property 'map'
@Suppress("UNCHECKED_CAST")
val map = config.javaClass.getDeclaredField("map").let {
it.isAccessible = true
it.get(config) as Map<String, String>
}

addPropertySource(MapPropertySource(map))
}
}
//fun ConfigLoaderBuilder.addKtorConfig(config: ApplicationConfig) = apply {
// if (config is MapApplicationConfig) {
// // get access to the protected property 'map'
// @Suppress("UNCHECKED_CAST")
// val map = config.javaClass.getDeclaredField("map").let {
// it.isAccessible = true
// it.get(config) as Map<String, String>
// }
//
// addPropertySource(MapPropertySource(map))
// }
//}

0 comments on commit 32f79e4

Please sign in to comment.