Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DAO.scala
- package root
- import scalaz._
- import Scalaz._
- trait Environment {
- type V
- }
- object testEnv extends Environment {
- type V = Bin
- }
- trait DAO[T] {
- import testEnv._
- type K
- val namespace = "test"
- val set: String
- val key: Reader[T, K]
- val bins: Reader[T, Seq[V]]
- def create(obj: T) = {
- println( set )
- println( key(obj) )
- println( bins(obj) )
- }
- }
- object CookieDAO extends DAO[Cookie] {
- type K = String
- val set = "Cookies"
- val key: Reader[Cookie, K] = Reader {_.id}
- val bins: Reader[Cookie, Seq[Bin]] = Reader { x =>
- Bin("id", x.id) ::
- Bin("userId", x.userId) ::
- Bin("domain", x.domain) :: Nil
- }
- }
- class LogDAO[A : LogEntry] extends DAO[A] {
- type K = Int
- val typeval = implicitly[LogEntry[A]]
- val set = typeval._set
- val key = typeval._key
- val bins = typeval._bins
- }
- object LogDAO {
- def log[A: LogEntry](x: A) = new LogDAO[A].create(x)
- }
- /*
- Key key = new Key("test", "myset", "mykey");
- Bin bin = new Bin("mybin", "myvalue");
- client.put(policy, key, bin);
- Bin bin1 = new Bin("name", "John");
- Bin bin2 = new Bin("age", 25);
- client.put(policy, key, bin1, bin2);
- */
- //domain.scala
- package root
- import scalaz._
- import Scalaz._
- case class Key(nsp: String, set: String, id: String)
- case class Bin(name: String, value: Any)
- case class Cookie (id: String, userId: String, domain: String)
- case class Bid (id: Int, price: Float, site: String)
- case class Impression(id: Int, userId: String, bannedId: String)
- trait LogEntry[T] {
- val _set: String
- val _key: Reader[T, Int]
- val _bins: Reader[T, Seq[Bin]]
- }
- object Bid {
- implicit val someVal = new LogEntry[Bid] {
- val _set = "BidLog"
- val _key: Reader[Bid, Int] = Reader { _.id }
- val _bins: Reader[Bid, Seq[Bin]] = Reader { x =>
- Bin("id", x.id) ::
- Bin("price", x.price) ::
- Bin("site", x.site) :: Nil
- }
- }
- }
- object Impression {
- implicit val someVal = new LogEntry[Impression] {
- val _set = "ImpressionLog"
- val _key: Reader[Impression, Int] = Reader { _.id }
- val _bins: Reader[Impression, Seq[Bin]] = Reader { x =>
- Bin("id", x.id) ::
- Bin("userId", x.userId) ::
- Bin("bannedId", x.bannedId) :: Nil
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement