1
votes

I have input payload an array containing keywords eg.

{
"array" : ["sunday", "monday", "tuesday", "wednesday"]
}

and enum in a groovy script as

FUNDAY('sunday'),
BOREDAY('monday'),
OKAYDAY('tuesday'),
NUMBDAY('wednesday'),
YOUCANDOITDAY('thursday'),
PUSHHARDDAY('friday'),
PARTYDAY('saturday')

I want to update the payload value with the id of enum, as

{
"array" : ["FUNDAY", "BOREDAY", "OKAYDAY", "NUMBDAY"]
}

Is there any effective way to get this done? I understand it is easy if consider this data, but there are situations where the data is huge and writing case for each keyword becomes hectic. Looking for generalized solution. Thanks

2
Note, your question is not about filtering but about mapping.aled

2 Answers

3
votes

You just need a DataWeave object to store the mapping from one value to the other. An object can be treated as a hash or dictionary in other languages. You can setup this object from a database or a file, so the method is generic.

Example:

%dw 2.0
output application/json
var mapping={
    sunday: "FUNDAY", 
    monday: "BOREDAY",
    tuesday: "OKAYDAY",
    wednesday: "NUMBDAY"
}
---
payload.array map mapping[$]
2
votes

To expand on the discussion in the comments (should really be a new question...) I'll lay out the fastest way to create a dataweave module. This isn't necessarily the best (there isn't much in the way of documentation around doing this), but it will work just fine.

  1. Create a new project in anypoint studio. Pick something relevant to the library name. For example, if this is going to be a library around XML functionality, you would probably name it something like xml-dw-library.

  2. We'll need to change our pom.xml with a specific build profile, dependencies, and repos to support a dataweave module. I've create a gist you can use a template here: https://gist.github.com/mikeacjones/52ab8e4e01eec4c49116ecaca5f2a9e3 You want to take the time now to update your groupId as this dictates how you will use the module later. In my case, I'm going to use io.syntaxsugar as I use this as my namespace for personal projects. Set an appropriate artifactId as well.

pom screenshot

  1. In src/main/mule go ahead and delete the default flow created there. It is still useful to create MUnit flows for testing, but we won't actually have a flow here.

  2. In src/main/, create a folder called dw. Now, from here, the folder name and file names dictate your imports. In my case, I want my import to look like this: import * from io::syntaxsugar::XML, so I'll create a file call XML.dwl with this path: src/main/dw/io/syntaxsugar/XML.dwl.

  3. Next, I'm going to set my content (this is just an example so I'm using something I created earlier) with all of my functions. In this case, I'm going to use this one:

%dw 2.0
fun appendNamespace(data, nsSelector: (k: Key) -> Namespace | Null) =
  data match {
    case is Array -> data map appendNamespace($, nsSelector)
    case is Object -> data mapObject do {
      var ns0 = nsSelector($$)
      ---
      if (ns0 != null) ns0#"$($$)": appendNamespace($, nsSelector)
      else ($$): appendNamespace($, nsSelector)
    }
    else -> data
}

Your project should look something like this at this point:

studio screenshot

  1. Now that we've created our module, we can install it into our local maven repo (or if you have a central repo, you can setup your distribution management).

enter image description here

  1. And finally, to use it, we can add this module as a dependency to projects now:
<dependency>
    <groupId>io.syntaxsugar</groupId>
    <artifactId>xml-dw-library</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>dw-library</classifier>
</dependency>

And then use it like so in a dataweave script:

enter image description here

This lets you keep your dataweave modules separate and reusable (and tracked under separate source control), and you can also apply CI/CD to the dataweave modules themselves and unit test separately from your mule applications.

You'll hopefully have noticed in the POM.xml we defined test resource folders; these actually allow you to create dataweave unit tests using dataweave. For each test, create a folder for the test (example: appendNsTest), and then in the folder you can create a folder called inputs that you place a file for each input (example: payload.json or vars.json), out.json/xml/csv/etc, and transform.dwl. The transform.dwl is executed and the results compared against out.*.

This is a lot, so I've taken the working example I used for screenshots and published it to this public repo: https://github.com/mikeacjones/xml-dw-library