Skip to content

andersoal/dataweave-playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎮 DataWeave Playground

A local, version-controlled alternative to the MuleSoft DataWeave Playground - develop, test, and share DataWeave transformations offline.

DataWeave Version Maven Version

✨ Features

Feature Online Playground This Project

🔌 Offline Development

📂 Version Control (Git)

🤖 AI (Copilot, Claude, Antigravity, etc)

🔄 CI/CD Integration

💻 Full IDE Support

📦 Reusable Module Libraries

🏢 Enterprise Formats (Excel, FlatFile, Avro)

🚀 Publish to Exchange

🧪 Automated Unit Testing

⚡ Quick Experimentation

🛠️ Custom Module Creation

🎮 Mimics Online Playground Experience

🆓 Free and Open Source

🚀 Getting Started

Prerequisites

  • Java 8+ (JDK installed and JAVA_HOME configured)

  • Tools to manage multiple JDK versions:

  • 📦 Maven 3.6+

  • 🛠️ IDE (VS Code with Anypoint Code Builder recommended)

Installation

git clone https://github.com/andersoal/mule-dataweave-playground
cd mule-dataweave-playground

📁 Project Structure

playground/
|-- pom.xml
|-- README.md
|-- README.adoc
`-- src/
    |-- main/
    |   |-- dw/
    |   |   |-- DateFormatConversionModule.dwl
    |   |   |-- Mule.dwl
    |   |   `-- main.dwl
    |   `-- resources/
    |       `-- properties.yaml
    `-- test/
        |-- dw/
        |   `-- DateFormatConversionModuleTest.dwl
        `-- resources/
            |-- DateFormatConverstionModuleMapping/
            |   `-- Playground/inputs/payload.json
            `-- main/
                `-- Playground/inputs/
                    |-- attributes.dwl
                    |-- correlationId.dwl
                    |-- error.dwl
                    |-- payload.dwl
                    `-- vars.dwl

🎯 Playground Workflow

This repository currently uses src/main/dw/main.dwl as the closest equivalent to the online DataWeave playground.

Online Playground Local Project

Main Editor Panel

src/main/dw/main.dwl

Reusable snippets

src/main/dw/*.dwl modules

Input Panel

src/test/resources/main/Playground/inputs/

Output Panel

DataWeave: Run Preview or Maven test output

🧭 Current Development Flow

  1. Edit src/main/dw/main.dwl for quick experiments.

  2. Use Mule::p(…​) from Mule.dwl when you need mock property access.

  3. Move reusable logic into dedicated modules under src/main/dw/.

  4. Add assertion-based tests under src/test/dw/ when logic needs regression coverage.

🎯 How It Mirrors the Online Playground

Online Playground Local Project

Quick experimentation

src/main/dw/main.dwl + preview inputs

Reusable modules

src/main/dw/*.dwl

Automated validation

src/test/dw/*.dwl unit tests

Auto Preview with DataWeave Extension

Use DataWeave: Enable Auto Preview in VS Code

📦 Available Modules

📅 DateFormatConversionModule

Location: src/main/dw/DateFormatConversionModule.dwl

Utilities for date and time formatting conversions.

link:src/main/dw/DateFormatConversionModule.dwl[role=include]

🔧 Mule

Location: src/main/dw/Mule.dwl

Utilities for Mule runtime property access simulation.

link:src/main/dw/Mule.dwl[role=include]

🎮 main.dwl

Location: src/main/dw/main.dwl

This is the current playground entrypoint for local experimentation.

link:src/main/dw/main.dwl[role=include]

🧪 Testing

🧪 Testing Strategies

This project currently supports two complementary workflows.

1️⃣ Interactive Playground Workflow

Use src/main/dw/main.dwl together with the fixtures in src/test/resources/main/Playground/inputs/.

link:src/test/resources/main/Playground/inputs/payload.dwl[role=include]
link:src/test/resources/main/Playground/inputs/attributes.dwl[role=include]
link:src/test/resources/main/Playground/inputs/vars.dwl[role=include]
link:src/test/resources/main/Playground/inputs/error.dwl[role=include]
link:src/test/resources/main/Playground/inputs/correlationId.dwl[role=include]

2️⃣ Unit Tests (Testing Framework)

link:src/test/dw/DateFormatConversionModuleTest.dwl[role=include]

Fixture data for mapping-style experiments:

link:src/test/resources/DateFormatConverstionModuleMapping/Playground/inputs/payload.json[role=include]

🏃 Running Tests

mvn test
mvn test -Dtest=DateFormatConversionModuleTest
mvn clean test

📊 Test Reports

After running tests, reports are generated in:

target/data-weave-test-reports/

🔍 Preview Like Playground

The Anypoint Code Builder extension for VS Code provides real-time preview capabilities for DataWeave mappings.

Prerequisites

How to Use

  1. Open src/main/dw/main.dwl

  2. Run DataWeave: Run Preview from the Command Palette

  3. Enable DataWeave: Enable Auto Preview for instant refresh on save

🎯 Usage Examples

Example 1: Transform JSON Payload

%dw 2.0
output application/json
---
payload.users map (user) -> {
    fullName: user.firstName ++ " " ++ user.lastName,
    email: lower(user.email)
}

Example 2: Date Formatting with Module

%dw 2.0
import dateTimeConversion from DateFormatConversionModule
output application/json
---
{
    timestamp: dateTimeConversion(now(), "yyyy-MM-dd'T'HH:mm:ss"),
    readable: dateTimeConversion(now(), "MMMM dd, yyyy")
}

Example 3: Working with Mule Runtime Objects

%dw 2.0
output application/json
---
{
    data: payload,
    requestId: correlationId,
    headers: attributes.headers,
    customVar: vars.myVariable,
    hello: Mule::p("example.hello")
}

🏗️ Creating New Modules

Step 1: Create the Module File

%dw 2.9
// src/main/dw/StringUtilsModule.dwl

fun titleCase(text: String): String =
    text splitBy " "
    map (word) -> upper(word[0]) ++ lower(word[1 to -1])
    joinBy " "

fun removeWhitespace(text: String): String =
    text replace /\s+/ with ""

Step 2: Create a Test Mapping

%dw 2.0
// src/test/dw/StringUtilsMapping.dwl
import * from StringUtilsModule
output application/json
---
{
    titleCaseResult: titleCase("hello world"),
    noWhitespace: removeWhitespace("hello world")
}

Step 3: Add Test Input (Optional)

{
  "text": "hello dataweave playground"
}

Step 4: Run Your Test

mvn test -Dtest=StringUtilsMapping

📚 Documentation Standards

Function Documentation Template

/**
 * Brief description of what the function does.
 */
fun myFunction(param: Type): ReturnType = ...

📝 Naming Conventions

Type Convention Example

Modules

PascalCase + Module suffix

StringUtilsModule.dwl

Test Mappings

PascalCase + Mapping suffix

StringUtilsMapping.dwl

Unit Tests

PascalCase + Test suffix

StringUtilsModuleTest.dwl

Functions

camelCase

formatDate(), parseJson()

🌐 Publishing to Exchange

Share your modules with others by publishing to Anypoint Exchange.

Prerequisites

  1. Anypoint Platform account with Exchange contributor permissions

  2. Configure credentials in ~/.m2/settings.xml

<server>
    <id>exchange</id>
    <username>your-username</username>
    <password>your-password</password>
</server>

Enable Exchange Publishing

Replace the placeholder organization id in pom.xml, then uncomment the Exchange configuration.

link:pom.xml[role=include]

Publish

mvn deploy -Dproject.groupId=your-organization-id

🤝 Contributing

  1. 🍴 Fork the repository

  2. 🌿 Create a feature branch (git checkout -b feature/amazing-module)

  3. 💾 Commit your changes (git commit -m 'Add amazing module')

  4. 📤 Push to the branch (git push origin feature/amazing-module)

  5. 🔃 Open a Pull Request

📄 License

This project is for development and learning purposes.

Notes About Includes

This document intentionally references project files via include:: so code samples stay synchronized with source.

If you render this file with Asciidoctor locally, includes are expanded automatically.

Made with ❤️ for the MuleSoft Community

About

MuleSoft DataWeave Library Project to use as Playground

Topics

Resources

Stars

Watchers

Forks

Contributors