Last active
August 29, 2015 14:16
-
-
Save frgomes/10d45151a26f76bde406 to your computer and use it in GitHub Desktop.
Scala - Transforms an Object into a Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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