Skip to content

Instantly share code, notes, and snippets.

View mikeacjones's full-sized avatar
🚀

Michael Jones mikeacjones

🚀
View GitHub Profile
@mikeacjones
mikeacjones / dw-module-pom.xml
Last active March 1, 2021 13:31
Example pom file for dataweave module
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- UPDATE THE INFORMATION HERE -->
<groupId></groupId>
<artifactId></artifactId>
<name></name>
@mikeacjones
mikeacjones / expand-xml-attrs.dwl
Last active January 6, 2021 16:47
Function to convert XML to JSON, while expanding attribute if there are any
%dw 2.0
output application/json
var expandXmlAttrs = (payload) ->
payload match {
case is Array -> payload map expandXmlAttrs($)
case is Object -> payload mapObject {
(($$): expandXmlAttrs($)) if ($$.@ == null),
(($$): {
value: expandXmlAttrs($),
@mikeacjones
mikeacjones / hungarian.dwl
Last active January 6, 2021 16:41
Least cost matching algorithm using hungarian method. Probably poorly implemented but it works
%dw 2.0
input payload csv
import * from dw::util::Values
import * from dw::Runtime
//#region Constants
var INF = pow(2, 28)
var countryMap = {
"USA": "USA",
@mikeacjones
mikeacjones / shuffle.dwl
Last active January 6, 2021 16:40
Function to shuffle an array
%dw 2.0
fun splice(arr: Array, index: Number) =
if (index == 0) arr[1 to -1]
else (arr[0 to index-1] default []) ++ (arr[index+1 to -1] default [])
fun shuffle(arr: Array) =
(((0 to sizeOf(arr)-1) as Array) reduce (i, s = {res:[], items: arr}) -> do {
var ri = floor(random() * sizeOf(s.items))
---
@mikeacjones
mikeacjones / max-matching.dwl
Last active December 10, 2020 02:11
hopcroft-karp implemented using dataweave
%dw 2.0
import * from dw::util::Values
var INF = pow(2, 28) //infinity constant
var ARR = (size, value) -> ((0 to size-1) as Array) map value //make it a bit easier to initialize an array
//function for while looping
var while = (conditionFn: (s: Any) -> Boolean, whileFn: (s: Any) -> Any, state = {}) ->
if (conditionFn(state)) while(conditionFn, whileFn, whileFn(state))
else state
@mikeacjones
mikeacjones / common.dwl
Last active January 6, 2021 16:40
Just some odds and ends functions that I may or may not use. Some of these are just personal experiments.
%dw 2.0
fun getEdges(left: Array, right: Array, isEdgeFn: (l: Object, r: Object) -> Boolean) =
left reduce ((leftItem, mAccum = { li: 0, edges: [] }) ->
(right reduce ((rightItem, iAccum = { li: mAccum.li, ri: 0, edges: mAccum.edges }) ->
{
li: iAccum.li,
ri: iAccum.ri + 1,
edges: iAccum.edges ++ [([iAccum.li, iAccum.ri]) if (isEdgeFn(leftItem, rightItem))]
}
@mikeacjones
mikeacjones / hlLoop_usage.dwl
Last active January 6, 2021 16:39
Example usage of hlLoop.dwl - building EDI data format
%dw 2.0
import * from dw::edi::hlLoop
output application/json
---
{
"Delimiters": "|>U\n",
TransactionSets: {
v004010: {
@mikeacjones
mikeacjones / hlLoop.dwl
Last active January 6, 2021 16:39
Example hlLoop for EDI - elements use indexing and back-reference relative indexes when building structure. See hlLoop_usage.dwl for example
%dw 2.0
fun hlLoop(payload: Object) =
((payload.DeliveryItems distinctBy $.PurchaseOrder).PurchaseOrder reduce ((order, accum=hlLoopRoot(payload)) ->
payload.DeliveryItems distinctBy $.ProductId reduce ((item, accum=hlLoopPurchaseOrder(order, accum)) ->
(payload.DeliveryItems filter ($.ProductId == item.ProductId and $.ProductBatchNumber != null)) reduce ((package, accum=hlLoopItem(item, accum)) ->
hlLoopPackage(package, accum)
)
)
)).items
@mikeacjones
mikeacjones / settings.xml
Last active January 6, 2021 16:37
Example settings.xml file for maven CI with Mulesoft
<servers>
<server>
<id>anypoint-exchange-v2</id>
<username>USERNAME</username>
<password>PASSWORD</password>
</server>
<server>
<id>MuleRepository</id>
<username>USERNAME</username>
<password>PASSWORD</password>
@mikeacjones
mikeacjones / Namespaces.dwl
Created May 19, 2020 12:48
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)