Last active
August 29, 2015 14:17
-
-
Save matt-martin/3699dbb4bf2de801863c to your computer and use it in GitHub Desktop.
HList TupleConverter for Scalding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapeless._ | |
import shapeless.ops.hlist._ | |
import shapeless.ops.nat._ | |
import cascading.tuple.{Tuple, TupleEntry} | |
import com.twitter.scalding._ | |
// documentation for cascading: | |
// http://docs.cascading.org/cascading/2.5/javadoc/cascading/tuple/TupleEntry.html | |
// http://docs.cascading.org/cascading/2.5/javadoc/cascading/tuple/Tuple.html | |
// documentation for scalding: | |
// https://twitter.github.io/scalding/index.html#com.twitter.scalding.TupleConverter | |
// https://twitter.github.io/scalding/index.html#com.twitter.scalding.TupleGetter$ | |
// https://twitter.github.io/scalding/index.html#com.twitter.scalding.TupleSetter | |
implicit def baseHListConverter[H](implicit gH: TupleGetter[H]): TupleConverter[H :: HNil] = | |
new TupleConverter[H :: HNil] { | |
override def apply(te: TupleEntry): H :: HNil = gH.get(te.getTuple, te.size() - 1) :: HNil | |
override def arity: Int = 1 | |
} | |
implicit def recursiveHListConverter[H, T <: HList] | |
(implicit gH: TupleGetter[H], tailConverter: TupleConverter[T]): TupleConverter[H :: T] = | |
new TupleConverter[H :: T] { | |
override def apply(te: TupleEntry): H :: T = | |
gH.get(te.getTuple, te.size() - arity) :: tailConverter(te) | |
override def arity: Int = (tailConverter.arity + 1) | |
} | |
implicit def hListSetter[L <: HList, N <: Nat] | |
(implicit tl: ToList[L, Any], len: Length.Aux[L, N], ti: ToInt[N]): | |
TupleSetter[L] = | |
new TupleSetter[L] { | |
override def apply(arg: L): Tuple = { | |
val argList: List[Any] = arg.toList | |
new Tuple(argList.asInstanceOf[List[Object]]:_*) | |
} | |
override def arity: Int = ti() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment