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:
mapwithdefault_before

Case 1:
Both firstname and lastname segment is existed and with values. Display queue show value “John”. Mapping test run-time success.
mapwithdefault_case1
Case 2:
Firstname is existed but with empty string value. Display queue show empty string value “”. Mapping test run-time success.
mapwithdefault_case2
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.
mapwithdefault_case3
 
After implement mapWithDefault:
mapwithdefault_after
 
Case 4:
Firstname is not existed. Display queue show empty context and only have <null> displayed. Mapping test run-time success.mapwithdefault_case4
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”.
mapwithdefault_case4_explain
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
Tagged on:         

4 thoughts on “Handle Empty Context with mapWithDefault

  • January 23, 2015 at 12:11 pm
    Permalink

    Nice Explanation>..

    Reply
  • April 17, 2015 at 3:16 pm
    Permalink

    I like the example. easy to follow and understand.

    Reply
  • June 18, 2015 at 11:39 pm
    Permalink

    nic eexplanation 🙂

    Reply
  • March 2, 2023 at 6:04 pm
    Permalink

    Great explanation thanks for your efforts and explanation.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *