Skip to content

Instantly share code, notes, and snippets.

@lucasrpb
Last active June 4, 2016 02:10
Show Gist options
  • Save lucasrpb/2fe1942d552ce15649b4fdb0cde91a43 to your computer and use it in GitHub Desktop.
Save lucasrpb/2fe1942d552ce15649b4fdb0cde91a43 to your computer and use it in GitHub Desktop.
// GET INFO AT RUNTIME ABOUT SOME INSTANCE
def extractInfo(e: Any): Unit = {
val im = cm.reflect(e)
val tpe = im.symbol.typeSignature.typeSymbol.asType.toType
tpe match {
case t if t <:< typeOf[String] => {
println("string")
}
case t if t <:< typeOf[Int] => {
println("int")
}
case t if t <:< typeOf[Iterable[_]] => {
println("list[]")
val list = e.asInstanceOf[Iterable[_]]
list.foreach(e => extractInfo(e))
println()
}
case t if t <:< typeOf[Product] => {
println(s"case class ${t.typeSymbol.name.toString} \n")
val fields = tpe.decls
fields.foreach(f => {
val field = f.asTerm
//val fieldX = u.tpe.decl(TermName("name")).asTerm.accessed.asTerm
if(field.isVal){
val fm = im.reflectField(field)
println(field.name.toString)
extractInfo(fm.get)
println()
}
})
}
case _ =>
}
}
case class Cep(street: String)
case class UserInfo(name: String, list: List[Cep])
val u = UserInfo("Lucas", List(Cep("Rua X")))
extractInfo(u)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment