mapWithDefault is useful to handle those cases where the source message field is expected, but might not be existed.
Example in previous firstname lastname to fullname example, both firstname and lastname is expected to be concatenated.
Before implement mapWithDefault:
Case 1:
Both firstname and lastname segment is existed and with values. Display queue show value “John”. Mapping test run-time success.
Case 2:
Firstname is existed but with empty string value. Display queue show empty string value “”. Mapping test run-time success.
Case 3:
Firstname is not existed. Display queue show empty context and only have <null> displayed. Mapping test run-time error.
The concat function expected first parameter must feed in value, but none provided and error out.
After implement mapWithDefault:
Case 4:
Firstname is not existed. Display queue show empty context and only have <null> displayed. Mapping test run-time success.
Let’s see the queue of mapWithDefault.
Firstname’s mapWithDefault received input value “SUPPRESS” because firstname not existed. MapWithDefault mapped it with empty string value “”.
Lastname’s mapWithDefault received input value”Locke” because lastname existed and with value “Locke”. Since field is existed, so take existed value “Locke”.
General advise, if in situation where the expected field might or might not existed but is mandatory in mapping, or the source field is not primary and optional in source system, then it is advisable to include mapWithDefault. This will avoid the mapping failed badly in run-time. Remember, all input data is evil!
Handle Empty Context with mapWithDefault
Nice Explanation>..
I like the example. easy to follow and understand.
nic eexplanation 🙂
Great explanation thanks for your efforts and explanation.