Skip to content

Commit

Permalink
fix: use Construct. in map and list parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
testersen committed Nov 7, 2023
1 parent 54d449d commit 0f1aa1a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/no/telenor/kt/env/parsers/ListParser.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package no.telenor.kt.env.parsers

import no.telenor.kt.env.Construct
import no.telenor.kt.env.ListEnv
import no.telenor.kt.env.Parser
import no.telenor.kt.env.parseValue
import kotlin.reflect.KType

class ListParser : Parser {
fun parseList(type: KType, name: String, value: String): List<Any?> {
val arrayEnvAnnot: ListEnv? = type.annotations.find { it is ListEnv } as ListEnv?

val separator = arrayEnvAnnot?.separator ?: ","

val itemType = type.arguments.getOrNull(0)?.type ?: throw Throwable("Could not detect list item type")
val items = if (arrayEnvAnnot?.regex == true) value.split(Regex(separator)) else value.split(separator)
val outputs = mutableListOf<Any?>()

for (n in items.indices) outputs.add(parseValue(itemType, "$name#$n", items[n]))
for (n in items.indices) outputs.add(Construct.parseValue(itemType, "$name#$n", items[n]))

return outputs
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/no/telenor/kt/env/parsers/MapParser.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package no.telenor.kt.env.parsers

import no.telenor.kt.env.Construct
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 {
Expand Down Expand Up @@ -34,7 +34,8 @@ class MapParser : Parser {
val arr = eqSplit(keyValuePairs[n])
val key = arr.getOrNull(0) ?: continue
val str = arr.getOrNull(1) ?: ""
outputs[parseValue(keyType, "$name[$n#key]", key)] = parseValue(valueType, "$name[$key:$n#value]", str)
outputs[Construct.parseValue(keyType, "$name[$n#key]", key)] =
Construct.parseValue(valueType, "$name[$key:$n#value]", str)
}

return outputs
Expand Down

0 comments on commit 0f1aa1a

Please sign in to comment.