简体   繁体   中英

Global functions with dataweave operators?

I've few data weave functions which needs to be reused in different places in the flow. but these are using dataweave operators such as pluck. So is there a way to make them global.

%function formatterm(a)  (a pluck (null when $==null otherwise "term" : {'$$':$})) -null

%function formatterms(a) (a pluck (null when $==null otherwise "terms" : {'$$':$})) -null %function formatquerystring(a) (a pluck (null when $==null otherwise ("query_string" : "query": '$$' ++ ':' ++ $) )) - null

Thanks Sushma

You can create a global MEL function. From MEL you can call dataweave, example:

https://docs.mulesoft.com/mule-user-guide/v/3.8/mel-dataweave-functions

See https://docs.mulesoft.com/mule-user-guide/v/3.8/mel-dataweave-functions for details.

You have to be careful with quoting:

def formatquerystring(a) { 
return dw("("+ a + " pluck (null when $==null otherwise 
           (\"query_string\" : \"query\": '$$' ++ ':' ++ $) )) - null")
} 

works for me. But with this you are dynamically creating a dwl script which has to be compiled. And beware of code injection.

May be its better to write your formatquerystring in MEL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM