Skip to content

Commit

Permalink
Disable property type cast to fix issue gh-291 (#781)
Browse files Browse the repository at this point in the history
* update changelog

* Disable property type cast to fix issue gh-291

* Rename KoinExt.kt to KoinPropertyExt.kt

Co-authored-by: Arnaud Giuliani <giuliani.arnaud@gmail.com>
  • Loading branch information
Swordsman-Inaction and arnaudgiuliani authored May 27, 2020
1 parent d9424f4 commit 39f1c3e
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MVVMActivity : AppCompatActivity() {
.replace(R.id.mvvm_frame, MVVMFragment())
.commit()

getKoin().setProperty("session", lifecycleScope.get<Session>())
getKoin().setProperty("session_id", lifecycleScope.get<Session>().id)

mvvm_button.setOnClickListener {
navigateTo<ScopedActivityA>(isRoot = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MVVMFragment : Fragment() {
assertNotEquals(sharedViewModel, simpleViewModel)
assertEquals((activity as MVVMActivity).simpleViewModel, sharedViewModel)

assertEquals(session, getKoin().getProperty("session"))
assertEquals(session?.id, getKoin().getProperty("session_id"))

assertNotEquals(simpleViewModel, (activity as MVVMActivity).simpleViewModel)
assertEquals(sharedViewModel, (activity as MVVMActivity).simpleViewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class MVVMActivity : AppCompatActivity() {
.replace(R.id.mvvm_frame, MVVMFragment::class.java, null, null)
.commit()

getKoin().setProperty("session", lifecycleScope.get<Session>())
getKoin().setProperty("session_id", lifecycleScope.get<Session>().id)

mvvm_button.setOnClickListener {
navigateTo<ScopedActivityA>(isRoot = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ class MVVMFragment(val session: Session) : Fragment() {
assertEquals((requireActivity() as MVVMActivity).savedVm, sharedSaved2)
assertEquals(sharedSaved, sharedSaved2)

assertEquals(requireActivity().lifecycleScope.get<Session>(), getKoin().getProperty("session"))
assertEquals(requireActivity().lifecycleScope.get<Session>().id, getKoin().getProperty("session_id"))
}
}
}
8 changes: 4 additions & 4 deletions koin-projects/koin-core/src/main/kotlin/org/koin/core/Koin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ class Koin {
* @param key
* @param defaultValue
*/
fun <T> getProperty(key: String, defaultValue: T): T {
return _propertyRegistry.getProperty<T>(key) ?: defaultValue
fun getProperty(key: String, defaultValue: String): String {
return _propertyRegistry.getProperty(key) ?: defaultValue
}

/**
* Retrieve a property
* @param key
*/
fun <T> getProperty(key: String): T? {
fun getProperty(key: String): String? {
return _propertyRegistry.getProperty(key)
}

Expand All @@ -289,7 +289,7 @@ class Koin {
* @param key
* @param value
*/
fun <T : Any> setProperty(key: String, value: T) {
fun setProperty(key: String, value: String) {
_propertyRegistry.saveProperty(key, value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class KoinApplication private constructor() {
* Load properties from Map
* @param values
*/
fun properties(values: Map<String, Any>): KoinApplication {
fun properties(values: Map<String, String>): KoinApplication {
koin._propertyRegistry.saveProperties(values)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.koin.core.registry
import org.koin.core.Koin
import org.koin.core.error.NoPropertyFileFoundException
import org.koin.core.logger.Level
import org.koin.ext.isFloat
import org.koin.ext.isInt
import org.koin.ext.quoted
import java.util.*
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -33,13 +31,13 @@ import java.util.concurrent.ConcurrentHashMap
@Suppress("UNCHECKED_CAST")
class PropertyRegistry(val _koin: Koin) {

private val _values: MutableMap<String, Any> = ConcurrentHashMap()
private val _values: MutableMap<String, String> = ConcurrentHashMap()

/**
* saveProperty all properties to registry
* @param properties
*/
fun saveProperties(properties: Map<String, Any>) {
fun saveProperties(properties: Map<String, String>) {
if (_koin._logger.isAt(Level.DEBUG)) {
_koin._logger.debug("load ${properties.size} properties")
}
Expand All @@ -57,18 +55,14 @@ class PropertyRegistry(val _koin: Koin) {

val propertiesMapValues = properties.toMap() as Map<String, String>
propertiesMapValues.forEach { (k: String, v: String) ->
when {
v.isInt() -> saveProperty(k, v.toInt())
v.isFloat() -> saveProperty(k, v.toFloat())
else -> saveProperty(k, v.quoted())
}
saveProperty(k, v.quoted())
}
}

/**
* save a property (key,value)
*/
internal fun <T : Any> saveProperty(key: String, value: T) {
internal fun saveProperty(key: String, value: String) {
_values[key] = value
}

Expand All @@ -83,8 +77,8 @@ class PropertyRegistry(val _koin: Koin) {
* Get a property
* @param key
*/
fun <T> getProperty(key: String): T? {
return _values[key] as? T?
fun getProperty(key: String): String? {
return _values[key]
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,19 +339,19 @@ data class Scope(
* @param key
* @param defaultValue
*/
fun <T> getProperty(key: String, defaultValue: T): T = _koin.getProperty(key, defaultValue)
fun getProperty(key: String, defaultValue: String): String = _koin.getProperty(key, defaultValue)

/**
* Retrieve a property
* @param key
*/
fun <T> getPropertyOrNull(key: String): T? = _koin.getProperty(key)
fun getPropertyOrNull(key: String): String? = _koin.getProperty(key)

/**
* Retrieve a property
* @param key
*/
fun <T> getProperty(key: String): T = _koin.getProperty(key)
fun getProperty(key: String): String = _koin.getProperty(key)
?: throw MissingPropertyException("Property '$key' not found")

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.koin.ext

import org.koin.core.Koin

/**
* Koin extension functions
*
* Non-essential helper functions
*
* @author Edward Zhou
*/

/**
* Retrieve an int property
* @param key
* @param defaultValue
*/
fun Koin.getIntProperty(key: String, defaultValue: Int): Int {
return getIntProperty(key) ?: defaultValue
}

/**
* Retrieve an int property
* @param key
*/
fun Koin.getIntProperty(key: String): Int? {
return getProperty(key)?.toIntOrNull()
}

/**
* Save an int property
* @param key
* @param value
*/
fun Koin.setIntProperty(key: String, value: Int) {
setProperty(key, value.toString())
}

/**
* Retrieve a float property
* @param key
* @param defaultValue
*/
fun Koin.getFloatProperty(key: String, defaultValue: Float): Float {
return getFloatProperty(key) ?: defaultValue
}

/**
* Retrieve a float property
* @param key
*/
fun Koin.getFloatProperty(key: String): Float? {
return getProperty(key)?.toFloatOrNull()
}

/**
* Save a float property
* @param key
* @param value
*/
fun Koin.setFloatProperty(key: String, value: Float) {
setProperty(key, value.toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@
*/
package org.koin.ext

fun String.isFloat() = this.toFloatOrNull() != null

fun String.isInt() = this.toIntOrNull() != null

fun String.quoted() = this.replace("\"", "")
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EnvironmentPropertyDefinitionTest {
environmentProperties()
}.koin

val foundValue = koin.getProperty<String>(aPropertyKey)
val foundValue = koin.getProperty(aPropertyKey)
assertEquals(aPropertyValue, foundValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Test
import org.koin.core.error.NoPropertyFileFoundException
import org.koin.ext.getFloatProperty
import org.koin.ext.getIntProperty

class FilePropertyDefinitionTest {

Expand All @@ -17,7 +19,7 @@ class FilePropertyDefinitionTest {
fileProperties("/koin.properties")
}.koin

val gotValue = koin.getProperty<String>(key)
val gotValue = koin.getProperty(key)

assertEquals(value, gotValue)
}
Expand All @@ -32,7 +34,7 @@ class FilePropertyDefinitionTest {
fileProperties()
}.koin

val gotValue = koin.getProperty<String>(key)
val gotValue = koin.getProperty(key)

assertEquals(value, gotValue)
}
Expand All @@ -56,7 +58,7 @@ class FilePropertyDefinitionTest {
fileProperties()
}.koin

assertEquals("this is a string", koin.getProperty<String>("string.value"))
assertEquals("this is a string", koin.getProperty("string.value"))
}

@Test
Expand All @@ -66,7 +68,7 @@ class FilePropertyDefinitionTest {
fileProperties()
}.koin

assertEquals(42, koin.getProperty<Int>("int.value"))
assertEquals(42, koin.getIntProperty("int.value"))
}

@Test
Expand All @@ -76,6 +78,6 @@ class FilePropertyDefinitionTest {
fileProperties()
}.koin

assertEquals(42.0f, koin.getProperty<Int>("float.value"))
assertEquals(42.0f, koin.getFloatProperty("float.value"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PropertyDefinitionTest {
properties(values)
}.koin

val gotValue = koin.getProperty<String>(key)
val gotValue = koin.getProperty(key)

assertEquals(value, gotValue)
}
Expand All @@ -25,7 +25,7 @@ class PropertyDefinitionTest {
val koin = koinApplication {}.koin

val defaultValue = "defaultValue"
val gotValue = koin.getProperty<String>("aKey", defaultValue)
val gotValue = koin.getProperty("aKey", defaultValue)

assertEquals(defaultValue, gotValue)
}
Expand All @@ -38,7 +38,7 @@ class PropertyDefinitionTest {
val koin = koinApplication { }.koin

koin.setProperty(key, value)
val gotValue = koin.getProperty<String>(key)
val gotValue = koin.getProperty(key)
assertEquals(value, gotValue)
}

Expand All @@ -47,7 +47,7 @@ class PropertyDefinitionTest {
val key = "KEY"
val koin = koinApplication { }.koin

val gotValue = koin.getProperty<String>(key)
val gotValue = koin.getProperty(key)
assertNull(gotValue)
}

Expand All @@ -62,7 +62,7 @@ class PropertyDefinitionTest {
}.koin

koin.setProperty(key, value2)
val gotValue = koin.getProperty<String>(key)
val gotValue = koin.getProperty(key)
assertEquals(value2, gotValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ inline fun <reified T : Any> Application.get(
/**
* Retrieve given property for KoinComponent
* @param key - key property
* throw MissingPropertyException if property is not found
*/
inline fun <reified T> Application.getProperty(key: String) =
getKoin().getProperty<T>(key)
fun Application.getProperty(key: String) =
getKoin().getProperty(key)

/**
* Retrieve given property for KoinComponent
Expand All @@ -73,5 +72,5 @@ inline fun <reified T> Application.getProperty(key: String) =
* @param defaultValue - default value if property is missing
*
*/
inline fun <reified T> Application.getProperty(key: String, defaultValue: T) =
fun Application.getProperty(key: String, defaultValue: String) =
getKoin().getProperty(key) ?: defaultValue
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ inline fun <reified T : Any> Route.get(
/**
* Retrieve given property for KoinComponent
* @param key - key property
* throw MissingPropertyException if property is not found
*/
inline fun <reified T> Route.getProperty(key: String) =
getKoin().getProperty<T>(key)
fun Route.getProperty(key: String) =
getKoin().getProperty(key)

/**
* Retrieve given property for KoinComponent
Expand All @@ -65,7 +64,7 @@ inline fun <reified T> Route.getProperty(key: String) =
* @param defaultValue - default value if property is missing
*
*/
inline fun <reified T> Route.getProperty(key: String, defaultValue: T) =
fun Route.getProperty(key: String, defaultValue: String) =
getKoin().getProperty(key) ?: defaultValue

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ inline fun <reified T : Any> Routing.get(
/**
* Retrieve given property for KoinComponent
* @param key - key property
* throw MissingPropertyException if property is not found
*/
inline fun <reified T> Routing.getProperty(key: String) =
getKoin().getProperty<T>(key)
fun Routing.getProperty(key: String) =
getKoin().getProperty(key)

/**
* Retrieve given property for KoinComponent
Expand Down

0 comments on commit 39f1c3e

Please sign in to comment.