Created
October 8, 2017 16:32
-
-
Save xerial/370e4b2e5e8caf4cdc0311df9ce0c486 to your computer and use it in GitHub Desktop.
Using Surface to convert objects into 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
import wvlet.surface | |
import wvlet.surface.Surface | |
import scala.reflect.runtime.{universe => ru} | |
class MapConverter[A](surface: Surface) { | |
def toMap(a: A): Map[String, Any] = { | |
val m = Map.newBuilder[String, Any] | |
for (p <- surface.params) { | |
m += (p.name -> p.get(a)) | |
} | |
m.result() | |
} | |
} | |
object MapConverter { | |
def of[A: ru.TypeTag]: MapConverter[A] = new MapConverter(surface.of[A]) | |
def toMap[A: ru.TypeTag](a: A): Map[String, Any] = { | |
MapConverter.of[A].toMap(a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment