diff --git a/scala/runtime/src/main/scala/org/coursera/courier/templates/DataTemplates.scala b/scala/runtime/src/main/scala/org/coursera/courier/templates/DataTemplates.scala index c40e426c..42318fb0 100644 --- a/scala/runtime/src/main/scala/org/coursera/courier/templates/DataTemplates.scala +++ b/scala/runtime/src/main/scala/org/coursera/courier/templates/DataTemplates.scala @@ -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] } @@ -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] ) } @@ -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] - } } diff --git a/scala/runtime/src/test/scala/org/coursera/courier/templates/DataTemplatesTest.scala b/scala/runtime/src/test/scala/org/coursera/courier/templates/DataTemplatesTest.scala index e1a0000f..90a4d141 100644 --- a/scala/runtime/src/test/scala/org/coursera/courier/templates/DataTemplatesTest.scala +++ b/scala/runtime/src/test/scala/org/coursera/courier/templates/DataTemplatesTest.scala @@ -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 @@ -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]) @@ -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 {