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 add type conversions required for sql filters
- Loading branch information
1 parent
d6660ca
commit f4296fd
Showing
6 changed files
with
142 additions
and
13 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
83 changes: 83 additions & 0 deletions
83
...lugin/src/main/scala/org/finos/vuu/feature/ignite/filter/SqlFilterColumnValueParser.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,83 @@ | ||
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.SqlFilterColumnValueParser.{ErrorMessage, ParsedResult} | ||
import org.finos.vuu.util.schema.typeConversion.TypeConverter.buildConverterName | ||
import org.finos.vuu.util.schema.{SchemaField, SchemaMapper} | ||
|
||
protected trait SqlFilterColumnValueParser { | ||
def parseColumnValue(columnName: String, columnValue: String): Either[ErrorMessage, ParsedResult[String]] | ||
def parseColumnValues(columnName: String, columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[String]]] | ||
} | ||
|
||
protected object SqlFilterColumnValueParser { | ||
def apply(schemaMapper: SchemaMapper): SqlFilterColumnValueParser = new ColumnValueParser(schemaMapper) | ||
|
||
case class ParsedResult[T](externalField: SchemaField, data: T) | ||
|
||
type ErrorMessage = String | ||
} | ||
|
||
private class ColumnValueParser(private val mapper: SchemaMapper) extends SqlFilterColumnValueParser with StrictLogging { | ||
|
||
override def parseColumnValue(columnName: String, columnValue: String): Either[ErrorMessage, ParsedResult[String]] = { | ||
mapper.externalSchemaField(columnName) match { | ||
case Some(f) => RawColumnValueParser(f).parse(columnValue).map(ParsedResult(f, _)) | ||
case None => Left(externalFieldNotFoundError(columnName)) | ||
} | ||
} | ||
|
||
override def parseColumnValues(columnName: String, columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[String]]] = { | ||
mapper.externalSchemaField(columnName) match { | ||
case Some(f) => parseColumnValues(RawColumnValueParser(f), columnValues) | ||
case None => Left(externalFieldNotFoundError(columnName)) | ||
} | ||
} | ||
|
||
private def parseColumnValues(parser: RawColumnValueParser, | ||
columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[String]]] = { | ||
val (errors, parsedValues) = columnValues.partitionMap(parser.parse) | ||
val combinedError = errors.mkString("\n") | ||
|
||
if (parsedValues.isEmpty) return Left(combinedError) | ||
|
||
if (errors.nonEmpty) logger.error(s"Failed to parse some of the column values corresponding to " + | ||
s"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, String] = { | ||
parseStringToColumnDataType(columnValue) | ||
.flatMap(convertColumnValueToExternalFieldType) | ||
.map(convertExternalValueToString) | ||
} | ||
|
||
private def parseStringToColumnDataType(value: String): Either[ErrorMessage, Any] = | ||
DataType.parseDataType(value, column.dataType) | ||
|
||
private def convertColumnValueToExternalFieldType(columnValue: Any): Either[ErrorMessage, Any] = | ||
mapper.toMappedExternalFieldType(columnValue, column.name) | ||
.toRight(s"Failed to convert column value `$columnValue` from `${column.dataType}` to external type `${field.dataType}`") | ||
|
||
private def convertExternalValueToString(v: Any): String = | ||
mapper.convertExternalValueToString(v, field.name).getOrElse(logWarningAndUseDefaultToString(v)) | ||
|
||
private def logWarningAndUseDefaultToString(value: Any): String = { | ||
logger.warn( | ||
s"Could not find a converter [${buildConverterName(field.dataType, classOf[String])}] " + | ||
s"required for SQL filters to be applied to ${field.name}. Falling back to using the " + | ||
s"default `toString`." | ||
) | ||
Option(value).map(_.toString).orNull | ||
} | ||
} | ||
} |
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
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
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
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