Transforming Data10% of the exam

DataWeave modules, functions and variables

How to build a custom .dwl module under src/main/resources and import it (module prefix, import *, named elements, aliases), how fun declarations take typed and optional parameters (including right-to-left assignment when optional parameters come first), and how global vars, do-scoped local vars and lambda vars behave.

Official exam objective
Define, use, and reuse DataWeave modules, functions, and variables, and predict outcomes.

~7 min read · 5 practice questions ·

What the exam expects

  • Create a reusable custom module in src/main/resources and know what it may and may not contain.
  • Import a module or its elements (import modules::X, import * from, named elements, as aliases) and call them with or without the X:: prefix.
  • Define functions with fun, type constraints and optional parameters, and predict how arguments bind.
  • Predict the scope and lifetime of global vars, do-scoped local variables and lambdas assigned to variables.
  • Key terms: custom module (a .dwl file of var, fun, type and ns declarations); mapping file (a .dwl file holding a complete script, called through main); alias (a local name assigned with as at import); optional parameter (a parameter with a default value); do scope (a header-and-body block that declares local variables); lambda (an anonymous function such as (s) -> upper(s)).

Key facts

Module contents
Detail
Only var, fun, type, ns; no output, --- or body
Module location
Detail
Under src/main/resources, e.g. src/main/resources/modules/MyModule.dwl
import modules::M
Detail
Calls need the prefix: M::myFunc
import * or named import
Detail
No prefix needed
Alias
Detail
as renames a module or element to avoid name clashes
Mapping file
Detail
Has a body; call it with Module::main(...)
Argument binding
Detail
Left to right; right to left when optional parameters come first or all are optional
Immutability
Detail
DataWeave variables cannot be reassigned
do scope
Detail
Local variables visible only inside the scope
Lifetime
Detail
DataWeave variables do not persist beyond the script and are not Mule event variables
Sources for this section (3)
  • What does the documented MyMapping::main(payload: { "user" : "bar" }) return?
  • Why does allParametersOptional('A') set param3 rather than param1?
  • Why does a do scope need the --- separator?
  • What does fun do relative to var for a lambda?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the MuleSoft Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 40 lessons of the MuleSoft Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Practice exams purchase adds inline bank questions and topic practice (5 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Unlimited timed practice exams

Practice for the MuleSoft Developer exam