forked from finos/vuu
-
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.
finos#1296 make FilterColumnValueParser independent of SQL use-case
- it is now just a utility to convert column value (String) from ANTLR to external value (original external date type) that will then be used with external data source for filtering. - In the process makes it responsible only for one thing i.e. parse column value (String) to external value's data type. Should not care about how we convert the resulting external value to a String understood by SQL. That logic's been moved back to IgniteSqlFilterClause. - also renames SqlStringConverterContainer to less verbose ToSqlStringContainer.
- Loading branch information
1 parent
3dd67cf
commit 3d6e70a
Showing
8 changed files
with
194 additions
and
212 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
73 changes: 73 additions & 0 deletions
73
...e-plugin/src/main/scala/org/finos/vuu/feature/ignite/filter/FilterColumnValueParser.scala
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.finos.vuu.feature.ignite.filter | ||
|
||
import com.typesafe.scalalogging.StrictLogging | ||
import org.finos.vuu.core.table.{Column, DataType} | ||
import org.finos.vuu.feature.ignite.filter.FilterColumnValueParser.{ErrorMessage, ParsedResult} | ||
import org.finos.vuu.util.schema.{SchemaField, SchemaMapper} | ||
|
||
protected trait FilterColumnValueParser { | ||
def parse(columnName: String, columnValue: String): Either[ErrorMessage, ParsedResult[Any]] | ||
def parse(columnName: String, columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[Any]]] | ||
} | ||
|
||
protected object FilterColumnValueParser { | ||
def apply(schemaMapper: SchemaMapper): FilterColumnValueParser = { | ||
new ColumnValueParser(schemaMapper) | ||
} | ||
|
||
case class ParsedResult[T](externalField: SchemaField, externalData: T) | ||
|
||
type ErrorMessage = String | ||
|
||
val STRING_DATA_TYPE: Class[String] = classOf[String] | ||
} | ||
|
||
private class ColumnValueParser(private val mapper: SchemaMapper) extends FilterColumnValueParser with StrictLogging { | ||
|
||
override def parse(columnName: String, columnValue: String): Either[ErrorMessage, ParsedResult[Any]] = { | ||
mapper.externalSchemaField(columnName) match { | ||
case Some(f) => RawColumnValueParser(f).parse(columnValue).map(ParsedResult(f, _)) | ||
case None => Left(externalFieldNotFoundError(columnName)) | ||
} | ||
} | ||
|
||
override def parse(columnName: String, columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[Any]]] = { | ||
mapper.externalSchemaField(columnName) match { | ||
case Some(f) => parseValues(RawColumnValueParser(f), columnValues) | ||
case None => Left(externalFieldNotFoundError(columnName)) | ||
} | ||
} | ||
|
||
private def parseValues(parser: RawColumnValueParser, | ||
columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[Any]]] = { | ||
val (errors, parsedValues) = columnValues.partitionMap(parser.parse) | ||
val combinedError = errors.mkString("\n") | ||
|
||
if (parsedValues.isEmpty) { | ||
Left(combinedError) | ||
} else { | ||
if (errors.nonEmpty) logger.error( | ||
s"Failed to parse some of the column values corresponding to the column ${parser.column.name}: \n $combinedError" | ||
) | ||
Right(ParsedResult(parser.field, parsedValues)) | ||
} | ||
} | ||
|
||
private def externalFieldNotFoundError(columnName: String): String = | ||
s"Failed to find mapped external field for column `$columnName`" | ||
|
||
private case class RawColumnValueParser(field: SchemaField) { | ||
val column: Column = mapper.tableColumn(field.name).get | ||
|
||
def parse(columnValue: String): Either[ErrorMessage, Any] = { | ||
parseStringToColumnDataType(columnValue).flatMap(convertColumnValueToExternalFieldType) | ||
} | ||
|
||
private def parseStringToColumnDataType(value: String): Either[ErrorMessage, Any] = | ||
DataType.parseToDataType(value, column.dataType) | ||
|
||
private def convertColumnValueToExternalFieldType(columnValue: Any): Either[ErrorMessage, Any] = | ||
mapper.toMappedExternalFieldType(column.name, columnValue) | ||
.toRight(s"Failed to convert column value `$columnValue` from `${column.dataType}` to external type `${field.dataType}`") | ||
} | ||
} |
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
122 changes: 0 additions & 122 deletions
122
...lugin/src/main/scala/org/finos/vuu/feature/ignite/filter/SqlFilterColumnValueParser.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.