Create and Import Custom Functions in DataWeave
In this written and video tutorial, learn how to create custom functions in DataWeave and how to import the files and everything from the files.
Join the DZone community and get the full member experience.
Join For FreeThe article will showcase the creation of a file with custom functions and then show how to import them in DataWeave.
Creating a File With Custom Functions
A .dwl file for custom functions is created in src/main/resources under a folder called module.
Importing Custom Functions in Dataweave
1. Import File
The custom functions file is under the module folder, so while importing, we need first to mention the folder name followed by the scope resolution operator (::
) and then the file name.
import <folder name>::<file name>
We are just importing the file, so we need to mention the file name followed by the scope resolution operator (::
) then the function while calling the function.
<file name>::<function>
Calling only one function:
Calling both the functions:
2. Import Everything From File
We use an asterisk (*
) followed by from
keyword, which means import everything from the file to import everything.
Import * from <folder name>::<file name>
Since we are importing everything this time, we don’t need to mention the file name while calling the functions, or else it will throw an error.
Without file name:
With file name:
Notes
- We can directly create the functions file under the src/main/resource folder, but it’s best practice to create a folder and store the file in that.
- Naming conventions are very important.
Preferred
|
Not Preferred |
camelCase -> customFunctions snake_case -> custom_functions PascalCase -> CustomFunctions |
Kebab-case -> custom-functions |
Opinions expressed by DZone contributors are their own.
Comments