Enumeration as Key of a Map

442 views
Skip to first unread message

Gabriel Raineri

unread,
Feb 28, 2013, 11:01:42 AM2/28/13
to scala...@googlegroups.com
Hi everybody!

I'm persisting a Map[MyEnum, String] where

object MyEnum extends Enumeration {
  val VAL_ONE, VAL_TWO = Value

  implicit def fromString(name: String) = MyEnum.withName(name)
}

The thing is that when I try to retrieve the object I get it correctly but as a Map[String, String].

Does SalatDAO support doing this?

Thanks in advance!

Regards,
G.

rose katherine toomey

unread,
Feb 28, 2013, 11:02:46 AM2/28/13
to scala-salat
Hi Gabriel, MongoDB does not support using an enum as the key to a map.  The key of your map must be a string.


--
You received this message because you are subscribed to the Google Groups "scala-salat" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-salat...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gabriel Raineri

unread,
Feb 28, 2013, 11:10:07 AM2/28/13
to scala...@googlegroups.com
That's correct! However, I was expecting SalatDAO to convert the types correctly. 

And does MongoDB always store Enumerations as String? (I'm quite new to MongoDB)

Thanks again!

rose katherine toomey

unread,
Feb 28, 2013, 11:13:25 AM2/28/13
to scala-salat
Nope.  The key of a map is expected to be a String.  The end.

rose katherine toomey

unread,
Feb 28, 2013, 11:20:53 AM2/28/13
to scala-salat
See our wiki page for more information about enum support.
https://github.com/novus/salat/wiki/Enums

On 28 February 2013 11:10, Gabriel Raineri <gabriel.rain...@gmail.com> wrote:

Gabriel Raineri

unread,
Feb 28, 2013, 12:33:21 PM2/28/13
to scala...@googlegroups.com
Thanks!

I had already seen it. =)

Michael Ludwig

unread,
Feb 21, 2014, 2:00:39 PM2/21/14
to scala...@googlegroups.com
I stumbled about the same problem. 

I don't understand why this should be "the end". Are their technical problems?

The thing is it work sperfectly saving all that stuff withouth errors, even retrieving it. But it looks like behind the scenes the keys are strings, even for the scala maps.
The MongoDB part is clear, but the side how salat handles it I don't understand.

I will show you my case class:
case class Region (
    id: ObjectId = new ObjectId,
    name: String,
    recomDuration : Option[DurationMinMax] = None,
    dailyBudget : Option[Budget] = None,
    textualDescription: Option[String] = None,
    recomPeriod : Map[Month.Value,Double] = Map(), 
    travelerTypeFit : Map[TravelerType.Value, Double] = Map(),
    @Key("country_ids")countries: Set[ObjectId] = Set.empty,
    @Key("parentRegion_id")parentRegion: Option[ObjectId] = None,
    @Key("neighbour_ids")neighbourRegions : Set[ObjectId] = Set.empty,
    isActive : Boolean = false
    ) extends LabeledToStringDef

Writing "recomPeriod" like that works perfectly. Reading also looks right. 
But things get wrong when I try to retrieve things with equality checks.

I figured out why that is:
The following gives me "String" as a type:
println("map keys type: " + parentRegion.recomPeriod.keySet.toList(0).getClass())

whereas I suspect it to be Month.Value (or actually: scala.Enumeration$Val)

I don't wanna loose my type-safety with that Map, which is anyways String-based (although it is a Enum).

The enum looks like this:

object Month extends Enumeration {
  type Month = Value
  val January = Value("january")
  val Feburary = Value("february")
  val March = Value("march")
  val April = Value("april")
  val May = Value("may")
  val June = Value("june")
  val July = Value("july")
  val August = Value("august")
  val September = Value("september")
  val October = Value("october")
  val November = Value("november")
  val December = Value("december")
  val options2 = values.toList.map(value => (value.toString -> value.toString.capitalize)).toSeq
}

Any help is appreciated. Thanks, 
Michael

rose katherine toomey

unread,
Feb 21, 2014, 2:20:21 PM2/21/14
to scala-salat
If you'd like to submit a pull request and some tests, I'd be happy to include it.

Michael Ludwig

unread,
Feb 21, 2014, 2:29:39 PM2/21/14
to scala...@googlegroups.com
So is this a "it would theoretical possible to do that" or do you see any technical limitations?

I don't know much about the Salat internals. That is why I am asking. Not that I waste my time if you see many technical problems with it.

Best regards,
Michael

rose katherine toomey

unread,
Feb 21, 2014, 2:48:10 PM2/21/14
to scala-salat
It's theoretically possible.  Technically, it would involve inspecting the type of map keys and if necessary serializing them into Mongo/deserializing them back from Mongo.  It's generic functionality that could also allow people to use, say, a string representation of a date as a map key.
Reply all
Reply to author
Forward
0 new messages