-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from aPureBase/v2.2.0
v2.2.0
- Loading branch information
Showing
14 changed files
with
242 additions
and
68 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
38 changes: 10 additions & 28 deletions
38
arkenv/src/main/kotlin/com/apurebase/arkenv/ArkenvDelegateLoader.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 |
---|---|---|
@@ -1,40 +1,22 @@ | ||
package com.apurebase.arkenv | ||
|
||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KProperty | ||
|
||
class ArkenvDelegateLoader<T : Any>( | ||
private val argument: Argument<T>, | ||
private val kClass: KClass<T>, | ||
private val arkenv: Arkenv | ||
) { | ||
operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): ReadOnlyProperty<Any?, T> = createDelegate(prop) | ||
|
||
private fun createDelegate(prop: KProperty<*>): ArgumentDelegate<T> = with(argument) { | ||
names = (if (names.isEmpty()) listOf("--${prop.name.toSnakeCase()}") else names).let(::processNames) | ||
|
||
return ArgumentDelegate( | ||
arkenv, | ||
argument, | ||
prop, | ||
kClass == Boolean::class, | ||
mapping ?: getMapping(prop) | ||
).also { arkenv.delegates.add(it) } | ||
operator fun provideDelegate(thisRef: Arkenv, prop: KProperty<*>): ReadOnlyProperty<Arkenv, T> { | ||
argument.names = getNames(argument.names, prop.name) | ||
return ArgumentDelegate(argument, prop) | ||
.also { arkenv.delegates.add(it) } | ||
} | ||
|
||
private fun processNames(names: List<String>) = names.map { | ||
if (!it.startsWith("-")) "--$it".mapRelaxed() | ||
else it.mapRelaxed() | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST", "IMPLICIT_CAST_TO_ANY") | ||
private fun getMapping(prop: KProperty<*>): (String) -> T = { value -> | ||
when (kClass) { | ||
Int::class -> value.toIntOrNull() | ||
Long::class -> value.toLongOrNull() | ||
String::class -> value | ||
else -> throw IllegalArgumentException("${prop.name} ($kClass) is not supported") | ||
} as T | ||
} | ||
private fun getNames(names: List<String>, propName: String) = | ||
names.ifEmpty { listOf("--${propName.toSnakeCase()}") } | ||
.map { | ||
if (!it.startsWith("-")) "--$it".mapRelaxed() | ||
else it.mapRelaxed() | ||
} | ||
} |
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
19 changes: 18 additions & 1 deletion
19
arkenv/src/main/kotlin/com/apurebase/arkenv/ValidationException.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 |
---|---|---|
@@ -1,10 +1,27 @@ | ||
package com.apurebase.arkenv | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KProperty | ||
|
||
internal open class ArkenvException(message: String, cause: Exception? = null) : RuntimeException(message, cause) | ||
|
||
/** | ||
* Unchecked exception thrown when validation of an [Argument] was unsuccessful. | ||
*/ | ||
class ValidationException(property: KProperty<*>, value: Any?, message: String) : IllegalArgumentException( | ||
internal class ValidationException(property: KProperty<*>, value: Any?, message: String) : ArkenvException( | ||
"Argument ${property.name} with value '$value' did not pass validation: '$message'" | ||
) | ||
|
||
/** | ||
* Unchecked exception thrown when no supported mapping exists for the given class. | ||
*/ | ||
internal class UnsupportedMappingException(key: String, clazz: KClass<*>) : ArkenvException( | ||
"Property '$key' of type '$clazz' is not supported. Define a custom mapping." | ||
) | ||
|
||
/** | ||
* Unchecked exception thrown when mapping was unsuccessful. | ||
*/ | ||
internal class MappingException(key: String, value: String, clazz: KClass<*>, cause: Exception) : ArkenvException( | ||
"Could not parse property '$key' with value '$value' as class '$clazz'", cause | ||
) |
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.