Introduction
Recently, I encountered the following piece of Mule Dataweave 2.0 function code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:
1 2 3 4 5 6 7 8 9 10 | %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, } |
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.