-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 0.5.0
- Loading branch information
Showing
30 changed files
with
316 additions
and
74 deletions.
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
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
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
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
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
dependencies { | ||
compile "com.typesafe:config:$version_typesafe_config" | ||
compile "uy.kohesive.injekt:injekt-core:$version_kohesive_injekt" | ||
|
||
compile relativeProject(":klutter-json-jackson-jdk6") | ||
} |
115 changes: 115 additions & 0 deletions
115
config-typesafe-jdk6/src/main/kotlin/uy/klutter/config/typesafe/InjektConfig.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,115 @@ | ||
package uy.klutter.config.typesafe | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.type.TypeFactory | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.typesafe.config.Config | ||
import com.typesafe.config.ConfigRenderOptions | ||
import uy.kohesive.injekt.Injekt | ||
import uy.kohesive.injekt.api.* | ||
import kotlin.properties.Delegates | ||
|
||
/** | ||
* A class that startups up an system using Injekt + TypesafeConfig, using the default global scope, and default object binder | ||
*/ | ||
public abstract class KonfigAndInjektMain() : KonfigAndInjektScopedMain(Injekt) | ||
|
||
/** | ||
* A startup module that registers and uses singletons/object factories from a specific scope, | ||
* and an ObjectMapper to bind configuration properties into class instances. | ||
*/ | ||
public abstract class KonfigAndInjektScopedMain(public val scope: InjektScope, public val mapper: ObjectMapper = jacksonObjectMapper()) : InjektModule, KonfigModule { | ||
private val ADDON_ID = "Konfigure" | ||
|
||
protected var resolvedConfig: Config by Delegates.notNull() | ||
|
||
abstract fun configFactory(): Config | ||
|
||
private inner class ScopedKonfigRegistrar(val path: List<String>, val scope: InjektScope) : KonfigRegistrar, InjektRegistrar by scope { | ||
override fun importModule(atPath: String, module: KonfigModule) { | ||
module.registerWith(ScopedKonfigRegistrar(path + atPath.split('.'), scope)) | ||
} | ||
|
||
override fun <T : Any> bindClassAtConfigRoot(klass: TypeReference<T>) { | ||
val fullpath = path.filter { it.isNotBlank() }.map { it.removePrefix(".").removeSuffix(".") }.joinToString(".") | ||
loadAndInject(resolvedConfig, fullpath, klass) | ||
} | ||
|
||
override fun <T : Any> bindClassAtConfigPath(configPath: String, klass: TypeReference<T>) { | ||
val fullpath = (path + configPath.split('.')).filter { it.isNotBlank() }.map { it.removePrefix(".").removeSuffix(".") }.joinToString(".") | ||
loadAndInject(resolvedConfig, fullpath, klass) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T : Any> loadAndInject(config: Config, fullPath: String, klass: TypeReference<T>) { | ||
val configAtPath = config.getConfig(fullPath) | ||
val asJson = configAtPath.root().render(ConfigRenderOptions.concise().setJson(true)) | ||
val instance: T = mapper.readValue(asJson, TypeFactory.defaultInstance().constructType(klass.type))!! | ||
scope.registrar.addSingleton(klass, instance) | ||
} | ||
} | ||
|
||
init { | ||
resolvedConfig = configFactory() | ||
val registrar = ScopedKonfigRegistrar(emptyList(), scope) | ||
registrar.registerConfigurables() | ||
scope.registrar.registerInjectables() | ||
} | ||
} | ||
|
||
public interface KonfigRegistrar : InjektRegistrar { | ||
/** | ||
* import a module loading it and any submodules immediately | ||
*/ | ||
fun importModule(atPath: String, module: KonfigModule) | ||
|
||
/** | ||
* bind a class bindings its values from a configuration path immediately | ||
*/ | ||
final inline fun <reified T : Any> bindClassAtConfigPath(configPath: String) { | ||
bindClassAtConfigPath(configPath, fullType<T>()) | ||
} | ||
|
||
/** | ||
* bind a class bindings its values from a configuration path immediately | ||
*/ | ||
fun <T : Any> bindClassAtConfigPath(configPath: String, klass: TypeReference<T>) | ||
|
||
/** | ||
* bind a class bindings its values from a configuration path immediately | ||
*/ | ||
final inline fun <reified T : Any> bindClassAtConfigPath(configPath: String, klass: Class<T>) { | ||
bindClassAtConfigPath(configPath, fullType<T>()) | ||
} | ||
|
||
/** | ||
* bind a class bindings its values from the root of the current configuration path immediately | ||
*/ | ||
final inline fun <reified T : Any> bindClassAtConfigRoot() { | ||
bindClassAtConfigRoot(fullType<T>()) | ||
} | ||
|
||
/** | ||
* bind a class bindings its values from the root of the current configuration path immediately | ||
*/ | ||
fun <T : Any> bindClassAtConfigRoot(klass: TypeReference<T>) | ||
|
||
/** | ||
* bind a class bindings its values from the root of the current configuration path immediately | ||
*/ | ||
final inline fun <reified T : Any> bindClassAtConfigRoot(klass: Class<T>) { | ||
bindClassAtConfigRoot(fullType<T>()) | ||
} | ||
} | ||
|
||
/** | ||
* A package of configuration bound items that can be included into a scope of someone else | ||
*/ | ||
public interface KonfigModule { | ||
final internal fun registerWith(intoModule: KonfigRegistrar) { | ||
intoModule.registerConfigurables() | ||
} | ||
|
||
fun KonfigRegistrar.registerConfigurables() | ||
} | ||
|
8 changes: 4 additions & 4 deletions
8
config-typesafe-jdk6/src/test/kotlin/uy/klutter/config/typesafe/TestConfigLoading.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
77 changes: 77 additions & 0 deletions
77
config-typesafe-jdk6/src/test/kotlin/uy/klutter/config/typesafe/TestInjektConfig.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,77 @@ | ||
package uy.klutter.config.typesafe.tests | ||
|
||
import com.typesafe.config.Config | ||
import org.junit.Test | ||
import uy.klutter.config.typesafe.* | ||
import uy.kohesive.injekt.Injekt | ||
import uy.kohesive.injekt.api.InjektModule | ||
import uy.kohesive.injekt.api.InjektRegistrar | ||
import kotlin.test.assertEquals | ||
|
||
class TestTypesafeConfigInjection { | ||
companion object : KonfigAndInjektMain() { | ||
override fun configFactory(): Config { | ||
return loadConfig(MapAsConfig(kotlin.mapOf( | ||
"http" to kotlin.mapOf("httpPort" to 8080, "workerThreads" to 16), | ||
"data" to kotlin.mapOf("bucket" to "com.test.bucket", "region" to "us-east"), | ||
"other" to kotlin.mapOf("name" to "frisbee")))) | ||
} | ||
|
||
override fun KonfigRegistrar.registerConfigurables() { | ||
bindClassAtConfigPath<HttpConfig>("http") | ||
bindClassAtConfigPath<DataConfig>("data") | ||
importModule("other", OtherModule) | ||
} | ||
|
||
override fun InjektRegistrar.registerInjectables() { | ||
addFactory { ConfiguredThing() } | ||
importModule(OtherModule) | ||
} | ||
|
||
} | ||
|
||
@Test public fun testConfigSingletonsExist() { | ||
val matchHttp = HttpConfig(8080,16) | ||
val matchData = DataConfig("com.test.bucket", "us-east") | ||
|
||
assertEquals(matchHttp, Injekt.get<HttpConfig>()) | ||
assertEquals(matchData, Injekt.get<DataConfig>()) | ||
} | ||
|
||
@Test public fun testFactoryUsingConfigWorks() { | ||
val matchHttp = HttpConfig(8080,16) | ||
val matchData = DataConfig("com.test.bucket", "us-east") | ||
|
||
val thing = Injekt.get<ConfiguredThing>() | ||
assertEquals(matchHttp, thing.httpCfg) | ||
assertEquals(matchData, thing.dataCfg) | ||
} | ||
|
||
@Test public fun testWithModules() { | ||
val thing = Injekt.get<OtherThingWantingConfig>() | ||
assertEquals("frisbee", thing.cfg.name) | ||
} | ||
|
||
|
||
data class HttpConfig(val httpPort: Int, val workerThreads: Int) | ||
data class DataConfig(val bucket: String, val region: String) | ||
data class ConfiguredThing(val httpCfg: HttpConfig = Injekt.get(), val dataCfg: DataConfig = Injekt.get()) | ||
} | ||
|
||
|
||
data class OtherConfig(val name: String) | ||
data class OtherThingWantingConfig(val cfg: OtherConfig = Injekt.get()) | ||
|
||
public object OtherModule : KonfigModule, InjektModule { | ||
override fun KonfigRegistrar.registerConfigurables() { | ||
bindClassAtConfigRoot<OtherConfig>() | ||
} | ||
|
||
override fun InjektRegistrar.registerInjectables() { | ||
addFactory { OtherThingWantingConfig() } | ||
} | ||
} | ||
|
||
|
||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
dependencies { | ||
compile relativeProject(":klutter-config-typesafe-jdk6") | ||
compile "com.typesafe:config:$version_typesafe_config" | ||
|
||
compile relativeProject(":klutter-json-jackson-jdk6") | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
dependencies { | ||
compile relativeProject(':klutter-config-typesafe-jdk6') | ||
compile relativeProject(':klutter-config-typesafe-jdk7') | ||
compile relativeProject(":klutter-json-jackson-jdk8") | ||
|
||
compile "com.typesafe:config:$version_typesafe_config_jdk8" | ||
} |
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
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
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
Oops, something went wrong.