/salat-core/src/main/scala/com/novus/salat/transform/Extractors.scala

https://github.com/ostewart/salat · Scala · 124 lines · 80 code · 23 blank · 21 comment · 6 complexity · b162ff6d1a91c82ae63cac6f91c18f7b MD5 · raw file

  1. /**
  2. * Copyright (c) 2010, 2011 Novus Partners, Inc. <http://novus.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * For questions and comments about this product, please see the project page at:
  17. *
  18. * http://github.com/novus/salat
  19. *
  20. */
  21. package com.novus.salat.transform
  22. import com.novus.salat._
  23. import com.novus.salat.impls._
  24. import com.novus.salat.annotations._
  25. import scala.collection.Traversable
  26. import scala.tools.scalap.scalax.rules.scalasig._
  27. import com.mongodb.casbah.commons.Imports._
  28. import com.novus.salat.annotations.raw.EnumAs
  29. import com.novus.salat.util._
  30. import scala.math.{BigDecimal => SBigDecimal}
  31. import org.scala_tools.time.Imports._
  32. trait OptionExtractor extends Transformation {
  33. override def before(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  34. case Some(value) if value != null => Some(transform(path, t, value))
  35. case _ => None
  36. }
  37. }
  38. trait TraversableExtractor extends TransformationWithParentType {
  39. override def after(path: String, t: TypeRefType, pt: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  40. case traversable: Traversable[_] => Some(MongoDBList(traversable.map(transform(path, t, _)).toSeq: _*))
  41. case _ => None
  42. }
  43. }
  44. trait MapExtractor extends TransformationWithParentType {
  45. override def after(path: String, t: TypeRefType, pt: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  46. case map: scala.collection.Map[String, _] => {
  47. val builder = MongoDBObject.newBuilder
  48. map.foreach {
  49. case (k, el) =>
  50. builder += (k match {
  51. case s: String => s
  52. case x => x.toString
  53. }) -> transform(path, t, el)
  54. }
  55. Some(builder.result)
  56. }
  57. case _ => None
  58. }
  59. }
  60. trait EnumDeflator extends Transformation {
  61. def transform(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = {
  62. val strategy = Class.forName(path).getAnnotation(classOf[EnumAs]) match {
  63. case specific: EnumAs => specific.strategy
  64. case _ => ctx.defaultEnumStrategy
  65. }
  66. value match {
  67. case ev: Enumeration#Value if strategy == EnumStrategy.BY_VALUE => ev.toString
  68. case ev: Enumeration#Value if strategy == EnumStrategy.BY_ID => ev.id
  69. }
  70. }
  71. }
  72. trait InContextExtractor extends Transformation {
  73. // TODO: add proxyGrater
  74. def transform(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  75. case cc: CaseClass => ctx.lookup_!(path, cc).asInstanceOf[Grater[CaseClass]].asDBObject(cc)
  76. case _ => MongoDBObject("failed-to-convert" -> value.toString)
  77. }
  78. }
  79. trait SBigDecimalToDouble extends Transformation {
  80. def transform(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  81. case sbd: SBigDecimal => sbd(ctx.mathCtx).toDouble
  82. }
  83. }
  84. trait BigIntToLong extends Transformation {
  85. def transform(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  86. case bi: BigInt => bi.toLong
  87. case bi: java.math.BigInteger => bi.longValue
  88. }
  89. }
  90. trait FloatToDouble extends Transformation {
  91. def transform(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  92. case f: Float => f.toDouble
  93. case f: java.lang.Float => f.doubleValue()
  94. }
  95. }
  96. trait CharToString extends Transformation {
  97. def transform(path: String, t: TypeRefType, value: Any)(implicit ctx: Context) = value match {
  98. case c: Char => c.toString
  99. case c: java.lang.Character => c.toString
  100. }
  101. }