expr_story Pub

Simple expressions

Setting a variable

ctx.set is used to set one or more variables in the current context:

send::  msg ctx.set  (res<-"123")

expect::  (res is "123")

ctx.setVal is useful if the name is itself sourced, a variable:

send::  msg ctx.setVal  (name<-"res", value:Number<-321)

expect::  (res is 321)
expect::  (res is number)

ctx.echo shows all the details about the input arguments - use it to see types, values etc:

send::  msg ctx.echo  (res12<-"123")

expect::  (res12 is "123")

Contains:

send::  msg ctx.set  (res23<-"123")

expect::  (res23 contains "2")
expect::  (res23 containsNot "4")

encoders

send::  msg ctx.base64encode  (res29<-"res29")

expect::  (payload is "cmVzMjk=")

send::  msg ctx.base64decode  (res29<-payload)

expect::  ( ((payload as string) is "res29"))

send::  msg ctx.sha1  (res35<-"res35")

expect::  (payload is "7D509433920201DF3A7EE4363970D83359D826B8")
expect::  (res35_sha1 is "7D509433920201DF3A7EE4363970D83359D826B8")

send::  msg ctx.sha256  (res35<-"res35")

expect::  (payload is "2e994a1eb4aea52f7c059c6b82f05f14777575371f3acf833ef72fb7e294ac71")
expect::  (res35_sha256 is "2e994a1eb4aea52f7c059c6b82f05f14777575371f3acf833ef72fb7e294ac71")

Resetting a value

This will remove a value from the context:

send::  msg ctx.setVal  (name<-"res")

expect::  (res not defined)

Reset the entire context, with ctx.clear:

send::  msg ctx.setVal  (name<-"res", value:Number<-321)

expect::  (res is 321)

send::  msg ctx.clear 

expect::  (res not defined)

Constants

Examples of constants of different types:

send::  msg ctx.set  (res:Number<-45)

expect::  (res is 45)
expect::  (res is number)

send::  msg ctx.set  (res<-"astra")

expect::  (res is "astra")
expect::  (res is string)

String interpolation and escaping of such

send::  msg ctx.set  (interpol<-"x")

send::  msg ctx.set  (int1<-"${interpol}")

expect::  (int1 contains "x")
send::  msg ctx.set  (int1<-"a${interpol}b")

expect::  (int1 contains "x")
send::  msg ctx.set  (int1<-"$${interpol}")

expect::  (int1 containsNot "x")
send::  msg ctx.set  (int1<-"a$${interpol}b")

expect::  (int1 containsNot "x")

Simple expressions

The usual simple expressions are available and they are type aware:

send::  msg ctx.set  (ai:Number<-1, bi:Number<-2)

send::  msg ctx.set  (as<-"1", bs<-"2")

send::  msg ctx.set  (res53<-(ai + bi))

expect::  (res53 is 3)
send::  msg ctx.set  (res56<-(as + bs))

expect::  (res56 is "12")

Precendence and associativity

send::  msg ctx.set  (res52:Number<-(((1 - 2) + (3 * 4)) + 9))

expect::  (res52 is 20)

String functions

send::  msg ctx.set  (res57<-"1 2 3 ")

expect::  (res57:Array)
expect::  (res57[="length"] is 6)
expect::  ( ((res57[="length"] + 5) is 11))
expect::  ( ((sizeOf(res57.lines) + 4) is 7))
send::  msg ctx.set  (l66<-res57[="lines"])

Template expansion

By default all strings are templatized in expressions, using ${...} to match a simpler expression:

send::  msg ctx.setVal  (name<-"template1", value<-"is${1+2}")

expect::  (template1 is "is3")

Use this to configure environment settings like URLs for instance

send::  msg ctx.echo  (HOST<-"https://www.google.ca")

send::  msg ctx.echo  (url<-"${HOST}/search?q=dieselapps")

Javascript and regex

These are not used: send::  msg ctx.set  (a:Number<-1, b:Number<-2)

The input parameters overwrite context above send::  msg expr.js.sum1  (a:Number<-1, b:Number<-2)

expect::  (res39 is 3)

send::  msg expr.js.sum2  (a:Number<-1, b:Number<-2)

expect::  (res42 is 3.0)

send::  msg expr.js.name1  (email<-"jane@doe.com")

expect::  (res40 is "jane")

/$send ctx.setVal (name="cart", value={})

send::  msg expr.js.validateEmail  (email<-"jane@doe.com")

expect::  (ok25 is true)

send::  msg ctx.set  (ok25:Boolean<-false)

send::  msg expr.js.validateEmail  (email<-"janedoe.com")

expect::  (ok25 is false)

Sourcing and checking values

send::  msg expr.source.erase 

expect::  (erased not defined)

send::  msg ctx.set  (val192<-"")

expect::  (val192 is defined)
expect::  (val192 not undefined)
expect::  (val192 is empty)
expect::  (val192 not nzlen)

send::  msg ctx.set  (val198:Undefined<-null)

expect::  (val198 not defined)
expect::  (val198 is undefined)
expect::  (val198 is empty)
expect::  (val198 not nzlen)

send::  msg ctx.set  (val204<-"abcd")

expect::  (val204 is defined)
expect::  (val204 not undefined)
expect::  (val204 not empty)
expect::  (val204 is nzlen)

The diesel object:

send::  msg ctx.set  (res5<-js{{ diesel.engineId() }})

expect::  (res5 not empty)

send::  msg ctx.set  (res139<-js{{ diesel.env() }})

expect::  (res139 not empty)

The wix object:

send::  msg ctx.set  (res144<-js{{ wix.diesel.env }})

expect::  (res144 not empty)

Boolean expressions

send::  msg ctx.set  (b1:Boolean<-true)

expect::  (b1 is true)
expect::  (b1 not false)
expect::  (b1)
expect::  (b1)$ifc::true
expect::  (b1)$ifc::b1
expect::  (b1)$ifc::(b1 is true)
expect::  (b1)$ifc::(b1 not false)

send::  msg expr.bool.test1 

expect::  (b2)
expect::  (b3 is true)
expect::  (b4)
expect::  (b5 not defined)

send::  msg expr.bool.test1nos 

expect::  (b2n)
expect::  (b3n is true)
expect::  (b4n)
expect::  (b5n not defined)

send::  msg expr.bool.test2  (a:Number<-1, b:Number<-2)

expect::  (b23)
expect::  (b26)

send::  msg expr.bool.test2  (a:Number<-2, b:Number<-1)

expect::  (b22)
expect::  (b25)

send::  msg expr.bool.test2  (a:Number<-2, b:Number<-2)

expect::  (b24)
expect::  (b25)
expect::  (b26)

send::  msg expr.bool.testAllExpr  (a:Number<-1, bb:Number<-2)

expect::  (b > 23)

send::  msg expr.bool.testContains  (a<-"123")

expect::  (b94 is true)
expect::  (b95 is true)

send::  msg expr.bool.testAndUndef 

expect::  (b47 not defined)
expect::  (b48 is true)
expect::  (b49 not defined)
expect::  (b50 not defined)
expect::  (b51 is true)

send::  msg expr.bool.testBra1 

expect::  (bb105 is true)

send::  msg expr.int.lt 

Arrays, Lists and range accessors

Range accessor on Strings

send::  msg ctx.set  (l3<-"012345")

send::  msg ctx.set  (l4<-l3[:Range=0..None])

expect::  (l4 is "012345")

send::  msg ctx.set  (l8<-l3[:Range=0..Some(3)])

expect::  (l8 is "0123")

send::  msg ctx.set  (l11<-l3[:Range=0..Some(-1)])

expect::  (l11 is "01234")

send::  msg ctx.set  (l14<-l3[:Range=-2..Some(-1)])

expect::  (l14 is "34")

Range accessor on Lists

send::  msg ctx.set  (l21:Array<-[0,1,2,3,4,5])

send::  msg ctx.set  (l4<-l21[:Range=0..None])

expect::  (l4 is [0,1,2,3,4,5])

send::  msg ctx.set  (l8<-l21[:Range=0..Some(3)])

expect::  (l8 is [0,1,2,3])

send::  msg ctx.echo  (l11<-l21[:Range=0..Some(-1)])

expect::  (l11 is [0,1,2,3,4])

send::  msg ctx.set  (l239:Array<-[])

expect::  (l239 is [])

Accessor in pmatch /expect

send::  msg ctx.set  (l14<-l21[:Range=-2..Some(-1)])

expect::  (l14[=1] is 4)

send::  msg ctx.set  (state281:JSON<-{source:[0,1,2,3,4,5],dest:[],aux:[]})

expect::  (state281[="source"][=0] == 0)

send::  msg ctx.set  (state281:JSON<-{source:[0,1,2,3,4,5],dest:[],aux:[]})

expect::  (state281[="source"][=0] is 0)

Setters and accessors

send::  msg test.ctxAccessor 

expect::  (asdf is 90)
expect::  (decb is 87)

json in context and array sum

send::  msg ctx.set  (state294:JSON<-{source:[0,1,2,3,4,5],dest:[],aux:[]})

send::  msg test.jsonAccessorSet  (source<-"source", dest<-"dest", aux<-"aux")

expect::  (state294[="source"] is [1,2,3,4,5])
expect::  (state294[="dest"] is [0])

Expansion

val a217<-"a217v" send::  msg ctx.set  (a218<-"${a217}")

expect::  (a218 is "a217v")

Capture groups

send::  msg expr.captureGroups  (path<-"/getAccount1/67")

expect::  (acctId is "67")
expect::  (payload[="accountId"] is "67")

send::  msg ctx.set  (payload<-"/getAccount1/99")

send::  msg ctx.regex  (regex<-"/getAccount1/(?<acctId3>\d+)")

expect::  (acctId3 is "99")

Don't overwrite payload: send::  msg ctx.set  (payload<-"pa")

send::  msg ctx.regex  (regex<-"/getAccount1/(?<acctId4>\d+)", payload<-"/getAccount1/999")

expect::  (acctId4 is "999")
expect::  (payload is "pa")

Javascript expressions

Simple javascript - script is independent (no arguments):

send::  msg ctx.set  (a225<-js{{ 1+4 }})

expect::  (a225 is 5)

Escaping quotes with javascript regex (notice a230 available inside the js expression):

val a230<-"{"a":32}" send::  msg ctx.set  (a231<-js{{ a230.replace(/"/g, "\"") }})

expect::  (a231 is "{&quot;a&quot;:32}")

More JS examples:

send::  msg ctx.set  (now<-js{{ java.lang.System.currentTimeMillis() }})

send::  msg ctx.set  (later<-js{{ var d = new Date(); d.setSeconds(d.getSeconds() + 10); return d.toISOString(); }})

send::  msg ctx.set  (later<-js{{ var d = new Date(); d.setSeconds(d.getSeconds() + 10); d.toISOString(); }})

send::  msg ctx.set  (noweh<-js{{ var d = new Date(); d.toISOString(); }}, later<-js{{ var d = new Date(); d.setSeconds(d.getSeconds() + 10); d.toISOString(); }})

expect::  (now is number)

Precedence

As

send::  msg ctx.set  (a<-(("1234" + "5") as Number))

expect::  (a is 12345)

val p15<-"p15" val p16<-(("p16-" + p15) as "application/pdf") send::  msg ctx.echo  (a<-p16)

expect::  ( (typeOf(p16) is "application/pdf"))
expect::  (p16 is "p16-p15")

Filter

send::  msg ctx.set  (a:Array<-([1,2,3] filter ( x->(x > 1)() )))

expect::  (a is [2,3])

Map

send::  msg ctx.set  (a278:Array<-([1,2,3] map ( x->(x + 1)() )))

expect::  (a278 is [2,3,4])

Both filter and map

TODO these work with ExprParser1

send::  msg ctx.set  (a281:Array<-(([1,2,3] filter ( x->(x > 1)() )) map ( x->(x + 1)() )))

expect::  (a281 is [3,4])

send::  msg ctx.set  (a284:Array<-((([1,2] + [3]) filter ( x->(x > 1)() )) map ( x->(x + 1)() )))

expect::  (a284 is [3,4])

send::  msg ctx.set  (a287:Array<-(([1,2,3] filter ( x->((((x - (1 * 1)) + 1) > 1) or (2 > 4))() )) map ( x->(x + 1)() )))

expect::  (a287 is [3,4])

TODO this is not working

// $send ctx.set( a290 = [1,2,3] filter (x=> js:{(x - 1*1 + 1) > 1}) map (x=> x + 1) ) // $expect (a290 is [3,4])

Convert to string and use a JS expression for filter lambda send::  msg ctx.set  (a298:Array<-(([1,2,3] filter ( x->(x > 1)() )) map ( x->(x + "")() )))

send::  msg ctx.echo  (res299<-(res57[="lines"] filter e->js{{ !a298.contains(e) }}()))

expect::  (res299 is ["1"])

Expressions in messages

send::  msg expr.oper.sumnum  (a:Number<-1, b:Number<-2)

expect::  (res is 3)
send::  msg expr.oper.sumnum2  (a:Number<-1)

expect::  (res is 3)
send::  msg expr.oper.sumnum3  (a:Number<-1)

expect::  (res is 3)

send::  msg expr.oper.sumstr  (first<-"Jane", last<-"Doe")

expect::  (res is "Jane Doe")

Properties

send::  msg diesel.try 

send::  msg diesel.props.system 

expect::  (payload containsNot "No permission")$ifc::diesel.[="isLocalhost"]
expect::  (payload contains "No permission")$ifc::NOT (diesel.[="isLocalhost"])
expect::  (payload[="diesel.testprop"] is "tp")$ifc::diesel.[="isLocalhost"]

send::  msg diesel.props.file  (path<-"./rk-test.properties")

expect::  (payload[="rk.safemode"] is defined)$ifc::diesel.[="isLocalhost"]

send::  msg diesel.props.jsonFile  (path<-"./conf/testdoc.json")

expect::  (payload[="ENV_NAME"] is defined)$ifc::diesel.[="isLocalhost"]

Props

Can only access system props in your local server, not our cloud

send::  msg diesel.props.system 

expect::  (payload containsNot "No permission")$ifc::diesel.[="isLocalhost"]
expect::  (payload contains "No permission")$ifc::NOT (diesel.[="isLocalhost"])

send::  msg diesel.catch 

sizeOf

send::  msg ctx.set  (a358:Array<-[1,2,3])

send::  msg ctx.echo  (x<-sizeOf(a358))

expect::  ( (sizeOf(a358) > 0))

send::  msg test.diesel.condif 

expect::  (b138 is true)

Special cases

Just making sure this parsed:

send::  msg dieseltest.expr151  (env:Number<-123)

expect::  (res151 >= 1)

end


Was this useful?    

By: Razie | 2017-09-02 .. 2020-04-22 | Tags: story , dsl


Viewed 135 times ( | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2020, all rights reserved.