Given the case class A
, I can extract its field names using Shapeless using the following snippet:
val fieldNames: List[String] = {
import shapeless._
import shapeless.ops.record.Keys
val gen = LabelledGeneric[A]
val keys = Keys[gen.Repr].apply
keys.toList.map(_.name)
}
This works well, but how can I implement this in a more general way so that I can conveniently use this method for arbitrary classes, for example
val fields: List[String] = fieldNames[AnyCaseClass]
Is there a library that is already doing this for me?
source
share