top of page
lp.jpeg

Blog

Tags:

DataWeave 2.0 scopes for local variables: 'using' vs. 'do' operators



 

In this post:

 


I had previously written a post with my top 5 DataWeave tips to make your life easier where I talked about the do operator. However, I’ve seen a lot of people asking about the using operator since it was inherited from DataWeave 1.0.


In this post, I’ll go through some of the main differences between these two operators so you decide which one to use in your DataWeave scripts!


Note: By this point, I’m assuming you already have the basic DataWeave understanding. If this is not the case, I recommend you read these tutorials first.



What are scopes?


If you come from a different programming paradigm, this concept might be easy to understand. To keep it simple, let’s just say that scopes are these imaginary boxes where you can keep variables, functions, and so on. These variables (or functions) are not available to anyone else outside of these boxes, only inside. We will see some examples in a bit.


In DataWeave, you can have global and local variables (and functions, annotations, etc.). The global variables are the ones that appear over the three dashes (---) that separate your script. For example


%dw 2.0
output application/json
var globalVar = "this is my global variable"
---
globalVar

These variables are accessible from anywhere in your script. They don’t have to exist inside a specific scope to be used.


Let’s talk about how to create local variables with the using or do operators so we can put together the meaning of scopes.



Creating scopes and local variables with the do operator


Try out the following example. What age do you think will be outputted from this script?


%dw 2.0
output application/json
var age = 21
---
{
  person: do { 
    var user = "Robin"
    var age = 5
    ---
    {
        name: user, 
        age: age
    }
  }
}

The answer is 5. We used the do operator in line 6 to create a local scope. Notice how we created the variables user and age inside this scope. This new scope will work very similarly to a regular script in the sense that it can have its variables, functions, etc.


Notice how we have to add three dashes (---) in line 9 to separate the variable declaration from the localized script. This is because when you use the do operator, you can use this syntax for your new local scope.


Below is a graphical representation of how these two scopes are separated.



The age in the output is 5 because it will take the “closest” variable. In other words, the more local the variable, the more precedence it takes.


What do you think will be the output if you use the following code? Try to figure it out! The answer will be at the end of the post 🙂


%dw 2.0
output application/json
var age = 21
---
{
  person: do { 
    var user = "Robin"
    var age = 5
    ---
    {
        name: user, 
        age: age + do {
            var age = 10
            ---
            age
        }
    }
  }
}

You can create as many local scopes as you want, but I wouldn’t recommend having more than 1 unless completely necessary. Mainly to avoid spaghetti code. You can read more about do in this post: My top 5 DataWeave tips to make your life easier.



Creating local variables with the using operator


Now, I have to be completely honest here. I haven’t used the using operator. I did work on DataWeave 1.0 for a while, but it was confusing for me to understand how this syntax worked, so I never really used it. Here is an awesome post if you’re still developing with DataWeave 1.0: How to use the “Using” Operator in DataWeave, the MuleSoft Mapping Tool. Otherwise, I’ll explain how to create local variables in DataWeave 2.0 here.


Let’s follow the previous example to demonstrate.


%dw 2.0
output application/json
var age = 21
---
{
  person: using (user = "Robin", age = "5") { 
    name: user, 
    age: age
  }
}

We will have the same functionality as before but see how this syntax is different than do.