2

I have a 3 functions in Dataweave transform message component and I would like to reuse these functions in 4 other transform message components.

Is there a way I can centralise the 3 functions and reference them in the 4 other transform message components without copying and pasting the function into every transform message I want to use it with?

I am using Anypoint Studio 6.1 and Mule 3.8.1.

The 3 functions in Dataweave that I would like to access globally in my project are:

%function acceptable(value) (
    value match {
        :null -> false,
        a is :array -> a != [{}],
        o is :object -> o != {},
        s is :string -> s != "",
        default -> true
    }
)

%function filterKeyValue(key, value) (
    {(key): value} when acceptable(value) otherwise {}
)

%function removeFields(x)
    x match {
        a is :array -> a map removeFields($),
        o is :object -> o mapObject
            (filterKeyValue($$, removeFields($))),
        default -> $
    }

These functions were taken from a Stackoverflow post around removing empty fields and I am getting this error when I try to deploy the application:

INFO  2017-02-17 19:31:37,190 [main] org.mule.config.spring.MuleArtifactContext: Closing org.mule.config.spring.MuleArtifactContext@70b2fa10: startup date [Fri Feb 17 19:31:30 GMT 2017]; root of context hierarchy
ERROR 2017-02-17 19:31:37,478 [main] org.mule.module.launcher.application.DefaultMuleApplication: null
org.mule.mvel2.CompileException: [Error: unknown class or illegal statement: org.mule.mvel2.ParserContext@515940af]
[Near : {... value match { ....}]
                        ^
[Line: 3, Column: 20]
    at org.mule.mvel2.compiler.AbstractParser.procTypedNode(AbstractParser.java:1476) ~[mule-mvel2-2.1.9-MULE-010.jar:?]

Thanks

user3165854
  • 1,505
  • 8
  • 48
  • 100
  • I had hope that this question was how to use dataweave function in another Mule project. Kinda shared library for all projects. But answers went to the road of explaining global functions in one project. This is obvious and not what I expected. Any thoughts about libraries? – Alex Feb 17 '17 at 13:57
  • Here is a blog post I created explaining one option for DataWeave 1.x. Starting in DataWeave 2.x you can not create reusable DataWeave modules. https://blogs.mulesoft.com/dev/training-dev/how-to-reuse-dataweave-code/ – Ethan Port Apr 17 '20 at 13:13

2 Answers2

0

This has already been answered here, see if this helps you. https://forums.mulesoft.com/questions/31467/invoking-java-or-groovy-method-in-dataweave-script.html

0

You can create a global function in configuration section and call it from your Dataweave.

Abani Patra
  • 172
  • 3
  • 12