Skip to content

Commit 44871e7

Browse files
authored
Merge pull request #1714 from Kotlin/importDataSchema-fix1
`importDataSchema()` notebooks fix
2 parents feedfa1 + e3b9b1f commit 44871e7

3 files changed

Lines changed: 35 additions & 50 deletions

File tree

  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter
  • dataframe-jdbc/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io
  • dataframe-jupyter/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/importDataSchema.kt

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,3 @@ public fun importDataSchema(path: String): ImportDataSchema = ImportDataSchema(p
1919
public fun importDataSchema(path: Path): ImportDataSchema = ImportDataSchema(path)
2020

2121
public fun importDataSchema(file: File): ImportDataSchema = ImportDataSchema(file)
22-
23-
@Language("kts")
24-
internal val importDataSchema =
25-
"""
26-
/** Import the type-only data schema from [url]. */
27-
fun importDataSchema(url: URL, name: String) {
28-
val formats = listOfNotNull(
29-
if (dataFrameConfig.enableExperimentalOpenApi) OpenApi() else null,
30-
)
31-
32-
require(formats.isNotEmpty()) {
33-
"importDataSchema() did not find any supported type-only data schema generation providers (`SupportedCodeGenerationFormat`). If you were looking for OpenAPI 3.0.0 types, set `%use dataframe(..., enableExperimentalOpenApi=true)`."
34-
}
35-
36-
val codeGenResult = org.jetbrains.dataframe.impl.codeGen.CodeGenerator.urlCodeGenReader(url, formats)
37-
when (codeGenResult) {
38-
is org.jetbrains.kotlinx.dataframe.impl.codeGen.CodeGenerationReadResult.Success -> {
39-
val readDfMethod = codeGenResult.getReadDfMethod(url.toExternalForm())
40-
val code = readDfMethod.additionalImports.joinToString("\n") +
41-
"\n" +
42-
codeGenResult.code.converter(name)
43-
44-
EXECUTE(code)
45-
DISPLAY("Data schema successfully imported as ${'$'}name")
46-
}
47-
48-
is org.jetbrains.kotlinx.dataframe.impl.codeGen.CodeGenerationReadResult.Error -> {
49-
DISPLAY("Failed to read data schema from ${'$'}url: ${'$'}{codeGenResult.reason}")
50-
}
51-
}
52-
}
53-
54-
/** Import the type-only data schema from [path]. */
55-
fun importDataSchema(path: String, name: String): Unit = importDataSchema(URI(path).toURL(), name)
56-
57-
/** Import the type-only data schema from [path]. */
58-
fun importDataSchema(path: Path, name: String): Unit = importDataSchema(path.toUri().toURL(), name)
59-
60-
/** Import the type-only data schema from [file]. */
61-
fun importDataSchema(file: File, name: String): Unit = importDataSchema(file.toURI().toURL(), name)
62-
""".trimIndent()

dataframe-jdbc/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/Jdbc.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ public class Jdbc :
2121
stream: InputStream,
2222
name: String,
2323
generateHelperCompanionObject: Boolean,
24-
): Code {
25-
TODO("Not yet implemented")
26-
}
24+
): Code = throw IllegalStateException("Jdbc.readCodeForGeneration() is not yet implemented: Issue #450")
2725

28-
override fun readCodeForGeneration(file: File, name: String, generateHelperCompanionObject: Boolean): Code {
29-
TODO("Not yet implemented")
30-
}
26+
override fun readCodeForGeneration(file: File, name: String, generateHelperCompanionObject: Boolean): Code =
27+
throw IllegalStateException("Jdbc.readCodeForGeneration() is not yet implemented: Issue #450")
3128

3229
override fun acceptsExtension(ext: String): Boolean = ext == "jdbc"
3330

dataframe-jupyter/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/Integration.kt

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ internal class Integration(private val notebook: Notebook, private val options:
7676
property: KProperty<*>,
7777
): VariableName? {
7878
val formats = supportedFormats.filterIsInstance<SupportedCodeGenerationFormat>()
79+
require(formats.isNotEmpty()) {
80+
"importDataSchema() did not find any supported type-only data schema generation providers (`SupportedCodeGenerationFormat`). If you were looking for OpenAPI 3.0.0 types, set `%use dataframe(..., enableExperimentalOpenApi=true)`."
81+
}
7982
val name = property.name + "DataSchema"
8083
return when (
8184
val codeGenResult = CodeGenerator.urlCodeGenReader(importDataSchema.url, name, formats, true)
@@ -87,14 +90,40 @@ internal class Integration(private val notebook: Notebook, private val options:
8790
codeGenResult.code
8891

8992
execute(code)
90-
execute("""DISPLAY("Data schema successfully imported as ${property.name}: $name")""")
93+
display("Data schema successfully imported as ${property.name}: $name", null)
9194

9295
name
9396
}
9497

9598
is CodeGenerationReadResult.Error -> {
96-
execute(
97-
"""DISPLAY("Failed to read data schema from ${importDataSchema.url}: ${codeGenResult.reason}")""",
99+
display(
100+
buildString {
101+
appendLine(
102+
"""
103+
Failed to read data schema from ${importDataSchema.url}.
104+
105+
Cause: ${codeGenResult.reason},
106+
107+
Available formats: ${formats.map { it::class.qualifiedName ?: it }}
108+
109+
""".trimIndent(),
110+
)
111+
112+
if (formats.none { it::class.simpleName?.lowercase()?.contains("openapi") == true }) {
113+
appendLine(
114+
"If you were looking for OpenAPI 3.0.0 types, set `%use dataframe(..., enableExperimentalOpenApi=true)`.",
115+
)
116+
}
117+
118+
appendLine(
119+
"""
120+
|
121+
|StackTrace:
122+
|${codeGenResult.reason.stackTrace.joinToString("\n|")}
123+
""".trimMargin(),
124+
)
125+
},
126+
null,
98127
)
99128
null
100129
}

0 commit comments

Comments
 (0)