Skip to content

Instantly share code, notes, and snippets.

@johntbush
Created August 15, 2014 06:00
Show Gist options
  • Save johntbush/5114592b7aec85d37fdf to your computer and use it in GitHub Desktop.
Save johntbush/5114592b7aec85d37fdf to your computer and use it in GitHub Desktop.
After learning about Function.chain...
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