Skip to content

Commit

Permalink
imp: added debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
testersen committed May 6, 2024
1 parent aed1561 commit d7ec3b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/kotlin/no/telenor/kt/env/internal/Debug.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package no.telenor.kt.env.internal

internal fun debug(message: String) {
if (System.getenv("KT_ENV_DEBUG") == "true") {
println("KT_ENV_DEBUG = $message")
}
}
20 changes: 19 additions & 1 deletion src/main/kotlin/no/telenor/kt/env/internal/ParserCollection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ private fun classToParserFunctions(cls: Class<*>): Map<KClass<*>, ParserFn> {
val kClass = cls.kotlin
if (kClass.visibility != KVisibility.PUBLIC || kClass.typeParameters.isNotEmpty()) return emptyMap()
val functions = kClass.memberFunctions.map {
debug("")
debug("Checking function $it ($kClass)")
debug("it.visibility == KVisibility.PUBLIC = ${it.visibility == KVisibility.PUBLIC}")
debug("it.name == \"parse${it.returnType.jvmErasure.simpleName}\" = ${it.name == "parse${it.returnType.jvmErasure.simpleName}"}")
debug("it.typeParameters.isEmpty() = ${it.typeParameters.isEmpty()}")
debug("!it.returnType.isMarkedNullable = ${!it.returnType.isMarkedNullable}")
debug("it.parameters.size == 4 = ${it.parameters.size == 4}")
debug("!it.parameters[0].isOptional && it.parameters[0].kind == KParameter.Kind.INSTANCE = ${!it.parameters[0].isOptional && it.parameters[0].kind == KParameter.Kind.INSTANCE}")
debug("!it.parameters[1].isOptional && it.parameters[1].type.jvmErasure == KType::class = ${!it.parameters[1].isOptional && it.parameters[1].type.jvmErasure == KType::class}")
debug("!it.parameters[2].isOptional && it.parameters[2].type.jvmErasure == String::class = ${!it.parameters[2].isOptional && it.parameters[2].type.jvmErasure == String::class}")
debug("!it.parameters[3].isOptional && it.parameters[3].type.jvmErasure == String::class = ${!it.parameters[3].isOptional && it.parameters[3].type.jvmErasure == String::class}")
if (!(
it.visibility == KVisibility.PUBLIC &&
it.name == "parse${it.returnType.jvmErasure.simpleName}" &&
Expand Down Expand Up @@ -62,21 +73,28 @@ internal object ParserCollection {

fun scan(classLoader: ClassLoader) {
for (url in classLoader.getResources("META-INF/services/no.telenor.kt.env.Parser")) {
debug("Scanning $url")
for (
classParsers in url
.openStream()
.bufferedReader()
.readText()
.trim()
.split(lineRegex)
.map { classLoader.loadClass(it) }
.map { line ->
classLoader.loadClass(line).also {
debug("Loaded ($line) $it")
}
}
.filter {
val isVisited = !visited.contains(it)
if (!isVisited) visited.add(it)
debug("Visited $it: $isVisited")
isVisited
}
.map(classToParserFunctionsTransformer)
) {
debug("Adding parsers $classParsers")
parsers.putAll(classParsers)
}
}
Expand Down

0 comments on commit d7ec3b1

Please sign in to comment.