Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get a list of all case class attributes with shapeless
import shapeless._
import shapeless.poly._
import shapeless.record._
import shapeless.ops.record._
import shapeless.ops.hlist.{Mapper,ToTraversable}
import shapeless.tag._
final case class Message(id: Int, title: String, body: String)
trait ToAttributes[T] {
def toAttributes(v: T): Seq[String]
object Attributes {
object symbolName extends Poly1 {
implicit def atTaggedSymbol[T] = at[Symbol with Tagged[T]](
implicit def familyFormat[T, Repr <: HList, KeysRepr <: HList, MapperRepr <: HList](
implicit gen: LabelledGeneric.Aux[T, Repr],
keys: Keys.Aux[Repr, KeysRepr],
mapper: Mapper.Aux[symbolName.type, KeysRepr, MapperRepr],
traversable: ToTraversable.Aux[MapperRepr, List, String]
): ToAttributes[T] =
new ToAttributes[T] {
def toAttributes(v: T): Seq[String] = keys().map(symbolName).toList.toSeq
def toAttributes[T](v: T)(implicit c: ToAttributes[T]): Seq[String] = c.toAttributes(v)
import Attributes._
val message = Message(10, "foo", "bar")
val attributes = toAttributes(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.