DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • The Two-Pointers Technique
  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Troubleshooting Memory Leaks With Heap Profilers

Trending

  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Introduction to Retrieval Augmented Generation (RAG)
  1. DZone
  2. Data Engineering
  3. Data
  4. Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically

Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically

In this article, we are trying to resolve a wildcard dynamically using a user-defined DataWeave function.

By 
Bibek Gorain user avatar
Bibek Gorain
·
Mar. 03, 22 · Code Snippet
Likes (4)
Comment
Save
Tweet
Share
6.2K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave is a powerful programming language designed and supported by MuleSoft in Mule applications. In Mule runtime version 4.x, DataWeave version 2.0 is supported. The internal DWL version could be different; however, in the script, %dwl 2.0 needs to be defined. It is mainly used for transforming and accessing data in the Mule flow relating to several components such as connectors and transformers.

In this article, we are trying to resolve a wildcard dynamically using a user-defined DataWeave function. In DWL 2.0, I could not find any core function or operator present to support the feature; hence, I decided to write one. An article for the same use case for Mule 3 with Dataweave 1.0 is already published on DZone.

Goal To Achieve Below

TypeScript
 
Input: "This is to test wildcards.1st wildcard {1}, 2nd wildcard {2}, Nth wildcard {n}"
Output: "This is to test wildcards.1st wildcard 1, 2nd wildcard 2, Nth wildcard Wn"
  Assuming passing set of wildcards as [1,2,"Wn"]

DataWeave(2.0) Code

TypeScript
 
%dw 2.0
output application/json

var inputString = "This is to test wildcards.1st wildcard {1}, 2nd wildcard {2}, Nth wildcard {n}"
fun resolveParam(unresolvedString,parameters=[]) =
	do {
    var propArr=unresolvedString splitBy /\{\w*\}/ 
	---
		if (sizeOf(propArr)  > 1 ) (flatten (propArr zip parameters) reduce ($$ ++ $)) else (unresolvedString)
    }
---
resolveParam(inputString,[1,2,"Wn"])

Explanation

Here in DWL 2.0, a function named resolveParam can accept two parameters: unresolvedString (mandatory), and parameters (optional: with default empty array value when no wildcard replacement is required). In this function, logic unresolvedString split into an array of strings based on a regex \{\w*\} that matches {<anystring_without_special_character>}.The results were that the array was then zipped with input parameters using those wildcards that needed to be replaced in the same order.  Lastly, using reduce and "++" operator, the final wildcard was resolved and the string prepared.

I hope this will help to achieve your goal. The Mule 4 DWL 2.0 function was created separately to create less confusion as Mule 3 DWL 1.0 is still being used in a large group of organizations.

Use case Data structure Strings Operator (extension) TypeScript application Data (computing) DZone Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • The Two-Pointers Technique
  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Troubleshooting Memory Leaks With Heap Profilers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!