<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<flow name="testObjectModule" doc:id="1cd56ec4-a96a-47d6-a127-bba078006d15" >
<ee:transform doc:name="Transform Message" doc:id="31f0a6d3-e241-462b-80f1-1a39a52eab45" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/dw
ns localnames http://dwtraining.mulesoft.com
var object1 = {
users: {
user1 @(name: "Annie", lastname: "Point"): "AP",
user2 @(name: "Connie", lastname: "Hector"): "CH"
}
}
---
{
internals: object1.users,
justvalues: valuesOf(object1.users),
justnames: namesOf(object1.users),
justKeys: keysOf(object1.users),
//justNS: keysOf(object1.users) map (user) -> (user.#),
keytypes: keysOf(object1.users) map (u) -> "index$$": typeOf(u)
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="testArraysModule" doc:id="3e4e1d11-de4e-4c24-a7a0-f34dbf5f26e3" >
<ee:transform doc:name="Transform Message" doc:id="d086179e-b3d0-4584-9233-698fbeabc5d1" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/dw
import dw::core::Arrays
var flights = readUrl("classpath://examples/extra/americanFlightsResponse.json")
---
{
// alldata: flights,
// bigflights: Arrays::some(flights..totalSeats, (ts) -> (ts > 200)),
// allbig: Arrays::every(flights..totalSeats, (ts) -> (ts < 400)),
// countem: sizeOf(flights),
// allseats: flights..totalSeats,
// smallcraftseats: flights..totalSeats Arrays::sumBy (a) -> (if (a<200) a else 0),
seatmates: flights..totalSeats Arrays::divideBy 2,
triplets: 1 to 17 Arrays::divideBy 3
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="constructAnObject" doc:id="2ee0df40-dc46-4b05-9c0f-1216419ed40e">
<ee:transform doc:name="Transform Message" doc:id="afe470dd-4bec-46f2-a53b-aa0e4c50ee57">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/dw
var a1 = [1,"a",true]
var o1 = {one:1,two:2,three:3}
---
{
object1: {(o1),four:4}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="addAndConcat" doc:id="49491bb6-5a07-43a1-b577-5efebc961f5d" >
<ee:transform doc:name="Transform Message" doc:id="fc25fac2-5e94-42c6-9b23-6802f012415a" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/dw
var a1 = [1,"a",true]
var o1 = {one:1,two:2}
---
{
// arrayadd: a1 + a1,
// arrayconcat: a1 ++ a1,
// arrayplusobject: a1 + o1,
// objectplusarray: o1 + a1, // error
// arrayconcatobject: a1 ++ o1, // error
objectconcatobject: o1 ++ o1,
// concatminus: o1 ++ o1 - "one", // operator before function
// concatstripped: (o1 ++ o1) - "one", // force order of precedence
concatandremove: (o1 ++ o1) -- two:2
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="passFunctions" doc:id="f4e6f1e8-a529-49a0-8de8-77b9e80b8450" >
<ee:transform doc:name="Transform Message" doc:id="6cca493b-ce66-40ea-90c5-84abb5017e5f" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
fun formatString( anInputString : String, formatter : (String) -> String ) =
formatter( anInputString )
---
"helloWorld" formatString
(inStr) -> lower( inStr replace "World" with " There" )
]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="chainableTest" doc:id="79891967-98f7-422f-afa2-551ace08d5b2" >
<ee:transform doc:name="Transform Message" doc:id="c16daf5b-fe55-4bb0-8efe-33de633d9dcb" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
fun chain(anInput, expression) =
expression(anInput)
---
//[1,5,3,2]
//filter ($ mod 2) == 1
////orderBy $
//orderBy -$
//{one:"One", two:"Two"}
// chain (x) -> (x.one ++ ' ' ++ x.two)
//upper( {one:"One", two:"Two"} chain (x) -> (x.one ++ ' ' ++ x.two) )
//
//chain (x) -> (x.one ++ ' ' ++ x.two)
//chain upper
chain(
chain( {one:"One", two:"Two"}, (x) -> (x.one ++ ' ' ++ x.two) ),
//upper
lower
)
]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="customMapping" doc:id="88c7422e-d668-42b0-bebe-d9a10a57b7b9" >
<ee:transform doc:name="Transform Message" doc:id="1b83ec5d-744c-45c2-9039-3abbafb7d3d3" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
import dw::mappings::MyMapping
---
//MyMapping::main( {someElement: ["order", "1001"]} )
//MyMapping::main( {someElement: ["order", "1001"], formatter: (k)->k } )
MyMapping::main( {someElement: ["order", "1001"], formatter: lower } )
//MyMapping::main( { incorrect: ["order", "1001"], formatter: lower } )
]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="matchOperatorTest" doc:id="bd9e2b8e-5637-40a9-869d-91d6eb33acba">
<ee:transform doc:name="Transform Message" doc:id="7b9b9a41-7682-42c7-b5c6-a2a291a395ec">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
var americanFlights =
[
{
"ID": 5,
"code": "rree1093",
"price": 142,
"departureDate": "2018-02-11T00:00:00",
"origin": "MUA",
"destination": "SFO",
"emptySeats": 1,
"plane": {
"type": "Boeing 737",
"totalSeats": 150
}
},
{
"ID": 7,
"code": "eefd1994",
"price": 676,
"departureDate": "2018-01-01T00:00:00",
"origin": "MUA",
"destination": "SFO",
"emptySeats": 0,
"plane": {
"type": "Boeing 777",
"totalSeats": 300
}
},
{
"ID": 8,
"code": "ffee2000",
"price": 300,
"departureDate": "2018-02-20T00:00:00",
"origin": "MUA",
"destination": "SFO",
"emptySeats": 30,
"plane": {
"type": "Boeing 737",
"totalSeats": 150
}
},
{
"ID": 9,
"code": "eefd3000",
"price": 900,
"departureDate": "2018-02-01T00:00:00",
"origin": "MUA",
"destination": "SFO",
"emptySeats": 0,
"plane": {
"type": "Boeing 737",
"totalSeats": 150
}
}
]
fun overloaded ( anInput : Array) =
"input is an Array of size $(sizeOf(anInput))"
fun overloaded ( anInput : Object) =
"input is an Object of size $(sizeOf(anInput))"
fun overloaded ( anInput : Null) =
"input is Null"
//fun processByType( anInput ) =
//anInput match {
// else -> anInput
//}
fun processByType( anInput ) =
anInput match {
//case a if( a is Array ) -> "input is an Array of size $(sizeOf(a))"
case is Array -> "input is an Array of size $(sizeOf(anInput))"
//case "SFO" -> "San Francisco"
case is Object -> (inputType: "Object") ++ anInput
// case is Number -> ($ * dw::modules::FlightsLib::exchangeRate)
// as String {format: "#,###.00"} as Number
case is Number -> ($ * dw::modules::FlightsLib::exchangeRate)
as String {format: "#,###.00"} as Number
// else -> anInput
//else -> typeOf( anInput )
else -> "input " ++ anInput ++ " is of type " ++ typeOf( anInput )
}
---
//americanFlights
//overloaded( [1,2])
//overloaded( {one: "One", two: "Two", three: "Three"} )
//overloaded( null )
//processByType( americanFlights )
//processByType( americanFlights[0] )
//processByType(americanFlights[0].ID)
processByType("SFO")
]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="tryAndFailTest" doc:id="98ca015f-be0f-4d36-b94f-5604c238da21" >
<http:listener doc:name="Listener" doc:id="3e7bc53c-45d1-46f6-a6c4-cee11a9cf70c" config-ref="HTTP_Listener_config" path="load"/>
<ee:transform doc:name="Transform Message" doc:id="0b539deb-fb6f-4ce9-9c4e-3b7d12ce52f8" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
import * from dw::Runtime
//
//fun readInput( anInputFile ) =
//readUrl("classpath://" ++ anInputFile)
//fun readInput(anInputFile) =
//try (
// () -> readUrl("classpath://" ++ anInputFile )
//)
//orElseTry(
// () -> readUrl("classpath://examples/" ++ anInputFile )
//)
//orElse(
// fail("Can't find the file: " ++ anInputFile ++
// " nor /examples/" ++ anInputFile)
//)
fun readInput(anInputFile) =
try (
log( "Could not read in the file, will try in the examples sub-folder",
() -> readUrl("classpath://" ++ anInputFile )
)
)
orElseTry(
() -> readUrl("classpath://examples/" ++ anInputFile )
)
orElse(
fail("Can't find the file: " ++ anInputFile ++
" nor /examples/" ++ anInputFile)
)
---
//dw::Runtime::try (
// //readInput("flightsToLAX.json")
// () -> readInput("flightsToLAX.json")
//)
//readInput("flightsToLAX.json")
//readInput("noGood.json")
readInput(attributes.queryParams.filename default "flightsToLAX.json")]]></ee:set-payload>
</ee:message>
</ee:transform>
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="556f9947-0a11-4293-921d-b233fa6bb2c2" >
<set-payload value="#[output application/json --- myError: error]" doc:name="Set Payload" doc:id="c5a8b8e0-ece8-444e-a963-dad2f104d271" />
</on-error-continue>
</error-handler>
</flow>
</mule>
Comments