Frage

This question is related to the first one: Iteration over a sealed trait in Scala?

I have the following sealed trait

/**
 * @author Sebastien Lorber (<i>lorber.sebastien@gmail.com</i>)
 * Date: 02/12/12 - Time: 17:49
 */
sealed trait ResizedImageKey {

  /**
   * Get the dimensions to use on the resized image associated with this key
   */
  def getDimension(originalDimension: Dimension): Dimension

}

object ResizedImageKey {
  val ALL_KEYS: List[ResizedImageKey] = List(Large,Medium,Small,X2)
}

case class Dimension(width: Int,  height: Int)

case object Large extends ResizedImageKey {
  def getDimension(originalDimension: Dimension) = Dimension(1000,1000)
}

case object Medium extends ResizedImageKey{
  def getDimension(originalDimension: Dimension) = Dimension(500,500)
}

case object Small extends ResizedImageKey{
  def getDimension(originalDimension: Dimension) = Dimension(100,100)
}

case object X2 extends ResizedImageKey{
  def getDimension(originalDimension: Dimension) = Dimension(
    width = originalDimension.width * 2,
    height = originalDimension.height * 2
  )
}

This works fine for now. The matter is that I need to be able to use my ResizedImageKey as a key for a map that will be stored in MongoDB with Salat.

I don't think Salat support "sealed trait convertion" right? So should I move to Enumeration, which forces me to do a match / case for the dimensions computations? Or is there any known solution to this problem? Is it possible to create enumeration Value object without extending Enumeration or something?

Thanks

War es hilfreich?

Lösung

Salat developer here.

Salat supports case object hierarchies using the @Salat annotation. (I never recommend using enums in Scala - they're dreadful.)

Now... using a case object as the key for a map is actually not something that mongo supports. All map keys in mongo need to be strings.

So what does this map you're trying to persist look like?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top