1
1
package org.jetbrains.kotlinx.dataframe.plugin.impl.api
2
2
3
- import kotlinx.serialization.decodeFromString
4
3
import kotlinx.serialization.json.Json
5
4
import org.jetbrains.kotlinx.dataframe.DataFrame
6
5
import org.jetbrains.kotlinx.dataframe.plugin.impl.AbstractInterpreter
@@ -30,7 +29,11 @@ internal class Read0 : AbstractInterpreter<PluginDataFrameSchema>() {
30
29
val Arguments .header: List <String > by arg(defaultValue = Present (listOf ()))
31
30
32
31
override fun Arguments.interpret (): PluginDataFrameSchema {
33
- return DataFrame .read(path).schema().toPluginDataFrameSchema()
32
+ val df = when (val source = tryResolveFile(resolutionPath, path)) {
33
+ is ResolutionDirFile -> DataFrame .read(source.file)
34
+ is UrlOrAbsolutePath -> DataFrame .read(source.path)
35
+ }
36
+ return df.schema().toPluginDataFrameSchema()
34
37
}
35
38
}
36
39
@@ -42,11 +45,13 @@ internal class ReadCSV0 : AbstractInterpreter<PluginDataFrameSchema>() {
42
45
val Arguments .duplicate: Boolean by arg(defaultValue = Present (true ))
43
46
44
47
override fun Arguments.interpret (): PluginDataFrameSchema {
45
- val file = resolveFile(resolutionPath, fileOrUrl)
46
- val df = if (file != null && file.exists()) {
47
- DataFrame .readCSV(file, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
48
- } else {
49
- DataFrame .readCSV(fileOrUrl, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
48
+ val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
49
+ is ResolutionDirFile -> {
50
+ DataFrame .readCSV(source.file, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
51
+ }
52
+ is UrlOrAbsolutePath -> {
53
+ DataFrame .readCSV(source.path, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
54
+ }
50
55
}
51
56
return df.schema().toPluginDataFrameSchema()
52
57
}
@@ -74,17 +79,14 @@ internal class ReadJson0 : AbstractInterpreter<PluginDataFrameSchema>() {
74
79
}
75
80
76
81
fun readJson (resolutionPath : String? , path : String ): DataFrame <Any ?> {
77
- val file = resolveFile(resolutionPath, path)
78
- val df = if (file != null && file.exists()) {
79
- DataFrame .readJson(file)
80
- } else {
81
- DataFrame .readJson(path)
82
+ return when (val source = tryResolveFile(resolutionPath, path)) {
83
+ is ResolutionDirFile -> DataFrame .readJson(source.file)
84
+ is UrlOrAbsolutePath -> DataFrame .readJson(source.path)
82
85
}
83
- return df
84
86
}
85
87
86
- private fun resolveFile (resolutionPath : String? , path : String ): File ? {
87
- return resolutionPath?.let {
88
+ private fun tryResolveFile (resolutionPath : String? , path : String ): DataSource {
89
+ val file = resolutionPath?.let {
88
90
try {
89
91
val file = File (it)
90
92
if (file.exists() && file.isDirectory) {
@@ -96,8 +98,17 @@ private fun resolveFile(resolutionPath: String?, path: String): File? {
96
98
null
97
99
}
98
100
}
101
+ return if (file != null && file.exists()) {
102
+ ResolutionDirFile (file)
103
+ } else {
104
+ UrlOrAbsolutePath (path)
105
+ }
99
106
}
100
107
108
+ private sealed interface DataSource
109
+ private class UrlOrAbsolutePath (val path : String ) : DataSource
110
+ private class ResolutionDirFile (val file : File ) : DataSource
111
+
101
112
internal class ReadDelimStr : AbstractInterpreter <PluginDataFrameSchema >() {
102
113
val Arguments .text: String by arg()
103
114
val Arguments .delimiter: Char by arg(defaultValue = Present (' ,' ))
@@ -128,7 +139,12 @@ internal class ReadExcel : AbstractSchemaModificationInterpreter() {
128
139
val Arguments .nameRepairStrategy: NameRepairStrategy by arg(defaultValue = Present (NameRepairStrategy .CHECK_UNIQUE ))
129
140
130
141
override fun Arguments.interpret (): PluginDataFrameSchema {
131
- val df = DataFrame .readExcel(fileOrUrl, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
142
+ val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
143
+ is ResolutionDirFile ->
144
+ DataFrame .readExcel(source.file, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
145
+ is UrlOrAbsolutePath ->
146
+ DataFrame .readExcel(source.path, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
147
+ }
132
148
return df.schema().toPluginDataFrameSchema()
133
149
}
134
150
}
0 commit comments