Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit c703d5d

Browse files
committed
Handles transient now. Handles pickling ClassTags too. + tests
1 parent c273eee commit c703d5d

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

core/src/main/scala/pickling/Compat.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ object Compat {
8585
c.Expr[FastTypeTag[T]](bundle.impl[T])
8686
}
8787

88+
def FastTypeTagMacros_implClassTag[T: c.WeakTypeTag](c: Context): c.Expr[FastTypeTag[T]] = {
89+
val c0: c.type = c
90+
val bundle = new { val c: c0.type = c0 } with FastTypeTagMacros
91+
c.Expr[FastTypeTag[T]](bundle.implClassTag[T])
92+
}
93+
8894
def FastTypeTagMacros_apply(c: Context)(key: c.Expr[String]): c.Expr[FastTypeTag[t]] forSome { type t } = {
8995
val c0: c.type = c
9096
val bundle = new { val c: c0.type = c0 } with FastTypeTagMacros

core/src/main/scala/pickling/FastTags.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scala.reflect.macros.Context
55
import scala.reflect.api.{Universe => ApiUniverse}
66
import scala.reflect.runtime.{universe => ru}
77
import language.experimental.macros
8+
import scala.reflect.ClassTag
89

910
trait FastTypeTag[T] extends Equals {
1011
def mirror: ru.Mirror
@@ -19,6 +20,8 @@ trait FastTypeTag[T] extends Equals {
1920
object FastTypeTag {
2021
implicit def materializeFastTypeTag[T]: FastTypeTag[T] = macro Compat.FastTypeTagMacros_impl[T]
2122

23+
implicit def materializeFastTypeTagOfClassTag[T]: FastTypeTag[ClassTag[T]] = macro Compat.FastTypeTagMacros_implClassTag[T]
24+
2225
private def stdTag[T: ru.TypeTag]: FastTypeTag[T] = apply(scala.reflect.runtime.currentMirror, ru.typeOf[T], ru.typeOf[T].key).asInstanceOf[FastTypeTag[T]]
2326
implicit val Null = stdTag[Null]
2427
implicit val Byte = stdTag[Byte]
@@ -108,6 +111,17 @@ trait FastTypeTagMacros extends Macro {
108111
}
109112
"""
110113
}
114+
def implClassTag[T: c.WeakTypeTag]: c.Tree = {
115+
import c.universe._
116+
val T = weakTypeOf[T]
117+
q"""
118+
new FastTypeTag[ClassTag[$T]] {
119+
def mirror = scala.pickling.internal.`package`.currentMirror
120+
lazy val tpe = scala.reflect.runtime.universe.weakTypeOf[ClassTag[$T]]
121+
def key = "ClassTag[" + ${T.key} + "]"
122+
}
123+
"""
124+
}
111125
def apply(key: c.Tree): c.Tree = {
112126
import c.universe._
113127
q"""scala.pickling.FastTypeTag(scala.pickling.internal.`package`.currentMirror, $key)"""

core/src/main/scala/pickling/ir/IRs.scala

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,9 @@ class IRs[U <: Universe with Singleton](val uni: U) {
3535
// TODO: minimal versus verbose PickleFormat. i.e. someone might want all concrete inherited fields in their pickle
3636

3737
def notMarkedTransient(sym: TermSymbol): Boolean = {
38-
//println(s"checking annots of ${sym.toString}...")
3938
val tr = scala.util.Try {
40-
if (sym.accessed != NoSymbol) {
41-
val overall = sym.accessed.annotations.forall { a =>
42-
val res = (a.tpe =:= typeOf[scala.transient])
43-
!res
44-
}
45-
overall
46-
} else true // if there is no backing field, then it cannot be marked transient
47-
}
48-
if (tr.isFailure) {
39+
(sym.accessed == NoSymbol) || // if there is no backing field, then it cannot be marked transient
40+
!sym.accessed.annotations.exists(_.tpe =:= typeOf[scala.transient])
4941
}
5042
tr.isFailure || tr.get
5143
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package scala.pickling.implicitparams
2+
3+
import org.scalatest.FunSuite
4+
import scala.pickling._
5+
import json._
6+
7+
8+
case class Person(implicit name: String, age: Int)
9+
object Test {
10+
class SimpleImplParamTest extends FunSuite {
11+
test("main") {
12+
implicit val nme = "Harry"
13+
implicit val age = 18
14+
15+
val per = Person()
16+
val p = per.pickle
17+
val up = p.unpickle[Person]
18+
assert(per == up)
19+
}
20+
}
21+
}
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
package scala.pickling.transienttest
22

33
import org.scalatest.FunSuite
4+
import scala.reflect.ClassTag
45
import scala.pickling._
56
import json._
67

78
case class Person(val name: String , @transient val ssNumber: Int) {
89
override def toString = s"Person($name)"
910
}
1011

12+
class Dependency[T]
13+
14+
class SparkConf(loadDefaults: Boolean)
15+
class SparkContext(config: SparkConf)
16+
17+
class RDD[T: ClassTag](
18+
@transient private var sc: SparkContext,
19+
@transient private var deps: Seq[Dependency[_]]
20+
)
21+
22+
class RangePartitioner[K : ClassTag, V](
23+
@transient val partitions: Int,
24+
@transient val rdd: RDD[_ <: Product2[K,V]],
25+
private var ascending: Boolean = true) {
26+
override def toString = s"RangePartitioner(ascending = $ascending)"
27+
}
28+
1129
class TransientSimpleTest extends FunSuite {
1230
test("main") {
1331
val per = Person("Jenny", 123)
@@ -16,4 +34,16 @@ class TransientSimpleTest extends FunSuite {
1634
assert(up.ssNumber == 0)
1735
assert(per.toString == up.toString)
1836
}
19-
}
37+
}
38+
39+
class TransientSparkTest extends FunSuite {
40+
test("main") {
41+
val sc = new SparkContext(new SparkConf(true))
42+
val rdd = new RDD[(Int, Int)](sc, Seq(new Dependency()))
43+
val rp = new RangePartitioner[Int, Int](2, rdd)
44+
val p: JSONPickle = rp.pickle
45+
val up = p.unpickle[RangePartitioner[Int, Int]]
46+
assert(rp.toString == up.toString)
47+
}
48+
}
49+

0 commit comments

Comments
 (0)