mulesoft-labs / data-weave-rfc

RFC for the data weave language
13 stars 5 forks source link

plumb: Specialized function to convert array to object in Dataweave #53

Open VictorManhani opened 1 year ago

VictorManhani commented 1 year ago

Frequently we have a necessity to convert an array to object.

I know the syntax using map, braces and parentheses already exists, but this syntax is very loaded and difficult to understand.

Current Syntax Example:

{(array map {($$): $})}

I created a function to facilitate our transformation on this and called it plumb because it makes the process different from pluck which is converting object to array. Here's the example of the function I created:

%dw 2.0
output application/json

fun plumb(array: Array<Any>, func: Any): Object = (
  {(array map {($$): func($)})}
)

var pay = ["Victor", 1, true, {"color": "red"}, ["açaí"]]
---
pay plumb (obj) -> obj

Expected output:

{
    "0": "Victor",
    "1": 1,
    "2": true,
    "3": {"color": "red"},
    "4": ["açaí"]
}