Skip to content

Instantly share code, notes, and snippets.

@frgomes
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frgomes/10d45151a26f76bde406 to your computer and use it in GitHub Desktop.
Save frgomes/10d45151a26f76bde406 to your computer and use it in GitHub Desktop.
Scala - Transforms an Object into a Map
trait Object2Map {
implicit class RichObject(o: AnyRef) {
def asMap : Map[String, Option[Any]] =
(Map[String, Option[Any]]() /: o.getClass.getDeclaredFields) {
(acc, field) =>
field.setAccessible(true)
acc + (field.getName -> Option(field.get(o)))
}
}
}
// private case class CmdLine(dir : String = ".",
// output: String = null,
// input : String = null)
//
// val args =
// cmd.asMap
// .filter{ case (k,v) => v.isDefined }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment