3 ways to import a function/module in DataWeave 2.0
There are different ways to import a module in DataWeave for functions that aren't part of the core: import the module directly, import all functions from a module, or import one or more specific functions from a module.
There are different ways to import a module in DataWeave. You need to do this for the modules that are not a part of the core functions. Here is a list of all the modules in DataWeave: Mule Docs - DataWeave Reference.
1. Import module directly
You can simply import the module without specifying the functions’ names. If you do this, you will need to reference the module every time you want to use that module’s function. Like this:
%dw 2.0
output application/json
import dw::core::Strings
---
Strings::capitalize("alex martinez")
To reference the function, you need to use the name of the module, then add two colons (::), and finally, the name of the function. After doing this, you can continue using the function as usual.
2. Import all functions from a module
You can import all of the functions from the module by using the asterisk (*) and using the keyword “from.” This will let you use the function directly without referencing the module’s name every time. Like this:
%dw 2.0
output application/json
import * from dw::core::Arrays
---
{
field1: [true, false] every ($),
field2: [true, false] some ($)
}
This approach, however, will import all of the functions that are found inside the module. Normally, this doesn’t affect performance. But I personally want to see which functions exactly I am importing to my code. For example, in the previous script, I don’t know if “every” or “some” are functions that come from the Arrays module or if they’re core functions. For this reason, my favorite approach is the 3rd option described below.
3. Import one or more functions from a module
To explicitly import each function separately, you have to list them instead of just writing the asterisk (*). Like this:
%dw 2.0
output application/json
import every, some from dw::core::Arrays
---
{
field1: [true, false] every ($),
field2: [true, false] some ($)
}
This will still let you call the function directly. The difference is that you’ll have to list all of the functions you’re using. I prefer this approach because sometimes you may not know which function comes from which module (especially for big scripts). With this, you can just search for the function and see which module is importing it.
Of course, it can become annoying if you’re changing functions often, and you remove and re-add them several times. You will also get an error if you forget to list one of the functions that you’re using.
For example, the following script shows an error because the “some” function is not listed in the import statement:
%dw 2.0
output application/json
import every from dw::core::Arrays
---
{
field1: [true, false] every ($),
field2: [true, false] some ($) // Unable to resolve reference of: `some`.
}
The option you choose really depends on your personal preference or the standards of your project.
By the way, do you know how to create your own modules and reference them for your DataWeave scripts? If not, check this out! - Custom Modules in Mule 4 - DataWeave 2.0.
-Alex
FAQs
Frequently asked questions about this post.
-
Why do I need to import a module in DataWeave at all?
You only need to import modules that are not part of the core functions. The core functions are available without an import, while everything else lives in a module you have to bring in first.
-
What are the 3 ways to import a function or module in DataWeave 2.0?
First, you can import the module directly with
import dw::core::Stringsand reference it each time, likeStrings::capitalize("alex martinez"). Second, you can import all functions from a module with the asterisk and thefromkeyword, likeimport * from dw::core::Arrays, so you call functions without the module prefix. Third, you can import one or more specific functions by listing them, likeimport every, some from dw::core::Arrays. -
How do I reference a function when I import the module directly?
You use the name of the module, then add two colons (
::), and finally the name of the function, for exampleStrings::capitalize("alex martinez"), after which you can continue using the function as usual. -
What's the difference between importing all functions with the asterisk and listing specific functions?
Both approaches let you call the function directly without referencing the module name. Importing all functions with
import * frombrings in everything in the module, while listing specific functions likeimport every, some fromrequires you to name each function you use, which makes it clear exactly which functions are being imported and lets you search a function to see which module it comes from. -
Why does my script throw an 'Unable to resolve reference' error when listing imported functions?
When you explicitly list the functions you import, you will get an error if you forget to list one of the functions you are using. For example, importing only
everywhile also callingsomeproduces an error becausesomeis not listed in the import statement. -
Which import approach should I use?
The option you choose really depends on your personal preference or the standards of your project. The author's favorite is listing functions individually, because for big scripts you may not know which function comes from which module and you can just search for the function to see which module imports it.