I'm currently dealing with the AWS Java SDK, which expects Java types.
I've been using JavaConverters
to explicitly converts types like maps and lists to java. I've been running into more and more complex types and found myself having to map deep into lists which is becoming unwieldy.
My current issue is I have a Map[String, List[String]]
converting the overall object to java is simple but I'm being hit by type erasure when pattern matching the list in the map.
val t = m.map{
case (k, v: List[String]) => k -> v //type erasure so unreachable
}
I found a great explanation of Type Tags in Scala which is an elegant solution I'd like to implement. Scala: What is a TypeTag and how do I use it?
I'm just unsure how I'm implement this into my pattern match.
I've implemented a method that looks like so
def isNestedMap[A : TypeTag](xs: A): Boolean = typeOf[A] match {
case t if t =:= typeOf[Map[String, List[String]]] => true
case t => false
}
And a new case case (k, v) if isNestedMap(v) => k -> "beans"
but this always return false as my method is seeing types as an Any
.
Any ideas how to surmount this? Or better yet a way of recursively convert deep lists or maps to their java equivalents?
Thanks all.
EDIT: The Map is actually a type of Map[String, Any]
so I need to know the type of Any
before I can convert it to Java