YAQL - Filtering

214 views
Skip to first unread message

rem...@gmail.com

unread,
Aug 31, 2015, 6:54:13 PM8/31/15
to StackStorm
Hello all!

Im having some issues with YAQL filtering when not specifically defining the comparator.  I am wondering if the following is possible.

user_data:
[
  {
    "tenant_id": 123,
    "username": "user1",
    "roles": ["admin"]
  },
  {
    "tenant_id": 321,
    "username": "user2",
    "roles": ["member"]
  },
  {
    "tenant_id": 123,
    "username": "user3",
    "roles": ["admin"]
  }
]

users:
[ 123, 321 ].

I have the following workflow item :

user_task:
with-items: uid in <% $.users %>
action: ...
input:
uid: <% $.uid %>
user_data: <% $.user_data[$.tenant_id = <% $.uid %>] %>

I cannot seem to get the filter to work. It works if I explicitly set the uid ($.tenant_id = 123)... however if I try [$.tenant_id = $.uid] I get an empty array. The above example just fails.

Are comparisons using variables allowed in YAQL?

Thanks


Dmitri Zimine

unread,
Sep 1, 2015, 2:31:12 AM9/1/15
to rem...@gmail.com, StackStorm
Hi remuso, 
Thanks for reporting, let’s try to figure this out.

first thing is don’t do double quotes <% $.user_data[$.tenant_id = <% $.uid %>] %>, 
make it: <% $.user_data[$.tenant_id = $.uid] %>

But this is beyond the point; you’re right it doesn’t work. But supposed to work. Looks like a bug. Will be looking. 

======



--
You received this message because you are subscribed to the Google Groups "StackStorm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stackstorm+...@googlegroups.com.
To post to this group, send email to stack...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/stackstorm/89f8c98d-2365-4635-b11b-538d2395d862%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dmitri Zimine

unread,
Sep 1, 2015, 10:56:23 AM9/1/15
to rem...@gmail.com, StackStorm
Remuso, 

turned out it’s not a bug. 
The statement which works is:

$.as($.uid=>uid).user_data[$.tenant_id=$uid]

Below are detailed explanation I got from one of YAQL authors, Alex Tivelkov.

Enjoy! 
=======

Hi Dmitri,

Nope, this is not a bug.

The trick here is that there is a context change when you do a filtration: inside the square brackets (or, more precisely, inside the "where" function which is aliased by square brackets in 0.2.x) the $ variable changes its meaning: it means the current element of a collection, not the initial input.
So, if you want to achieve such a filtering behaviour, you need to save the value of uid at some other variable which would not be overwritten when the context changes.  In 0.2.x you may use "as" function for this.
The expression should look like this:

$.as($.uid=>uid).user_data[$.tenant_id=int($uid)]

"As" function accepts a tuple, in which is first element is an expression to be evaluated on a passed data ($.uid in this case) and the second element is a string. The result of the evaluation will be saved in the context in a new variable having the name equal to the second element of the tuple. So, in all subsequent functions of the chain this value will be accessible by the appropriate variable name ($uid in this case - notice that there is no dot after the $)

Also, notice the usage of int() function - it is needed indeed, as the initial $.uid is string and tenant_id in each item is an integer. Another option would be to cast the value to int when saving in context instead of when doing comparison, like this:
$.as(int($.uid)=>uid).user_data[$.tenant_id=$uid]

This statement has a slightly better performance on large collections, as the string-to-integer casting happens only once (when the variable is created), while the previous one does it for every element in the collection.



rem...@gmail.com

unread,
Sep 1, 2015, 11:01:40 AM9/1/15
to StackStorm, rem...@gmail.com
Hey Dmitri,

Thanks for this, it works out as expected!  Also, thanks for the quick responses and reaching out to the YAQL developers. 

Thanks again!

Dmitri Zimine

unread,
Sep 1, 2015, 11:20:03 AM9/1/15
to rem...@gmail.com, StackStorm
Sure. 

Consider joining our Slack channel where we answer questions live - it’s like IRC but the history is preserved so you can find the previous answers. Registration link here.

And if you don’t mind sharing, what is your use case for StackStorm, and what company are you guys? If you want to stay stealth, fine :)

Cheers, 
DZ> 

rem...@gmail.com

unread,
Sep 8, 2015, 1:22:00 PM9/8/15
to StackStorm, rem...@gmail.com
Hey Dmitri,

Sorry for the delayed response.  I'll join up in the Slack channel next time I have some questions. 

We're POCing it for some internal escalations type stuff potentially using a mixture of sensors and manual initiation via the webhooks.  I'm currently working at Oracle. 

Thanks

Dmitri Zimine

unread,
Sep 9, 2015, 3:54:21 AM9/9/15
to rem...@gmail.com, StackStorm
Thanks for the info! Cheers, Dmitri.

t.le...@gmail.com

unread,
May 24, 2018, 1:57:13 PM5/24/18
to StackStorm

It seems that in recent versions of YAQL, .as() is depreciated.
My proposal for this issue is:
let(uid => $.uid=) -> $.user_data[$.tenant_id=$uid]
Reply all
Reply to author
Forward
0 new messages