-
-
Save johntbush/7922e49ff8034c036cae to your computer and use it in GitHub Desktop.
before learning about partial functions and 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 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