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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Exploring Exciting New Features in Java 17 With Examples
  • Java String: A Complete Guide With Examples
  • Generics in Java and Their Implementation

Trending

  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • Member Spotlight: Shai Almong

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically

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

Read here to learn a DataWeave(DWL 1.0) function to help you perform a requirement where you want to resolve a wildcard using a set of parameters.

By 
Bibek Gorain
·
Updated Mar. 01, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Mulesoft DataWeave is a simple powerful tool to transform data inside a flow. Numerous core operators and functions are already present to perform various operations such as capitalize, camelize, upper, and lower.

For string operations, there are no core functions present to resolve a wildcard. I hope the DataWeave(DWL 1.0) function below helps you to perform a requirement where you want to resolve a wildcard using a set of parameters.

Steps

Goal To Achieve Below:

Java
 
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 Code:

TypeScript-JSX
 
%dw 1.0
%output application/json

%var inputString = "This is to test wildcards.1st wildcard {1}, 2nd wildcard {2}, Nth wildcard {n}"
%function reolveParam(unresolvedString,parameters=[]) 
	using(propArr=unresolvedString splitBy /\{\w*\}/)
		((flatten (propArr zip parameters) reduce ($$ ++ $)) when ((sizeOf propArr) > 1 ) otherwise unresolvedString)
---

reolveParam(inputString,[1,2,"Wn"])

Explanation: 

Here in the above resolveParam function, two inputs are needed.  The first one is unresolvedString which is mandatory with a wildcard. In this case "{}" is used as a wildcard pattern, and those are resolved with provided set of parameters (2nd input) as an Array. unresolvedString is split into an array(propArr) of string tokens using the regex for the wildcard. Then, propArr is zipped with parameters array using zip operator. Ultimately flatten and reduce operators help to achieve the resolved string.

Transform Message

The second parameter named parameters is optional. In the case of an input string without a wildcard, there is no need to pass the 2nd parameter. The function could be invoked in this way resolveParam("This is an input without wildcard").

Your requirement might be a bit different, however, and changing a bit in the above code might help you achieve your program's goal. As a learning exercise, you could extend this function with another param with regex. I am leaving it up to you for your learning purposes.

A similar function in Mule 4(DWL 2.0) will also be added soon.

Strings Data Types Data structure Operator (extension) Requirement Java (programming language) Data (computing) Pass (software) Exercise (mathematics)

Opinions expressed by DZone contributors are their own.

Related

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Exploring Exciting New Features in Java 17 With Examples
  • Java String: A Complete Guide With Examples
  • Generics in Java and Their Implementation

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook