A New Dataweave Function Makes It Easier for You to Store, Reuse, and Share Dataweave Functions Between Flows and Mule Applications.
I’m a courseware developer and trainer atMuleSoft, and recently I’ve been spending a lot of time learning about DataWeave.
In theDataWeave1.x version included in the Mule runtime v3.8, there has been limited support for reusing DataWeave functions and variables between Transform Message components (the component that applies DataWeave code). In particular, there is currently no direct support for importing modules or libraries of DataWeave code. But starting with Mule Runtime v3.8.4 there is a new
readUrlfunction you can now use to start reusing DataWeave code across applications and even across deployments.
In this post, you’ll learn how to use the new
readUrlfunction to separate out reusable DataWeave code, and then reuse that code between multiple Transform Message components and flows, including flows in separate applications and MUnit flows. This is a nice new tool to add to your DataWeave tool box.
An example Mule project is available in thisGitHub repository. You can import this project into the latest Anypoint Studio to follow along with this post. Alternatively, you can create a new Mule project in Anypoint Studio and copy the examples from this blog post.
This project has added a folder named
src/main/resources, and then created a file named
dwfolder. When this Mule project is exported as a deployable archive file, all the files in
src/main/resourceswill be automatically added to the project’s classes folder, so will be in the classpath when the Mule application is deployed to a Mule runtime. In this sample project, the HTTP listener is listening for requests on port 8081.
The myLib.wev file contains some reusable variables and functions.
The body expression creates key names for the variables and functions. The
exchangeRatekey has as value the variable
exchangeRateFromUSDToBritishPounds. This variable acts as a constant in other DataWeave functions.
convertPricekey references the more lengthy function name
convertPriceFromUSDToBritishPounds. This function requires a parameter, which is used in the function’s expression to print out the String returned from the later
getIntroString()function, plus the result of multiplying the
inputvalue by the
formatStringkey is different from the other two keys in the body expression. The
formatStringkey has a value which is an anonymous function implementation. These types of unnamed functions are also called lambdas. The lambda requires two input parameters. The first parameter labeled
aStringis supplied to the
formatStringfunction with any string value, which then has the
formatterfunction applied to
Next, you will see how to use these functions, including
formatString, in a DataWeave flow.
The next step is to read in this external DataWeave file and use it in a Transform Message component. The first step is to use the new readUrl function to read in the DataWeave function from the classpath. Define a variable
myLibas a reference to the DataWeave file
In this example, we are embedding the DataWeave library file inside the project, so we give the URL relative to the classpath
dw/myLib.wev. If you are deploying into a customer-hosted on-prem Mule runtime, you could also store your DataWeave libraries in a common external location that you add to the Mule runtime’s classpath.
Note: This is a similar technique to the way you can store Mule application properties files in an external location. You can learn more about this technique in the Anypointproduct documentation, orTanuki Java Service Wrapperdocumentation.
Like the related read() function, readUrl() can also include a second parameter to specify the mime type, such as
application/xml. When not specified, the default mime type is
application/dw. So in this example, we could also have written:
In Anypoint Studio, the file is stored in
src/main/resources, but in the deployable archive, all the files from the
src/main/resourcesfolder are moved into the
Here is a complete Transform Message component in the convertPrice flow:
You can preview example data transformations using the Preview pane in the Transform Message component editor. In the Transform Message component, in the left-side Input pane, right click on Inbound Properties > http.query.params.
Set an example price:
In the right-side Output pane, select the Preview button, which opens the Preview pane. In the Preview pane, you should see the result of the body expression.
Change the price from 600 to 500 and verify the output in the Preview pane also changes. This shows you that you can preview live changes to DataWeave code, even when you are reading in external DataWeave files.
Here is the entirereuseDataWeaveCode.xmlfile:
Deploy the reuseDataWeaveCode project to a Mule runtime. For example, here I am deploying the project to an Anypoint Platform account from Anypoint Studio.
In this example, I deployed the application to a public Anypoint Platform URLhttp://reuse-dataweave-code.cloudhub.io.
After the application deploys, open a web client (you can use a web browser), and make a GET request to the HTTP listener. If you deployed to a Mule runtime on your local machine, the URL is
http://localhost:8081?price=300. For my CloudHub deployment, I’ll make requests from a web client to
http://reuse-dataweave-code.cloudhub.io/?price=700. You can click this URL now to try it out.
My web browser has a JSON parser extension and shows this response:
As you build up more complex DataWeave transformation for your projects, you’ll want to reuse some of your transformation logic. Today, you can do this using the readUrl() function. Please let us know what you think of this feature, and also let us know what additional modularity you’d like to see in future releases.