Skip to content

Commit 5368826

Browse files
committed
Merge acceptance results from host and from rules
1 parent e252d45 commit 5368826

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/util/acceptanceRules.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,13 @@ class PatternNameAcceptanceRule(
9999
* 3) returns `null` if all acceptance results are `null` or the iterable is empty
100100
*/
101101
fun <T> Iterable<AcceptanceRule<T>>.accepts(obj: T): Boolean? {
102-
return mapNotNull { it.accepts(obj) }.lastOrNull()
102+
return unionAcceptance(map { it.accepts(obj) })
103+
}
104+
105+
fun unionAcceptance(results: Iterable<Boolean?>): Boolean? {
106+
return results.filterNotNull().lastOrNull()
107+
}
108+
109+
fun unionAcceptance(vararg result: Boolean?): Boolean? {
110+
return unionAcceptance(result.toList())
103111
}

src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/LibrariesScanner.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@ import org.jetbrains.kotlinx.jupyter.config.getLogger
1818
import org.jetbrains.kotlinx.jupyter.exceptions.ReplException
1919
import org.jetbrains.kotlinx.jupyter.util.AcceptanceRule
2020
import org.jetbrains.kotlinx.jupyter.util.accepts
21+
import org.jetbrains.kotlinx.jupyter.util.unionAcceptance
2122

2223
class LibrariesScanner(val notebook: Notebook) {
2324
private val processedFQNs = mutableSetOf<TypeName>()
2425
private val discardedFQNs = mutableSetOf<TypeName>()
2526

26-
private fun <T, I : LibrariesInstantiable<T>> Iterable<I>.filterNamesToLoad(host: KotlinKernelHost): List<I> {
27+
private fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNamesToLoad(
28+
host: KotlinKernelHost,
29+
integrationTypeNameRules: Iterable<AcceptanceRule<TypeName>>,
30+
): List<I> {
2731
return filter {
2832
val typeName = it.fqn
29-
val acceptance = host.acceptsIntegrationTypeName(typeName)
33+
val acceptance = unionAcceptance(
34+
host.acceptsIntegrationTypeName(typeName),
35+
integrationTypeNameRules.accepts(typeName)
36+
)
3037
log.debug("Acceptance result for $typeName: $acceptance")
3138
when (acceptance) {
3239
true -> processedFQNs.add(typeName)
@@ -57,20 +64,15 @@ class LibrariesScanner(val notebook: Notebook) {
5764
val producers = mutableListOf<LibrariesProducerDeclaration>()
5865

5966
for (result in results) {
60-
for (definition in result.definitions) {
61-
if (integrationTypeNameRules.accepts(definition.fqn) != false)
62-
definitions += definition
63-
}
64-
65-
for (producer in result.producers) {
66-
if (integrationTypeNameRules.accepts(producer.fqn) != false)
67-
producers += producer
68-
}
67+
definitions.addAll(result.definitions)
68+
producers.addAll(result.producers)
6969
}
7070

71+
fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNames() = filterNamesToLoad(host, integrationTypeNameRules)
72+
7173
return LibrariesScanResult(
72-
definitions.filterNamesToLoad(host),
73-
producers.filterNamesToLoad(host),
74+
definitions.filterNames(),
75+
producers.filterNames(),
7476
)
7577
}
7678

0 commit comments

Comments
 (0)