cel expression to match list items?

491 views
Skip to first unread message

Rucha Deshpande

unread,
May 4, 2020, 11:27:29 AM5/4/20
to CEL Go Discussion Forum
Hello All,

 I am looking to find an optimized way of matching items in  2 lists. For example:
User                                           Data 
   Group1                                     Group1
   Group2                                     Group5
   Group3                                     Group6


I want to evaluate whether an element from  User Groups List has a matching element in Data Groups list.
Here Group1 matches, so result would be true.

Is there an optimized CEL way of doing this?
or do I have to write an iterator?

Thank you!

Jim Larson

unread,
May 4, 2020, 2:54:58 PM5/4/20
to Rucha Deshpande, CEL Go Discussion Forum
Hi, Rucha!

If you don't mind a quadratic-time solution, i.e. if your lists are small or if your performance expectations are loose, you can use nested macros.

    user.exists(u, data.exists(d, u == d))

Otherwise, if you want a more efficient algorithm (e.g. pre-sorting both lists and using a linear-time match finder), you'd have to implement it yourself.

Jim


--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/21ea19bb-48a5-4a0c-8b08-cb7f47276e7b%40googlegroups.com.

Rucha Deshpande

unread,
May 4, 2020, 6:20:18 PM5/4/20
to CEL Go Discussion Forum

Awesome! That works...
The list is going to be fairly small ... 7 to 8 elements in both the list so this will work for now.

This is working when I pass the list in the request:
decisions, err := enforcer.Evaluate(map[string]interface{}{
"User":"srihari",
"Service":"Storage",
"Resource":"/Get",
"Usergroups": []string{"service.storage.admin","data.default.admin"},
})


and my rule is:
Usergroups.exists(u,groups_to_match_storage_get.exists(d,u==d))

However, if the usergroups list is dynamic which I need to retrieve from a datastore by using  a rule such as:
getUserGroups(User).exists(u,groups_to_match_storage_get.exists(d,u==d))

and
(right now only returning static list)

getUserGroups = policy.Functions(&functions.Overload{
Operator: "get_usergroups_string",
Unary: func(User ref.Val) interface{} {
list:= []string{"A","B"}
return list

},
})

This does not work.
I did not find examples with custom functions returning lists. Can you help?


On Monday, May 4, 2020 at 1:54:58 PM UTC-5, Jim Larson wrote:
Hi, Rucha!

If you don't mind a quadratic-time solution, i.e. if your lists are small or if your performance expectations are loose, you can use nested macros.

    user.exists(u, data.exists(d, u == d))

Otherwise, if you want a more efficient algorithm (e.g. pre-sorting both lists and using a linear-time match finder), you'd have to implement it yourself.

Jim


On Mon, May 4, 2020 at 8:27 AM Rucha Deshpande <rborg...@gmail.com> wrote:
Hello All,

 I am looking to find an optimized way of matching items in  2 lists. For example:
User                                           Data 
   Group1                                     Group1
   Group2                                     Group5
   Group3                                     Group6


I want to evaluate whether an element from  User Groups List has a matching element in Data Groups list.
Here Group1 matches, so result would be true.

Is there an optimized CEL way of doing this?
or do I have to write an iterator?

Thank you!

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-...@googlegroups.com.

Tristan Swadell

unread,
May 4, 2020, 8:01:45 PM5/4/20
to Rucha Deshpande, CEL Go Discussion Forum
Hi Rucha, the `ext/strings.go` in cel-go is a pretty good example of how to do what you're looking for:

-Tristan

To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/1e7a5017-02b5-45e6-b0a4-5c80d385f7ea%40googlegroups.com.

Rucha Deshpande

unread,
May 7, 2020, 10:29:48 AM5/7/20
to CEL Go Discussion Forum
Thank you Tristan.. that helped!
Reply all
Reply to author
Forward
0 new messages