Re: an embedded map in a map

106 views
Skip to first unread message

rktoomey

unread,
Jul 11, 2012, 2:27:13 PM7/11/12
to scala-salat
Hi Todd,

Salat doesn't support collections embedded in collections at present.
I will add that with 1.9.1 but haven't done it yet.

Best,
Rose

On Jul 11, 1:52 pm, RB H <rbhd...@gmail.com> wrote:
> Hi,
>
> When I save a case class:
>
> case class TrackEvent(blah: String, measurement: Map[String, Map[String,
> Double]]) using grater:
>
> grater[TrackEvent].asDBObject(newTrackEvent)
>
> It's being stored in Mongo as Lists instead of Maps; example:
>
> "measurement" : { "intensity" : [ [ "intensity", 3 ] ], "time" : [ [
> "minutes", 60 ] ] }
>
> Apologies if this a known issue.
>
> Suggestions?  Ideas?
>
> -Todd

rktoomey

unread,
Jul 12, 2012, 12:02:34 PM7/12/12
to scala-salat
Sorry, Marcus, not yet. I'll try to do it in 1.9-SNAPSHOT soon.

On Jul 12, 11:56 am, Marcus Downing <marcus.down...@gmail.com> wrote:
> Are embedded collections implemented in 1.9.1-SNAPSHOT? I just got this
> error when decomposing Map[String, List[String]]:
>
> [error] (run-main) java.lang.RuntimeException: FromJValue: expected types
> for Traversable but instead got:
> [error]
> TypeRefType(SingleType(ThisType(scala),scala.Predef),scala.Predef.Map,List( TypeRefType(SingleType(ThisType(scala),scala.Predef),scala.Predef.String,Li st()),
> TypeRefType(SingleType(SingleType(ThisType(<no
> symbol>.<root>),scala),scala.package),scala.package.List,List(TypeRefType(S ingleType(ThisType(scala),scala.Predef),scala.Predef.String,List())))))
> java.lang.RuntimeException: FromJValue: expected types for Traversable but
> instead got:
> TypeRefType(SingleType(ThisType(scala),scala.Predef),scala.Predef.Map,List( TypeRefType(SingleType(ThisType(scala),scala.Predef),scala.Predef.String,Li st()),
> TypeRefType(SingleType(SingleType(ThisType(<no
> symbol>.<root>),scala),scala.package),scala.package.List,List(TypeRefType(S ingleType(ThisType(scala),scala.Predef),scala.Predef.String,List())))))
> root>),scala),scala.package),scala.package.List,List(TypeRefType(SingleType (ThisType(scala),scala.Predef),scala.Predef.String,List())))))
> at scala.sys.package$.error(package.scala:27)
> at
> com.novus.salat.json.FromJValue$$anonfun$apply$5.apply(ToJValue.scala:102)
> at com.novus.salat.json.FromJValue$$anonfun$apply$5.apply(ToJValue.scala:99)
> at scala.Option.map(Option.scala:133)
> at com.novus.salat.json.FromJValue$.apply(ToJValue.scala:99)
> at
> com.novus.salat.json.FromJValue$$anonfun$apply$5$$anonfun$apply$7.apply(ToJ Value.scala:115)
> at
> com.novus.salat.json.FromJValue$$anonfun$apply$5$$anonfun$apply$7.apply(ToJ Value.scala:115)
> at
> scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala :194)
> at
> scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala :194)
> at
> scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala: 59)
> at scala.collection.immutable.List.foreach(List.scala:45)
> at scala.collection.TraversableLike$class.map(TraversableLike.scala:194)
> at scala.collection.immutable.List.map(List.scala:45)
> at
> com.novus.salat.json.FromJValue$$anonfun$apply$5.apply(ToJValue.scala:115)
> at com.novus.salat.json.FromJValue$$anonfun$apply$5.apply(ToJValue.scala:99)
> at scala.Option.map(Option.scala:133)
> at com.novus.salat.json.FromJValue$.apply(ToJValue.scala:99)
> at com.novus.salat.ConcreteGrater$$anonfun$6.apply(Grater.scala:311)
> at com.novus.salat.ConcreteGrater$$anonfun$6.apply(Grater.scala:306)
> at
> scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala :194)
> at
> scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala :194)
> at
> scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala: 59)
> at scala.collection.immutable.List.foreach(List.scala:45)
> at scala.collection.TraversableLike$class.map(TraversableLike.scala:194)
> at scala.collection.immutable.List.map(List.scala:45)
> at com.novus.salat.ConcreteGrater.fromJSON(Grater.scala:306)
> at com.novus.salat.ConcreteGrater.fromJSON(Grater.scala:108)
> at com.novus.salat.Grater.fromJSON(Grater.scala:73)
> at model.RegionData$.load(regions.scala:19)
>
> The two signatures look the same to me.

rktoomey

unread,
Jul 12, 2012, 12:28:22 PM7/12/12
to scala-salat
Well, Salat can transform case classes <-> maps (see grater.toMap and
grater.fromMap), but... for something like Map[String, List[String]]
I'd just use Casbah really.

I'm not sure how freeform deserialization would work but if you want
to file an issue and describe how you would like it to behave, I'd
certainly consider trying to do it... :)

Best,
Rose

On Jul 12, 12:21 pm, Marcus Downing <marcus.down...@gmail.com> wrote:
> There's no hurry, I've found another way of achieving what I needed today.
> But I think it's an important thing for Salat to have.
>
> On a related note, will it be possible to use Salat to do collections
> without reference to case classes? ie to decode something like a
>
> Map[String, List[String]]
>
> without it being part of a
>
> case class Something(field: Map[String, List[String]])
>
> Or would that be against its stated goal of only doing case classes?
>
> (btw, Salat continues to make my job easier. Continued kudos.)

rose katherine toomey

unread,
Jul 12, 2012, 12:33:49 PM7/12/12
to scala-salat
Actually, people have asked for projection in SalatDAO to support
variad typing...

Which is quite similar, but obviously you have to manually provide the
type information:

// method signature would actually be uglier as I need a manifest for each type
FooDAO.projection[A, B, C, D](/* query */, /* ordered list of keys
*/): Iterable[(A, B, C, D)]

I keep meaning to add that to SalatDAO but first I have to devise a
good way of distinguishing types that need a grater from types whose
deserialization is handled by mongo-java-driver (not so simple now
that grater supports traits and abstract superclasses now).

So there's no reason you couldn't extend that idea to deserializing
based on supplied types.

Thanks for a good idea, Marcus. File an issue and I'll add this to the stack.
Reply all
Reply to author
Forward
0 new messages