Is it posible to use Dataweave mask or update functions with conditions that depend on dynamic data?

Viewed 120

I need to mask some fields which are dynamic. This is what I'm using now:

var data = { a: 1, b: 2, c: 3, d: 4, e: 5 }
var mask = { a: " ", d: "0"}
---
data mapObject ((value, key, index) -> 
    (key): mask[key] default value
)

And I'm getting the expected output

{"a": " ","b": 2,"c": 3,"d": "0","e": 5}

Is it possible to use mask or update functions for this? Also, if you know you know which is the most performant solution, I would be really like to know hence I need to process 70MM records with it.

1 Answers

The update operator is more helpful when you want to conditionally select nested keys and then only update those matching key cases based on some conditions. If the object is a simple flat schema, mapObject like in your solution is O(N). Another option if order doesn't matter and the keys are unique to use -- and ++.

var data = { a: 1, b: 2, c: 3, d: 4, e: 5, a:5, d:"something" } 
var mask = { a: " ", d: "0"} 
--- 
data -- keysOf(mask) ++ mask

This produces the output:

{
  "b": 2,
  "c": 3,
  "e": 5,
  "a": " ",
  "d": "0"
}
Related