Skip to content

Instantly share code, notes, and snippets.

@mikeacjones
Created May 19, 2020 12:48
Show Gist options
  • Save mikeacjones/dbe76e4f6f5e402e024ab57b8a2fc2b5 to your computer and use it in GitHub Desktop.
Save mikeacjones/dbe76e4f6f5e402e024ab57b8a2fc2b5 to your computer and use it in GitHub Desktop.
Function for easily appending namespaces to all key:value pairs of an object. Full traversal, so objects in arrays too.
%dw 2.0
fun appendNamespace(data, nsSelector: (k: Key) -> Namespace | Null) =
data match {
case is Array -> data map appendNamespace($, nsSelector)
case is Object -> data mapObject do {
var ns0 = nsSelector($$)
---
if (ns0 != null) ns0#"$($$)": appendNamespace($, nsSelector)
else ($$): appendNamespace($, nsSelector)
}
else -> data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment