Saturday, October 26, 2019

Mule Integration - Write Elegant Code

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

  1. Two dataweave 2.0 functions: switch and find
  2. Array constants in yaml property file
  3. When we encounter some strange code, we should think about the improvement. There is always ways to write elegant code.

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...