Last active
April 6, 2016 22:50
-
-
Save thatwist/e225e3086ea5461af3b425be174ac4db to your computer and use it in GitHub Desktop.
Trying to project map to tuples in typesafe way (think I need shapeless here :-P)
This file contains hidden or 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
object Projections { | |
object MapUtils { | |
implicit class MapWithGetAsInstanceOf[A](map: Map[A, Any]) { | |
/** get by key and cast to T, exception if cannot cast */ | |
def getAs[T](key: A): Option[T] = map.get(key).asInstanceOf[Option[T]] | |
} | |
} | |
import MapUtils._ | |
type Query | |
type Projection | |
sealed trait Predicate { | |
val op: String | |
} | |
def mkQuery(filter: Traversable[Predicate] = Set()): Query | |
def mkProjection(fields: Traversable[String] = Set()): Projection | |
def findWithProjection(query: Query, projection: Projection, | |
offset: Option[Int] = None, limit: Option[Int] = None, | |
sort: Option[String] = None, order: Boolean = true): Seq[Map[String, Any]] | |
def findFields(filter: Traversable[Predicate], fields: Traversable[String], | |
offset: Option[Int] = None, limit: Option[Int] = None, | |
sort: Option[String] = None, order: Boolean = true): Seq[Map[String, Any]] = { | |
findWithProjection(query = mkQuery(filter), projection = mkProjection(fields), | |
offset = offset, limit = limit, sort = sort, order = order) | |
import scala.language.higherKinds | |
trait Projectable[A[_]] { | |
def project[A](map: Map[String, Any], fields: List[String]): A | |
} | |
object Projectable { | |
implicit object Tuple1Projectable extends Projectable[Tuple1] { | |
override def project(map: Map[String, Any], fields: List[String]): Tuple1[T1] = { | |
fields.headOption.flatMap{ head => | |
map.getAs[T1](head) | |
}.map(Tuple1.apply).getOrElse(throw new IllegalArgumentException("failed to project")) | |
} | |
} | |
implicit class Tuple2Projectable[T1, T2](tuple2: (T1, T2)) extends Projectable[Tuple1[T1]] { | |
override def project(map: Map[String, Any], fields: List[String]): Tuple1[T1] = { | |
fields.headOption.flatMap{ head => | |
map.getAs[T1](head) | |
}.map(Tuple1.apply).getOrElse(throw new IllegalArgumentException("failed to project")) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment