Skip to content

Commit

Permalink
Merge pull request #1357 from finos/revert-1354-approvedpush_20240520
Browse files Browse the repository at this point in the history
Revert "#1318 improve ExternalEntitySchema api and add a `toMap` method"
  • Loading branch information
keikeicheung committed May 23, 2024
2 parents 23965b2 + 9236f1c commit b41dc97
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 50 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.schema.ChildOrderSchema.toChildOrder
import org.finos.vuu.example.ignite.utils.getListToObjectConverter
import org.finos.vuu.feature.ignite.IgniteSqlQuery
import org.finos.vuu.feature.ignite.IgniteSqlQuery.QuerySeparator

Expand Down Expand Up @@ -143,6 +143,8 @@ 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,7 +1,5 @@
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 @@ -15,13 +13,4 @@ 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])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.finos.vuu.example.ignite.utils

object getListToObjectConverter {
def apply[ReturnType](obj: Object): List[_] => ReturnType = {
val converter = obj.getClass.getMethods.find(x => x.getName == "apply" && x.isBridge).get
values => converter.invoke(obj, values.map(_.asInstanceOf[AnyRef]): _*).asInstanceOf[ReturnType]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.finos.vuu.example.ignite.schema

import org.finos.vuu.example.ignite.utils.getListToObjectConverter
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers

class getListToObjectConverterTest extends AnyFeatureSpec with Matchers {

Feature("getListToObjectConverterTest") {
Scenario("Can build Dto from a list of values") {
val dto = getListToObjectConverter[TestDto](TestDto)(List("TestObject", 25.5))

dto shouldEqual TestDto(name = "TestObject", value = 25.5)
}
}

private case class TestDto(name: String, value: Double)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,8 @@ import org.finos.vuu.util.schema.ExternalEntitySchemaBuilder.{InvalidIndexExcept
import scala.collection.mutable.ListBuffer

trait ExternalEntitySchema {
/**
* @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
val fields: List[SchemaField]
val indexes: List[SchemaIndex] = List.empty
}

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,6 +3,7 @@ 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 @@ -75,28 +76,6 @@ 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 b41dc97

Please sign in to comment.