Skip to content

Commit

Permalink
Add DataTemplates support for Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Erem Boto committed Jan 16, 2018
1 parent 485fb10 commit eda6ee7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,21 @@ object DataTemplates {
private[this] def newDataMapTemplate[T <: DataTemplate[DataMap]](
data: DataMap, clazz: Class[T]): T = {
val companionInstance = companion(clazz)
val applyMethod = getDataMapConstructor(companionInstance)
val applyMethod = getTemplateDataConstructor(companionInstance, classOf[DataMap])

applyMethod.invoke(companionInstance, data, DataConversion.SetReadOnly).asInstanceOf[T]
}

private[this] def newDataListTemplate[T <: DataTemplate[DataList]](
data: DataList, clazz: Class[T]): T = {
val companionInstance = companion(clazz)
val applyMethod = getTemplateDataConstructor(companionInstance, classOf[DataList])
applyMethod.invoke(companionInstance, data, DataConversion.SetReadOnly).asInstanceOf[T]
}

private[this] def newUnionTemplate[T <: UnionTemplate](data: DataMap, clazz: Class[T]): T = {
val companionInstance = companion(clazz)
val applyMethod = getTemplateDataConstructor(companionInstance, classOf[DataMap])

applyMethod.invoke(companionInstance, data, DataConversion.SetReadOnly).asInstanceOf[T]
}
Expand All @@ -225,15 +239,17 @@ object DataTemplates {
* "build" and fall back on "apply".
*
* @param tmplCompanionInstance the result of calling the private method companion(clazz)
* @param dataClass either classOf[DataMap] or classOf[DataList], depending on whether
* operating on a record/union or an array.
* @return a method that can be construct a Union or Record template when
* invoked a DataMap and DataConversion as args
*/
private[this] def getDataMapConstructor(tmplCompanionInstance: AnyRef): Method = {
private[this] def getTemplateDataConstructor(tmplCompanionInstance: AnyRef, dataClass: Class[_]): Method = {
val companionClass = tmplCompanionInstance.getClass
def getWithName(methodName: String): Method = {
companionClass.getDeclaredMethod(
methodName,
classOf[DataMap],
dataClass,
classOf[DataConversion]
)
}
Expand All @@ -244,22 +260,4 @@ object DataTemplates {
case e: NoSuchMethodException => getWithName("apply")
}
}

private[this] def newDataListTemplate[T <: DataTemplate[DataList]](
data: DataList, clazz: Class[T]): T = {
val companionInstance = companion(clazz)
val applyMethod =
companionInstance.getClass.getDeclaredMethod(
"apply",
classOf[DataList],
classOf[DataConversion])
applyMethod.invoke(companionInstance, data, DataConversion.SetReadOnly).asInstanceOf[T]
}

private[this] def newUnionTemplate[T <: UnionTemplate](data: DataMap, clazz: Class[T]): T = {
val companionInstance = companion(clazz)
val applyMethod = getDataMapConstructor(companionInstance)

applyMethod.invoke(companionInstance, data, DataConversion.SetReadOnly).asInstanceOf[T]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.linkedin.data.schema.UnionDataSchema
import com.linkedin.data.template.DataTemplateUtil
import com.linkedin.data.template.RecordTemplate
import com.linkedin.data.template.UnionTemplate
import org.coursera.courier.data.IntArray
import org.coursera.courier.templates.DataTemplates.DataConversion
import org.scalatest.junit.{AssertionsForJUnit, JUnitSuite}
import org.junit.Test
Expand Down Expand Up @@ -62,7 +63,6 @@ class DataTemplatesTest extends JUnitSuite with AssertionsForJUnit {
assert(DataTemplates.readDataMap(roundTripped) === DataTemplates.readDataMap(json))
}


@Test
def testGetSchema(): Unit = {
val schemaFromClass = DataTemplates.getSchema(classOf[MockLegacyRecord])
Expand All @@ -71,6 +71,14 @@ class DataTemplatesTest extends JUnitSuite with AssertionsForJUnit {
val schemaFromClassTag = DataTemplates.getSchema[MockLegacyRecord]
assert(schemaFromClassTag === MockLegacyRecord.SCHEMA)
}

@Test
def testReadWriteArray(): Unit = {
val array = "[1,2,3,4,5]"
val read = DataTemplates.readArray[IntArray](array)
val written = DataTemplates.writeArray(read)
assert(array === written)
}
}

object DataTemplatesTest {
Expand Down

0 comments on commit eda6ee7

Please sign in to comment.