generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: basic data source csv loader
- Loading branch information
Peva Blanchard
committed
Nov 29, 2023
1 parent
0f13dab
commit 3268d9d
Showing
2 changed files
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 38 additions & 4 deletions
42
plugin/src/main/kotlin/ch/kleis/lcaac/plugin/datasources/LcaDataSourceOperations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,49 @@ | ||
package ch.kleis.lcaac.plugin.datasources | ||
|
||
import ch.kleis.lcaac.core.datasource.DataSourceOperations | ||
import ch.kleis.lcaac.core.lang.expression.DataExpression | ||
import ch.kleis.lcaac.core.lang.expression.DataSourceExpression | ||
import ch.kleis.lcaac.core.lang.expression.SliceIndex | ||
import ch.kleis.lcaac.core.lang.evaluator.EvaluatorException | ||
import ch.kleis.lcaac.core.lang.expression.* | ||
import ch.kleis.lcaac.core.math.QuantityOperations | ||
import org.apache.commons.csv.CSVFormat | ||
import org.apache.commons.csv.CSVParser | ||
import java.nio.file.Path | ||
|
||
class LcaDataSourceOperations<Q>( | ||
private val ops: QuantityOperations<Q>, | ||
) : DataSourceOperations<Q> { | ||
override fun read(source: DataSourceExpression<Q>, row: SliceIndex, column: SliceIndex): DataExpression<Q> { | ||
TODO("Not yet implemented") | ||
if (row !is StrIndex || column !is StrIndex) { | ||
throw EvaluatorException("Not supported yet") | ||
} | ||
return when (source) { | ||
is ECsvSource -> readCsv(source, row, column) | ||
} | ||
} | ||
|
||
private val format = CSVFormat.DEFAULT.builder() | ||
.setHeader() | ||
.setSkipHeaderRecord(true) | ||
.build() | ||
|
||
// TODO: Very inefficient, csv is scanned on every call | ||
private fun readCsv(source: ECsvSource<Q>, row: StrIndex, column: StrIndex): DataExpression<Q> { | ||
val schema = source.schema | ||
val columnType = schema[column.s] ?: throw EvaluatorException("${column.s} not found in schema") | ||
|
||
val file = Path.of(source.location).toFile() | ||
val index = source.index ?: throw EvaluatorException("Requires index") | ||
val parser = CSVParser(file.inputStream().reader(), format) | ||
val rawValue = parser.records.find { | ||
it[index] == row.s | ||
}?.get(column.s) | ||
?: throw EvaluatorException("value not found (row=${row.s}, col=${column.s})") | ||
|
||
return when(columnType) { | ||
is CQuantity -> { | ||
val amount = ops.pure(rawValue.toDouble()) | ||
return EQuantityScale(amount, columnType.unit) | ||
} | ||
is CText -> EStringLiteral(rawValue) | ||
} | ||
} | ||
} |