%dw 2.0
import dw::core::Strings
output application/json
---
{ 'plural': Strings::pluralize("box") }
DataWeave Reference
DataWeave functions are packaged in modules. Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For DataWeave in Mule 3 apps, refer to DataWeave version 1.2 operators. For other Mule versions, you can use the version selector in the DataWeave table of contents.
Functions in the Core (dw::Core
) module are imported automatically into your DataWeave scripts. To use other modules, you need to import the module or functions you want to use by adding the import directive to the head of your DataWeave script, for example:
-
import dw::core::Strings
-
import camelize, capitalize from dw::core::Strings
-
import * from dw::core::Strings
The way you import a module impacts the way you need to call its functions from a DataWeave script. If the directive does not list specific functions to import or use * from
to import all functions from a function module, you need to specify the module when you call the function from your script. For example, this import directive does not identify any functions to import from the String module, so it calls the pluralize
function like this: Strings::pluralize("box")
.
The next example identifies a specific function to import from the String module, so it can call the method like this: pluralize("box")
.
%dw 2.0
import pluralize from dw::core::Strings
output application/json
---
{ 'plural': pluralize("box") }
The next example imports all functions from the String module, so it can call the method like this: pluralize("box")
.
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ 'plural': pluralize("box") }
Function Signatures
Each DataWeave function in the DataWeave Reference is identified by its function signature. A function specifies a function name, zero or more parameters, and a return type.
Basic syntax of a two-parameter function signature:
function(parameterType,parameterType): returnType
For example, the signature contains(String, String): Boolean
for the contains
function has two parameters, each of which accepts a value of the String type. The function returns a Boolean value.
Parameters in the function signature correspond, in order, to their named parameters, which are described in Parameters tables for each signature. For example, in contains(String, String): Boolean, the Parameters table indicates that text
is the first parameter and toSearch
is the second.
Many DataWeave functions are overloaded to handle different data types. There is a unique function signature for each variant of the function. For example, isEmpty is overloaded to support an input value of an Array, String, Object, or Null type.
For more information on the data types, see Type System.
Type Parameters in Function Signatures
Function signatures can contain type parameters, which are similar to generics in some programming languages. For example, the Array<T>
in contains(Array<T>, Any): Boolean
indicates that the array can contain elements of any type (T
) that DataWeave supports. By contrast, an array of a specific type or types specifies the type, for example, Array<String>
, Array<Number>
, or for both types Array<String|Number>
.
Function Types in Function Signatures
Functions can be passed as arguments. Some parameters within function signatures are function types that take one or more parameters of the form parameterName:Type
and have a return type (→ ReturnType
). For example, the function type (item: T, index: Number) → R)
is a parameter type of the map function (map(Array<T>, (item: T, index: Number) → R): Array<R>
). The function type accepts a value of any type T
and a value of type Number, which serve as parameters to return a value of type R
.