Skip to content

Commit

Permalink
finos#1318 improve ExternalEntitySchema api and add a toMap method
Browse files Browse the repository at this point in the history
- this change also removes getListToObjectConverter.scala since
  its api isn't Java friendly, and it assumes the passed values
  are in the same order as the class' constructor params. Which
  might not always be the case.
- replaced it with a more flexible solution with JsonMapper, that
  converts a map (of field names->values) to a class instance. Which
  should be easily replicable in  Java.
  • Loading branch information
junaidzm13 committed May 23, 2024
1 parent 25f4f4d commit b2c12c8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.apache.ignite.cache.query._
import org.apache.ignite.cluster.ClusterState
import org.apache.ignite.{IgniteCache, Ignition}
import org.finos.vuu.core.module.simul.model.{ChildOrder, OrderStore, ParentOrder}
import org.finos.vuu.example.ignite.utils.getListToObjectConverter
import org.finos.vuu.example.ignite.schema.ChildOrderSchema.toChildOrder
import org.finos.vuu.feature.ignite.IgniteSqlQuery
import org.finos.vuu.feature.ignite.IgniteSqlQuery.QuerySeparator

Expand Down Expand Up @@ -143,8 +143,6 @@ class IgniteOrderStore(private val parentOrderCache: IgniteCache[Int, ParentOrde
.map(x => x.getValue)
}

private def toChildOrder = getListToObjectConverter[ChildOrder](ChildOrder)

private def mapToString(results: FieldsQueryCursor[util.List[_]]): (Int, ListBuffer[String]) = {
var counter = 0
val buffer = mutable.ListBuffer[String]()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.finos.vuu.example.ignite.schema

import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.scala.{ClassTagExtensions, DefaultScalaModule}
import org.apache.ignite.cache.QueryEntity
import org.finos.vuu.core.module.simul.model.ChildOrder
import org.finos.vuu.feature.ignite.utils.buildQueryEntity
Expand All @@ -13,4 +15,13 @@ object ChildOrderSchema {
.build()

val queryEntity: QueryEntity = buildQueryEntity(schema, classOf[Int], classOf[ChildOrder])

object toChildOrder {
private val mapper: JsonMapper with ClassTagExtensions = JsonMapper
.builder()
.addModule(DefaultScalaModule)
.build() :: ClassTagExtensions

def apply(l: List[_]): ChildOrder = mapper.convertValue(schema.toMap(l), classOf[ChildOrder])
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ import org.finos.vuu.util.schema.ExternalEntitySchemaBuilder.{InvalidIndexExcept
import scala.collection.mutable.ListBuffer

trait ExternalEntitySchema {
val fields: List[SchemaField]
val indexes: List[SchemaIndex] = List.empty
/**
* @return schema fields sorted by their index (SchemaField.index).
* */
def fields: List[SchemaField]

def indexes: List[SchemaIndex] = List.empty

/**
* @param values represents a list of values corresponding to the fields in the schema.
* @return a map from field names to the passed values matched by field.index that is
* value at position 0 gets matched with the field x where x.index == 0.
*
* The method doesn't check for the types of passed values and would skip any fields
* that do not have a corresponding value e.g. when `values.length` < `fields.length`.
* */
def toMap(values: List[_]): Map[FieldName, Any] = fields.map(_.name).zip(values).toMap
}

private case class DefaultExternalEntitySchema private (override val fields: List[SchemaField],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.finos.vuu.util.schema
import org.finos.vuu.util.schema.ExternalEntitySchemaBuilder.InvalidIndexException
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks._

class ExternalEntitySchemaTest extends AnyFeatureSpec with Matchers {
Feature("ExternalEntitySchemaBuilder") {
Expand Down Expand Up @@ -76,6 +75,28 @@ class ExternalEntitySchemaTest extends AnyFeatureSpec with Matchers {
)
}
}

Feature("ExternalEntitySchema.toMap") {
val schema = ExternalEntitySchemaBuilder().withEntity(classOf[TestCaseClass]).build()

Scenario("Can convert list of values ordered according to the schema field index to a map of filedName->value") {
val m = schema.toMap(List("t-shirt", 3, 10.99))

m shouldEqual Map("name" -> "t-shirt", "size" -> 3, "value" -> 10.99)
}

Scenario("Can convert a list of correctly ordered values with some last missing to a map without the missing fields") {
val m = schema.toMap(List("t-shirt", 3))

m shouldEqual Map("name" -> "t-shirt", "size" -> 3)
}

Scenario("Can convert a list of correctly ordered values with extra values appended at the end to a map skipping any extra values") {
val m = schema.toMap(List("t-shirt", 3, 10.99, "extra-value-passed"))

m shouldEqual Map("name" -> "t-shirt", "size" -> 3, "value" -> 10.99)
}
}
}

private case class TestCaseClass(name: String, size: Int, value: Double)

0 comments on commit b2c12c8

Please sign in to comment.