trait TopicTrait
case class Topic1(name: String) extends TopicTrait
case class Topic2(role: Int) extends TopicTrait
//KafkaConsumer fetches topic and serialized json string (data) of either Topic1 or Topic2
def processConsumerData(Topic: String, data: String) {
val json = parse(data)
Topic match {
case "topic1" => {
//preference is to deserialize and not just send json string is to confirm json received is at least correct format
val item = json.extract[Topic1]
MyActor ! item
}
case "topic2" => {
val item = json.extract[Topic2]
MyActor ! item
}
}
}
trait TopicTrait
case class Topic1(name: String)
case class Topic2(role: Int)
//I'd like to be able to create map or list of topic name to topic class, eg:
val topicMapper = Map[String, TopicTrait]("topic1" -> *what goes here* Topic1, "topic2" -> Topic2)
def processConsumerData(topic: String, data: String) {
val json = parse(data)
val item = json.extract[*what goes here*topicMapper.get(topic).get] //I know .get is bad
MyActor ! item
}
--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
trait MyTopics
case class MyTopic1(name: String) extends MyTopics
case class MyTopic2(age: Int) extends MyTopics
object Topic2Class extends App {
implicit val formats = net.liftweb.json.DefaultFormats
val topicMapper: Map[String, TypeInfo] = Map(
"topic1" -> TypeInfo(classOf[MyTopic1], None),
"topic2" -> TypeInfo(classOf[MyTopic2], None)
)
val topic1Json = """{"name":"Bob"}"""
val topic2Json = """{"age":20}"""
// val topic = "topic1"
// val json = parse(topic1Json)
val topic = "topic2"
val json = parse(topic2Json)
topicMapper.get(topic) match {
case Some(topicManifest) =>
val res = extract(json, topicManifest)
println(s"res $res")
case None =>
println("No match")
}
}
Another option is to leverage lift-json's type extraction directly by using `ShortTypeHints` or your own TypeHints subclass that maps a topic name to a class. Then you could do e.g.:
extract(myJson ~ ("jsonClass" -> topic))
That feels maybe slightly cleaner, but maybe not heh.
Thanks,
Antonio