Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. //DAO.scala
  2. package root
  3.  
  4. import scalaz._
  5. import Scalaz._
  6.  
  7. trait Environment {
  8. type V
  9. }
  10. object testEnv extends Environment {
  11. type V = Bin
  12. }
  13.  
  14. trait DAO[T] {
  15. import testEnv._
  16.  
  17. type K
  18. val namespace = "test"
  19. val set: String
  20.  
  21. val key: Reader[T, K]
  22. val bins: Reader[T, Seq[V]]
  23.  
  24. def create(obj: T) = {
  25. println( set )
  26. println( key(obj) )
  27. println( bins(obj) )
  28. }
  29. }
  30.  
  31. object CookieDAO extends DAO[Cookie] {
  32. type K = String
  33. val set = "Cookies"
  34. val key: Reader[Cookie, K] = Reader {_.id}
  35. val bins: Reader[Cookie, Seq[Bin]] = Reader { x =>
  36. Bin("id", x.id) ::
  37. Bin("userId", x.userId) ::
  38. Bin("domain", x.domain) :: Nil
  39. }
  40. }
  41.  
  42. class LogDAO[A : LogEntry] extends DAO[A] {
  43. type K = Int
  44. val typeval = implicitly[LogEntry[A]]
  45. val set = typeval._set
  46. val key = typeval._key
  47. val bins = typeval._bins
  48. }
  49.  
  50. object LogDAO {
  51. def log[A: LogEntry](x: A) = new LogDAO[A].create(x)
  52. }
  53. /*
  54. Key key = new Key("test", "myset", "mykey");
  55. Bin bin = new Bin("mybin", "myvalue");
  56. client.put(policy, key, bin);
  57.  
  58. Bin bin1 = new Bin("name", "John");
  59. Bin bin2 = new Bin("age", 25);
  60. client.put(policy, key, bin1, bin2);
  61. */
  62.  
  63. //domain.scala
  64. package root
  65.  
  66. import scalaz._
  67. import Scalaz._
  68.  
  69. case class Key(nsp: String, set: String, id: String)
  70. case class Bin(name: String, value: Any)
  71.  
  72. case class Cookie (id: String, userId: String, domain: String)
  73. case class Bid (id: Int, price: Float, site: String)
  74. case class Impression(id: Int, userId: String, bannedId: String)
  75.  
  76. trait LogEntry[T] {
  77. val _set: String
  78. val _key: Reader[T, Int]
  79. val _bins: Reader[T, Seq[Bin]]
  80. }
  81.  
  82. object Bid {
  83. implicit val someVal = new LogEntry[Bid] {
  84. val _set = "BidLog"
  85. val _key: Reader[Bid, Int] = Reader { _.id }
  86. val _bins: Reader[Bid, Seq[Bin]] = Reader { x =>
  87. Bin("id", x.id) ::
  88. Bin("price", x.price) ::
  89. Bin("site", x.site) :: Nil
  90. }
  91. }
  92. }
  93.  
  94. object Impression {
  95. implicit val someVal = new LogEntry[Impression] {
  96. val _set = "ImpressionLog"
  97. val _key: Reader[Impression, Int] = Reader { _.id }
  98. val _bins: Reader[Impression, Seq[Bin]] = Reader { x =>
  99. Bin("id", x.id) ::
  100. Bin("userId", x.userId) ::
  101. Bin("bannedId", x.bannedId) :: Nil
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement