expr-json-story Pub

Json expressions

send::  msg expr.json.create  (customer<-"Jane")

expect::  (cart[="id"] is "Jane")
expect::  (cart contains "Jane")

send::  msg expr.json.addItem  (sku<-"book1", quantity:Number<-2)

send::  msg expr.json.addItem  (sku<-"book2", quantity:Number<-3)

expect::  (cart is JSON)
expect::  (cart[="items"] is Array)
expect::  (cart[="items"][=1] contains "book2")

Access inside JSON object using diesel expressions: send::  msg ctx.set  (size<-sizeOf(x=cart[="items"]))

expect::  (size is 2)

Or using JS expression - you have more flexibility this way (costs more CPU) send::  msg ctx.set  (size74<-js{{ cart.items.length }})

expect::  (size74 is 2)

You can do more fancy access: send::  msg ctx.set  (res78<-js{{ cart.items[0].sku }})

expect::  (res78 is "book1")

send::  msg ctx.set  (res84:JSON<-({a:"aa"} + {version:"V2"}))

expect::  (res84[="version"] == "V2")

Accessors

send::  msg ctx.set  (fieldName<-"items")

send::  msg ctx.set  (res240<-cart[="items"])

expect::  (res240 is Array)
send::  msg ctx.set  (res241<-cart[="items"][=0][:String="sku"])

expect::  (res241 is "book1")
send::  msg ctx.set  (res242<-cart[=fieldName][=0][:String="sku"])

expect::  (res242 is "book1")

More stuff: foreach

send::  msg ctx.foreach  (list<-cart[="items"], item<-"item", msg<-"testing.invcheck")

send::  msg ctx.set  (ajson:JSON<-{a-num:123,bnum:321,a-string:abc})

expect::  (ajson[="bnum"] is number)
expect::  (ajson[="bnum"] is 321)
send::  msg ctx.set  (line87<-js{{ ajson.bnum }})

expect::  (line87 is 321)
send::  msg ctx.set  (line89<-js{{ ajson["a-num"] }})

expect::  (line89 is 123)

Array of json

they map into array of json, we can acces members or result

send::  msg ctx.set  (g45:Array<-[{a:"aa"},{a:"bb"}])

send::  msg ctx.set  (g46<-(g45 map ( x->(x + {_id:x[="a"]})() )))

expect::  (g46[=0][:String="_id"] is "aa")

More

Support multi-line and ID with quotes, double quotes or nothing...

send::  msg ctx.set  (ajson2:JSON<-{a-num:123,bnum:321,a-string:abc})

expect::  (ajson2[="bnum"] is number)
expect::  (ajson2[="bnum"] is 321)

Make sure 123 is kept as a number, not "123"

send::  msg ctx.set  (body101:JSON<-{accountNumber:"1729534",start_num:123})

expect::  (body101 ~= "(?s).*: 123.")

And also that we preserve the input string send::  msg ctx.set  (body110:JSON<-{accountNumber:"1729534",start_num:"123"})

expect::  (body110 ~= "(?s).:"123".*")

send::  msg ctx.json  (a-num:Number<-123, bnum:Number<-321, a-string<-abc)

expect::  (payload[="nada"] not defined)
expect::  (payload[="bnum"] is 321)

send::  msg expr.json.create2  (a:Number<-1, b:Number<-2)

expect::  (payload[="a"] is 1)

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

expect::  (payload[="a"] is 1)
expect::  (payload[="b"] is 2)

More JSON examples

send::  msg ctx.echo  (msg:JSON<-{})

expect::  (msg is {})

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

expect::  (msg is [])

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

expect::  (msg is [1,"a",3])

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

send::  msg ctx.echo  (msg:Array<-[a,"a",b])

expect::  (msg is [1,"a",2])

send::  msg ctx.set  (msg127:JSON<-{a:"a"})

expect::  (msg127 contains ""a"")

send::  msg ctx.set  (msg130:JSON<-{a:[1,"a",b]})

TODO access arrays msg.a is an array

// $expect (msg130.a contains 1)

send::  msg ctx.set  (msg133:JSON<-{a:{a:3}})

expect::  (msg133[="a"][="a"] is 3)

send::  msg ctx.set  (msg:JSON<-{status:"ok",someStats:"67",accountId:a})

Numbers type

send::  msg expr.simplejson.do  (x:Number<-3)

expect::  (payload[="x"] is 3)
expect::  (payload[="x"] is Number)

calling a message with an object: flattening

This will match (a,b)

send::  msg ctx.json  (a:Number<-1, b:Number<-5)

send::  msg dieseltest.asAttrs  (payload.asAttrs)

expect::  (res33 is 6)

This will not match (a,b)

send::  msg ctx.json  (a:Number<-7)

send::  msg dieseltest.asAttrs  (payload.asAttrs)

expect::  (res33 is 6)

Complex objects

send::  msg ctx.set  (student:DieselTestStudent(JSON)<-{name:"John",address:{street:"Baker St",no:"121b"}})

send::  msg ctx.echo  (m<-student)

send::  msg ctx.echo  (m<-student[="address"][="no"])

// $send ctx.set(student.address.no="65") send::  msg test.diesel.rule40 

send::  msg ctx.echo  (m<-student[="address"][="no"])

expect::  (student[="address"][="no"] is "65")

Array operators

send::  msg ctx.set  (a171:Array<-[1,"a",3])

expect::  ( ("a" in [1,"a",3]))
expect::  ( ("a" in a171))
expect::  ( (3 in a171))
expect::  ( ("no" not in a171))

send::  msg test.diesel.isin  (x<-"123", y:Number<-3)

expect::  (y46 is true)
send:: msg  (y46=null)
send::  msg test.diesel.isin  (x<-"123", y:Number<-1)

expect::  (y46 not defined)
send:: msg  (y46=null)
send::  msg test.diesel.isin  (x<-"1234", y:Number<-1)

expect::  (y46 not defined)

Composing and subtracting from jsons:

send::  msg ctx.set  (j188:JSON<-{a:"a",b:"b"})

expect::  (j188[="b"] is "b")
send::  msg ctx.set  (j189<-(j188 + {c:"c"}))

expect::  (j189[="c"] is "c")

send::  msg ctx.set  (j192<-(j188 - {b:""}))

expect::  (j192[="b"] is undefined)

// ## TODO

// In a diesel:

// .todo this


Was this useful?    

By: Razie | 2019-06-10 | Tags: story , dsl


Viewed 71 times ( | Print ) this page.

You need to log in to post a comment!

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