crunch-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwi...@apache.org
Subject [2/2] git commit: CRUNCH-473: Properly indicate class type on the PType of serialized case classes
Date Fri, 03 Oct 2014 03:13:44 GMT
CRUNCH-473: Properly indicate class type on the PType of serialized case classes


Project: http://git-wip-us.apache.org/repos/asf/crunch/repo
Commit: http://git-wip-us.apache.org/repos/asf/crunch/commit/1e2ea6eb
Tree: http://git-wip-us.apache.org/repos/asf/crunch/tree/1e2ea6eb
Diff: http://git-wip-us.apache.org/repos/asf/crunch/diff/1e2ea6eb

Branch: refs/heads/master
Commit: 1e2ea6eb8eefc9fa97311ee8131152543a03f561
Parents: f6ed0ca
Author: Josh Wills <jwills@apache.org>
Authored: Thu Oct 2 19:40:01 2014 -0700
Committer: Josh Wills <jwills@apache.org>
Committed: Thu Oct 2 20:06:02 2014 -0700

----------------------------------------------------------------------
 .../apache/crunch/scrunch/ScalaTypesTest.scala   | 19 ++++++++++++-------
 .../org/apache/crunch/scrunch/PTypeFamily.scala  | 15 ++++++++-------
 2 files changed, 20 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/crunch/blob/1e2ea6eb/crunch-scrunch/src/it/scala/org/apache/crunch/scrunch/ScalaTypesTest.scala
----------------------------------------------------------------------
diff --git a/crunch-scrunch/src/it/scala/org/apache/crunch/scrunch/ScalaTypesTest.scala b/crunch-scrunch/src/it/scala/org/apache/crunch/scrunch/ScalaTypesTest.scala
index 4382ca9..f089a87 100644
--- a/crunch-scrunch/src/it/scala/org/apache/crunch/scrunch/ScalaTypesTest.scala
+++ b/crunch-scrunch/src/it/scala/org/apache/crunch/scrunch/ScalaTypesTest.scala
@@ -29,7 +29,8 @@ object ScalaTypesTest {
   }
 }
 
-case class MyCaseClass(name: List[String], gender: Option[String], age: Int, birthday: Long)
+case class MyCaseClass(name: List[String], gender: Option[String], other: Array[MyOtherCaseClass])
+case class MyOtherCaseClass(age: Int, birthday: Long)
 
 class ScalaTypesTest extends CrunchSuite {
   import ScalaTypesTest._
@@ -66,9 +67,11 @@ class ScalaTypesTest extends CrunchSuite {
     val pt = Avros.caseClasses[MyCaseClass]
     pt.getInputMapFn.initialize()
     pt.getOutputMapFn.initialize()
-    val cc = MyCaseClass(List("Josh", "Wills"), Some("Male"), 35, 1234L)
-    val ser = pt.getOutputMapFn.map(cc)
-    assert(cc == pt.getInputMapFn.map(ser))
+    val cc = MyCaseClass(List("Josh", "Wills"), Some("Male"), Array(MyOtherCaseClass(35,
1234L)))
+    val deser = pt.getInputMapFn().map(pt.getOutputMapFn.map(cc))
+    assert(cc.gender == deser.gender)
+    assert(cc.name.equals(deser.name))
+    assert(cc.other(0) == deser.other(0))
   }
 
   @Test
@@ -76,8 +79,10 @@ class ScalaTypesTest extends CrunchSuite {
     val pt = Avros.caseClasses[MyCaseClass]
     pt.getInputMapFn.initialize()
     pt.getOutputMapFn.initialize()
-    val cc = MyCaseClass(List("Josh", "Wills"), null, 35, 1234L)
-    val ser = pt.getOutputMapFn.map(cc)
-    assert(cc == pt.getInputMapFn.map(ser))
+    val cc = MyCaseClass(List("Josh", "Wills"), null, Array(MyOtherCaseClass(35, 1234L)))
+    val deser = pt.getInputMapFn().map(pt.getOutputMapFn.map(cc))
+    assert(cc.gender == deser.gender)
+    assert(cc.name.equals(deser.name))
+    assert(cc.other(0) == deser.other(0))
   }
 }

http://git-wip-us.apache.org/repos/asf/crunch/blob/1e2ea6eb/crunch-scrunch/src/main/scala/org/apache/crunch/scrunch/PTypeFamily.scala
----------------------------------------------------------------------
diff --git a/crunch-scrunch/src/main/scala/org/apache/crunch/scrunch/PTypeFamily.scala b/crunch-scrunch/src/main/scala/org/apache/crunch/scrunch/PTypeFamily.scala
index 0e31299..4edd762 100644
--- a/crunch-scrunch/src/main/scala/org/apache/crunch/scrunch/PTypeFamily.scala
+++ b/crunch-scrunch/src/main/scala/org/apache/crunch/scrunch/PTypeFamily.scala
@@ -58,15 +58,17 @@ object GeneratedTupleHelper {
   }
 }
 
-class TypeMapFn(val rc: Class[_], @transient var ctor: java.lang.reflect.Constructor[_] =
null)
-  extends MapFn[TupleN, Product] {
+class TypeMapFn[P <: Product](val rc: Class[_], @transient var ctor: java.lang.reflect.Constructor[_]
= null)
+  extends MapFn[TupleN, P] {
 
   override def initialize {
     this.ctor = rc.getConstructors().apply(0)
   }
 
-  override def map(x: TupleN): Product = {
-    ctor.newInstance(x.getValues : _*).asInstanceOf[Product]
+  override def map(x: TupleN): P = {
+    println(ctor)
+    x.getValues.foreach(println)
+    ctor.newInstance(x.getValues : _*).asInstanceOf[P]
   }
 }
 
@@ -234,7 +236,7 @@ trait PTypeFamily extends GeneratedTuplePTypeFamily {
 
   def namedTuples(tupleName: String, fields: List[(String, PType[_])]): PType[TupleN]
 
-  def caseClasses[T <: Product : TypeTag]: PType[T] = products[T](implicitly[TypeTag[T]].tpe)
+  def caseClasses[T <: Product : TypeTag]: PType[T] = products(implicitly[TypeTag[T]].tpe)
 
   private def products[T <: Product](tpe: Type): PType[T] = {
     val ctor = tpe.member(nme.CONSTRUCTOR).asMethod
@@ -242,8 +244,7 @@ trait PTypeFamily extends GeneratedTuplePTypeFamily {
     val out = (x: Product) => TupleN.of(x.productIterator.toArray.asInstanceOf[Array[Object]]
: _*)
     val rtc = currentMirror.runtimeClass(tpe)
     val base = namedTuples(rtc.getCanonicalName, args)
-    ptf.derivedImmutable(classOf[Product], new TypeMapFn(rtc), new TMapFn[Product, TupleN](out),
base)
-      .asInstanceOf[PType[T]]
+    ptf.derivedImmutable(rtc.asInstanceOf[Class[T]], new TypeMapFn[T](rtc), new TMapFn[T,
TupleN](out), base)
   }
 
   private val classToPrimitivePType = Map(


Mime
View raw message