-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added map parser and added more tests
- Loading branch information
Showing
5 changed files
with
50 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package no.telenor.kt.env | ||
|
||
@Target(AnnotationTarget.TYPE) | ||
annotation class MapEnv( | ||
/** Separates key and value */ | ||
val eq: String = "=", | ||
/** Separates key1=value1 key2=value2 */ | ||
val separator: String = ";", | ||
) |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package no.telenor.kt.env.parsers | ||
|
||
import no.telenor.kt.env.MapEnv | ||
import no.telenor.kt.env.Parser | ||
import no.telenor.kt.env.parseValue | ||
import kotlin.reflect.KType | ||
|
||
class MapParser : Parser { | ||
fun parseMap(type: KType, name: String, value: String): Map<Any?, Any?> { | ||
val mapEnvAnnot: MapEnv? = type.annotations.find { it is MapEnv } as MapEnv? | ||
|
||
val eq = arrayOf(mapEnvAnnot?.eq ?: "=") | ||
val separator = mapEnvAnnot?.separator ?: ";" | ||
|
||
val keyType = type.arguments[0].type ?: throw Throwable("Could not detect map key type") | ||
val valueType = type.arguments[1].type ?: throw Throwable("Could not detect map value type") | ||
|
||
val keyValuePairs = value.split(separator) | ||
val outputs = mutableMapOf<Any?, Any?>() | ||
|
||
for (n in keyValuePairs.indices) { | ||
val arr = keyValuePairs[n].split(delimiters = eq, ignoreCase = false, limit = 2) | ||
@Suppress("USELESS_ELVIS") val key = arr[0] ?: continue | ||
@Suppress("USELESS_ELVIS") val str = arr[1] ?: "" | ||
outputs[parseValue(keyType, "$name[$n#key]", key)] = parseValue(valueType, "$name[$key:$n#value]", str) | ||
} | ||
|
||
return outputs | ||
} | ||
} |
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