Skip to content

Commit

Permalink
Fix nested collection encoding by adding Lazy (#291)
Browse files Browse the repository at this point in the history
* Fix nested collection encoding by adding Lazy

* add newline
  • Loading branch information
gaborbarna authored and miliofotou committed May 3, 2018
1 parent b403445 commit 749ecc5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 7 additions & 7 deletions dataset/src/main/scala/frameless/TypedEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,27 @@ object TypedEncoder {

implicit def collectionEncoder[C[X] <: Seq[X], T]
(implicit
encodeT: TypedEncoder[T],
encodeT: Lazy[TypedEncoder[T]],
CT: ClassTag[C[T]]
): TypedEncoder[C[T]] =
new TypedEncoder[C[T]] {
def nullable: Boolean = false

def jvmRepr: DataType = FramelessInternals.objectTypeFor[C[T]](CT)

def catalystRepr: DataType = ArrayType(encodeT.catalystRepr, encodeT.nullable)
def catalystRepr: DataType = ArrayType(encodeT.value.catalystRepr, encodeT.value.nullable)

def toCatalyst(path: Expression): Expression =
if (ScalaReflection.isNativeType(encodeT.jvmRepr))
if (ScalaReflection.isNativeType(encodeT.value.jvmRepr))
NewInstance(classOf[GenericArrayData], path :: Nil, catalystRepr)
else MapObjects(encodeT.toCatalyst, path, encodeT.jvmRepr, encodeT.nullable)
else MapObjects(encodeT.value.toCatalyst, path, encodeT.value.jvmRepr, encodeT.value.nullable)

def fromCatalyst(path: Expression): Expression =
MapObjects(
encodeT.fromCatalyst,
encodeT.value.fromCatalyst,
path,
encodeT.catalystRepr,
encodeT.nullable,
encodeT.value.catalystRepr,
encodeT.value.nullable,
Some(CT.runtimeClass) // This will cause MapObjects to build a collection of type C[_] directly
)
}
Expand Down
15 changes: 15 additions & 0 deletions dataset/src/test/scala/frameless/EncoderTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package frameless

import org.scalatest.Matchers

object EncoderTests {
case class Foo(s: Seq[(Int, Int)])
}

class EncoderTests extends TypedDatasetSuite with Matchers {
import EncoderTests._

test("It should encode deeply nested collections") {
implicitly[TypedEncoder[Seq[Foo]]]
}
}

0 comments on commit 749ecc5

Please sign in to comment.