Skip to content

Instantly share code, notes, and snippets.

@johntbush
Last active August 29, 2015 14:05
Show Gist options
  • Save johntbush/7922e49ff8034c036cae to your computer and use it in GitHub Desktop.
Save johntbush/7922e49ff8034c036cae to your computer and use it in GitHub Desktop.
before learning about partial functions and Function.chain
def transformField(value: JsValue, fieldName: String, displayName: String): JsValue = {
value.transform(
(__ \ fieldName).json.update(
__.read[JsString].map { s => Json.obj("DisplayName" -> displayName, "Value" -> s)}
)
)
match {
case s: JsSuccess[JsObject] => s.get
case e: JsError => {
Logger.error("Errors: " + JsError.toFlatJson(e).toString())
JsNull
}
}
}
def transform(envId: String, value: JsValue, exception: SomeObject, overridableFieldsMap : HashMap[String, String]): JsValue = {
var newValue = value
typeOf[SomeObject].members.filter(!_.isPrivate)
.filter(_.isMethod)
.filter(_.asMethod.isAccessor)
.filter(_.asMethod.fullName.startsWith("services."))
.foreach(
methodScope => {
var term = methodScope.asTerm
var mirror = runtimeMirror(exception.getClass.getClassLoader)
val instanceMirror = mirror.reflect(exception)
val fieldMirror = instanceMirror.reflectField(term)
val fieldName = term.name.toString
val displayName = overridableFieldsMap.getOrElse(fieldName, fieldName)
newValue = transformField(newValue, fieldName, displayName)
}
)
newValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment