I'm trying to migrate from Dataweave 1.0 to 2.0 and please don't consider the variable in that given DWL. I'm facing the following error:
Unable to resolve reference of zeroPad2.
Unable to call `map` with (`Any`, ($, $$) -> `?`):
-
55| payload.DTOApplication.*DTOLossHistory[?($.@StatusCd == "Active")] default [] orderBy -($.@LossDt) map using (dispIdx = zeroPad2($$+1)) {
^^^^^^^^
- Unable to resolve reference of zeroPad2.
Actual: `map(items: `Array<T>`, mapper: (item: `T`, index: `Number`) -> `R`) -> `Array<R>``
-
55| payload.DTOApplication.*DTOLossHistory[?($.@StatusCd == "Active")] default [] orderBy -($.@LossDt) map using (dispIdx = zeroPad2($$+1)) {
^^^^^^^^
- Unable to resolve reference of zeroPad2.
Actual: `map(value: `Null`, mapper: (item: `Nothing`, index: `Nothing`) -> `Any`) -> `Null``
Dataweave 1.0 : https://github.com/Manikandan99/Map_request/blob/main/Dataweave_1.0_Mule_3.dwl
I could able to migrate the all the values except from at line 54:
(
payload.DTOApplication.*DTOLossHistory[?($.@StatusCd == "Active")] default [] orderBy -($.@LossDt) map using (dispIdx = zeroPad2($$+1)) {
'CauseOfLoss$dispIdx': $.@LossCauseCd,
'DateOfLoss$dispIdx': $.@LossDt,
'IncurredAmt$dispIdx': $.@TotalIncurred
}
)
I'm try to dataweave migration: https://github.com/Manikandan99/Map_request/blob/main/Dataweave_2.0_mule_4.dwl
Actual payload : https://github.com/Manikandan99/Map_request/blob/main/input_xml_request_for_transformation.xml
Any ideas please on how to migrate the given dataweave 1.0 to 2.0?
Clearly zeroPad2() is not a built-in function from DataWeave. I can make an educated guess that it is a custom global MEL function in the Mule 3 application that is being called from the DataWeave 1.0 script. Since Mule 4 does not support MEL functions you will need to reimplement that function in DataWeave 2.0 (example fun zeroPad2(n)=...
) to do the same job. Looks from the name and parameters that it is just used to pad with zeros the number of the index in the map operation so it should not be difficult to reimplement the same logic. Specially because it looks like it does not even parametrize the number of positions to pad.
As an example you can use DataWeave 2.0 built in function leftPad() to implement a similar result:
%dw 2.0
output application/json
import * from dw::core::Strings
fun zeroPad2(n)=leftPad(n, 2, "0")
---
zeroPad2(3)
Output: "03"
But unless the logic is more complex I advise to use leftPad() directly in your script and avoid creating a function that provides no value.
As an example in your script you could replace zeroPad2() usage by another function call or expression:
%dw 2.0
import * from dw::core::Strings
...
---
...map using (dispIdx = leftPad(n, $$+1, "0"))...
Remember that you didn't provide details of zeroPad2() so it is up to you to implement a similar functionality.