Juan Cruz Basso shares the real secrets of Dataweave programming using Mule, Visual Studio Code, Dataweave Playground, and the Dataweave CLI.
Video online: https://youtu.be/05Z-tJuvvVQ
Unblocking The Main Thread Solving ANRs and Frozen Frames
20230721_OKC_Meetup_MuleSoft.pptx
1. July 21, 2023 12:30pm (EST)
Oklahoma City MuleSoft Meetup Group
The Secrets of Dataweave
2. 2
● Introductions
● Our Sponsors
● The Secrets of Dataweave
● Trivia Game
● SPECIAL Dataweave Challenge!!!
● Exciting announcements
Welcome!
3. 3
●Meet the Organizers:
○ Victor Felisbino
○ Ryan Hoegg
○ Diane Kesler
●Meet the Sponsors:
○ AVIO Consulting – Mike Slack
○ Hoegg Software - Ryan Hoegg
Introductions
Where are you joining this Meetup From?
5. Juan Cruz Basso
Senior Software Engineer at
Avio Consulting
• Software Engineer
• 13+ years in the world of integrations
• MuleSoft Certified Developer L1/L2
• MuleSoft Certified Integration Architect
• MuleSoft Certified Platform Architect
• MuleSoft Mentor
/jcbasso
6. Agenda
● Dataweave basic structure
● Functional programing principal concepts
● Functional concepts applied in Dataweave
● Modularization
● Demo
● Dataweave tips
● Q & A
7. Structure of a Dataweave Script
● It has a header and a body separated by (---)
○ Header: Contains language directives, such as input/output format, the version of DataWeave to
execute, properties of readers/writers, and definitions of variables or functions.
○ Body: Contains the DataWeave expression that generates the output of the script.
7
8. Functional programming concepts
● Pure functions
● First class functions
● High order functions
● Lamda functions
● Function composition
9. Pure Functions
%dw 2.0
output application/java
fun incOne(param,param2) = param + 1
—
incOne(payload,1)
9
Payload: 1
Payload: 2
incOne(1,1) Payload: 2
incOne(2,1) Payload: 3
%dw 2.0
output application/java
fun incOne(param,param2) = param + 1
—
payload incOne 1
Execution Nro 1
Execution Nro 2
Infix Notation Prefix Notation
10. First Class Functions
%dw 2.0
output application/java
fun incOne(param) = param + 1
fun processArray(array, func) = array map
func($)
—
processArray(payload,incOne)
10
%dw 2.0
output application/java
fun incOne(param) = param + 1
fun processArray(array, func) = array map
func($)
—
payload processArray incOne
%dw 2.0
output application/java
var incOne = (param) -> param
+ 1
—
incOne(payload)
%dw 2.0
output application/java
fun incOne(param) = param +
1
—
payload incOne
Declare a function as a variable
Infix Notation
Prefix Notation
11. High Order Functions
11
%dw 2.0
output application/java
var inputData = [1]
fun processArray(array, func) = array map func($)
fun incrementFunction(increment1) = (increment2) -> (increment1 + increment2)
fun incOne(param) = incrementFunction(1)(param)
fun incTwo(param) = incrementFunction(2)(param)
---
processArray(inputData,incOne) ++ processArray(inputData,incTwo) ++ processArray(inputData,incrementFunction(5))
12. Function Composition
12
%dw 2.0
output application/java
var arrayInput = ["string1", "string2", "1string3"]
---
((arrayInput filter ($ contains "1"))
map (item,index) -> upper(item) ++ index)
%dw 2.0
output application/java
var arrayInput = ["string1", "string2", "1string3"]
var filterItems = (item,index) -> item contains "1"
fun upperPlusIndex(item,index) = upper(item) ++ index
---
map( filter (arrayInput,filterItems) , upperPlusIndex)
Prefix Notation
Infix Notation
13. Core Functions
● They are functions created for data transformation, and it is not necessary to explicitly import them
in the scripts.
● Think of them as tools for solving different problems, there are more libraries around, but they
need to be explicitly imported.
● Some commonly used core functions are:
○ map
○ filter
○ reduce
○ isEmpty
○ flatten
○ flatMap
○ pluck
○ sizeOf
○ read
○ write
○ …
14. Modularization
● Promotes code reusability
● Inside of them we can define functions, variables, types, or Dataweave scripts.
● Functions are imported by using the module name followed by ::
○ We can import all the elements of the module using:
■ import * from modules::utilsModule
○ We can import specific elements of the module using:
■ import numberToSAP from modules::utilsModule
15. Modularization in a Mulesoft API
● Promotes code reusability within the implementation of a single MuleSoft API.
● They are located within the "resources" folder of the MuleSoft application.
15
16. Modularization as an Exchange Library
● Promotes code reusability within the implementation of multiple MuleSoft APIs.
● They are published on Exchange.
● They are included as Maven dependencies in the project's pom.xml file.
17. ● MuleSoft Runtime
● CLI scripts
● Playground
● VS Code
● Dataweave in Apex (Beta)
Execution contexts
17
19. Tips when developing with Dataweave
● Use do to create partial context of execution to define “local” variables
● Use log() to trace intermediate results of the transformations for troubleshooting
● The object destructor is a powerful tool to concatenate objects, or transform them into
key/value pairs. (aka surround with () and then with {} )
● Be aware of the in the usage of () in the correct way, the parenthesis defines the scope of the
results and how the operations like ! or functions will impact.
22. 22
Trivia Game
Three winners of today’s trivia
receives:
A $30 Gift Card!
● Remember:
○ The first correct answer wins!
○ You can only receive 1 Gift Card
● Gift Cards are sponsored by
23. 23
1. How many parameters should have a function to be used with an
infix notation?
A. 1
B. 3
C. 2
D. any
Trivia Question 1
24. 24
2. How many functions can I pass as parameters in a function in
Datawave?
A. 1
B. any
C. 2
D. 0
Trivia Question 2
25. 25
3. When a function is a first-class function in a programming
language?
A. when it flights only in first class
B. when it's the first function written in the code
C. when it can be treated as an object ( having the same
"privileges" )
D. when it's the first function called of a program
Trivia Question 3
29. ● Share:
○ Tweet using the hashtag #MuleSoftMeetups
○ Invite your network to join: https://meetups.mulesoft.com/oklahoma-city/
○ Want to be a Speaker? Let us know at:
● Feedback:
○ Fill out the survey feedback – Win MuleSoft Swag! Drawing from completed surveys
○ Suggest topics for upcoming events: What are YOUR requests?
○ Contact MuleSoft at meetups@mulesoft.com for ways to improve the program
29
What’s next?