Created
August 15, 2014 06:00
-
-
Save johntbush/5114592b7aec85d37fdf to your computer and use it in GitHub Desktop.
After learning about Function.chain...
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
def transform[T: TypeTag: ClassTag](json: JsValue, bean: T, overridableFieldsMap : HashMap[String, String]): JsValue = { | |
val functionCalls: Seq[(JsValue) => JsValue] = typeOf[T].members.filter(!_.isPrivate) | |
.filter(_.isMethod) | |
.filter(_.asMethod.isAccessor) | |
.filter(_.asMethod.fullName.startsWith("services.")) | |
.map( | |
methodScope => { | |
var term = methodScope.asTerm | |
var mirror = runtimeMirror(bean.getClass.getClassLoader) | |
val instanceMirror = mirror.reflect(bean) | |
val fieldMirror = instanceMirror.reflectField(term) | |
val fieldName = term.name.toString | |
val displayName = overridableFieldsMap.getOrElse(fieldName, fieldName) | |
(x:JsValue) => { | |
transformField(x, fieldName, displayName) | |
} | |
} | |
).toSeq | |
scala.Function.chain(functionCalls)(json) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment