Skip to content

Commit 514d3dd

Browse files
authored
Merge pull request #247 from ermolenkodev/140
Enable multi-round DataSchema declaration processing (#140)
2 parents ac64553 + 19fcce6 commit 514d3dd

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

data/jetbrains_repositories.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"repositories": [
3+
{
4+
"full_name": "JetBrains/JPS",
5+
"html_url": "",
6+
"stargazers_count": 10,
7+
"topics": "[jetbrains, jetbrains-youtrack, youtrack, youtrack-api]",
8+
"watchers": 10
9+
}
10+
]
11+
}

plugins/symbol-processor/src/main/kotlin/org/jetbrains/dataframe/ksp/DataFrameSymbolProcessor.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class DataFrameSymbolProcessor(
1313

1414
override fun process(resolver: Resolver): List<KSAnnotated> {
1515
val extensionsGenerator = ExtensionsGenerator(resolver, codeGenerator, logger)
16-
val dataSchemas = extensionsGenerator.resolveValidDataSchemaDeclarations()
17-
dataSchemas.forEach {
16+
val (validDataSchemas, invalidDataSchemas) = extensionsGenerator.resolveDataSchemaDeclarations()
17+
validDataSchemas.forEach {
1818
val file = it.origin.containingFile ?: return@forEach
1919
extensionsGenerator.generateExtensions(file, it.origin, it.properties)
2020
}
@@ -25,6 +25,8 @@ class DataFrameSymbolProcessor(
2525
dataSchemaGenerator.generateDataSchema(importStatement)
2626
}
2727

28-
return emptyList()
28+
// by returning invalidDataSchemas we defer the processing of incomplete DataSchema declarations
29+
// for example when DataSchema declaration references another one generated by @file:ImportDataSchema
30+
return invalidDataSchemas
2931
}
3032
}

plugins/symbol-processor/src/main/kotlin/org/jetbrains/dataframe/ksp/ExtensionsGenerator.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ class ExtensionsGenerator(
3030
val EXPECTED_VISIBILITIES = setOf(Visibility.PUBLIC, Visibility.INTERNAL)
3131
}
3232

33-
fun resolveValidDataSchemaDeclarations(): Sequence<DataSchemaDeclaration> {
33+
fun resolveDataSchemaDeclarations(): Pair<Sequence<DataSchemaDeclaration>, List<KSClassDeclaration>> {
3434
val dataSchemaAnnotation = resolver.getKSNameFromString(DataFrameNames.DATA_SCHEMA)
3535
val symbols = resolver.getSymbolsWithAnnotation(dataSchemaAnnotation.asString())
3636

37-
return symbols
37+
val (validDeclarations, invalidDeclarations) = symbols
3838
.filterIsInstance<KSClassDeclaration>()
39-
.mapNotNull {
40-
if (it.validate()) {
41-
it.toDataSchemaDeclarationOrNull()
42-
} else {
43-
null
44-
}
45-
}
39+
.partition { it.validate() }
40+
41+
val preprocessedDeclarations = validDeclarations
42+
.asSequence()
43+
.mapNotNull { it.toDataSchemaDeclarationOrNull() }
44+
45+
return Pair(preprocessedDeclarations, invalidDeclarations)
4646
}
4747

4848
class DataSchemaDeclaration(

plugins/symbol-processor/src/test/kotlin/org/jetbrains/dataframe/ksp/DataFrameSymbolProcessorTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,42 @@ class DataFrameSymbolProcessorTest {
210210
result.successfulCompilation shouldBe true
211211
}
212212

213+
@Test
214+
fun `multi-round data schema generation`() {
215+
useHostedFile(jetbrainsCsv) {
216+
val result = KspCompilationTestRunner.compile(
217+
TestCompilationParameters(
218+
sources = listOf(
219+
SourceFile.kotlin(
220+
"MySources.kt",
221+
"""
222+
@file:ImportDataSchema(name = "Repo", "$it")
223+
224+
import org.jetbrains.kotlinx.dataframe.DataFrame
225+
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
226+
import org.jetbrains.kotlinx.dataframe.annotations.ImportDataSchema
227+
import org.jetbrains.kotlinx.dataframe.api.print
228+
import org.jetbrains.kotlinx.dataframe.io.readJson
229+
230+
@DataSchema
231+
interface Repos {
232+
val repositories: DataFrame<Repo>
233+
}
234+
235+
fun main() {
236+
val df: DataFrame<Repos> = DataFrame.readJson("data/jetbrains_repositories.json") as DataFrame<Repos>
237+
df.repositories[0].print()
238+
}
239+
240+
""".trimIndent()
241+
)
242+
)
243+
)
244+
)
245+
result.successfulCompilation shouldBe true
246+
}
247+
}
248+
213249
@Test
214250
fun `functional type`() {
215251
val result = KspCompilationTestRunner.compile(

0 commit comments

Comments
 (0)