Introduction
Recently, I encountered the following piece of Mule Dataweave 2.0 function code.
fun getEmployeePovince(worker) = getStateAbbrevation(worker) match { case "AB" -> "AB" case "BC" -> "BC" case "MB" -> "MB" case "NB" -> "NB" case "NL" -> "NL" case "NS" -> "NS" case "NT" -> "NT" case "NU" -> "NU" case "ON" -> "ON" case "PE" -> "PE" case "QC" -> "QC" case "SK" -> "SK" case "YT" -> "YT" else -> "ZZ" }
The purpose of the above function is to get home province's code of a Canadian employee. The Canadian province code is like, BC for British Columbia, ON for Ontario, etc. The input is an xml object which contains information about the employee. If the worker's province code is not among the list, default to "ZZ".
The above code works and about to go to production. However, this kind of code is really not very cool to say the least. It is just an amateur's code!
Improvement One
First of all, the gist of this kind of problem is to find a match from a given list of Strings. Mule Dataweave 2.0 provides a function, namely find. The documentation can be found here.. The find function works like the following:['aa', 'bb', 'cc'] find 'xy' //return [], empty array ['aa', 'bb', 'cc', 'aa'] find 'aa' //return [0, 3]Now the code using find function should be clear. If the return array from the find is empty (isEmpty(findFunction)), use the input, otherwise, use default, "ZZ".
The following is the datawave code:
%dw 2.0 var CanadianProvinces = ["BC", "MB", "NB", "NL", "NS", "NT", "NU", "NT", "NU", "ON", "PE", "QC", "SK", "YT"] var pv = payload.state fun findAKey(aKey) = CanadianProvinces find aKey output application/json --- { province: if(isEmpty(findAKey(pv))) "ZZ" else pv, }The above code is much cleaner. We put the constants of the province code into an array, and use find function. However the above code is still not good for maintenance. Let's see if I want to use the same to logic to US state's code. We have to modify the dataweave code. We can further improve the code. That is to put the constant into property file.
Improvement Two
First, we put the two constants to the yaml property file as the following:provinces: ["BC", "MB", "NB", "NL", "NS", "NT", "NU", "NT", "NU", "ON", "PE", "QC", "SK", "YT"] constants: default: "ZZ"
%dw 2.0 var provinces = p('provinces') var pv = payload.state var dp = p('constants.default') fun findAKey(aKey) = provinces find aKey output application/json --- { provice: if(isEmpty(findAKey(pv))) dp else pv, }
Take Aways
- Two dataweave 2.0 functions: switch and find
- Array constants in yaml property file
- When we encounter some strange code, we should think about the improvement. There is always ways to write elegant code.
Do you realize there's a 12 word sentence you can communicate to your crush... that will trigger intense emotions of love and instinctual attraction for you buried inside his chest?
ReplyDeleteThat's because hidden in these 12 words is a "secret signal" that triggers a man's impulse to love, treasure and look after you with his entire heart...
12 Words Who Trigger A Man's Love Response
This impulse is so hardwired into a man's brain that it will make him work harder than before to take care of you.
As a matter of fact, triggering this influential impulse is so important to getting the best ever relationship with your man that the second you send your man one of these "Secret Signals"...
...You will soon find him open his soul and heart to you in such a way he haven't experienced before and he will identify you as the one and only woman in the universe who has ever truly interested him.
This comment has been removed by the author.
ReplyDeleteThank you for sharing this information
ReplyDeleteIgrowsoft sloutions
nice ..................!
ReplyDeletemulesoft training
pega training
servicenow training training