Find identifiers automatically

58 views
Skip to first unread message

Rodrigo Trujillo

unread,
Mar 19, 2019, 1:17:59 PM3/19/19
to CEL Go Discussion Forum
Hi all, 

First of all, great work, I started to play with CEL-go this week and we are planning to used it in my project.
I have read the documentation and have a simple doubt:

- Having a string expression and, without declare the identifiers, could they abe found during evaluation phase ?

For instance:
I have following expression, where "fruit"  would be the identifier
"I love fruit"

then the evaluation would be like:

out, _, err := prg.Eval(cel.Vars(map[string]interface{}{
        "fruit": "APPLE" }))
if err != nil {
    ...

And I would get  "I love APPLE"

--------------

I know from examples that the expression should be `"I love " + fruit`, and I would have to declare fruit as an identifier like decls.NewIdent("fruit", decls.String, nil),
but I am just looking for other possible options that would fit my needs.

Thanks,

Rodrigo Trujillo

Jim Larson

unread,
Mar 19, 2019, 4:32:17 PM3/19/19
to Rodrigo Trujillo, CEL Go Discussion Forum
Hi, Rodrigo!

We're glad that you're exploring CEL!  We hope you'll find it useful for your needs.  If you can explain your use case a little we might be able to give further advice, but I'll try to address the question at hand first.

You're right - CEL would treat "I love fruit" as a string literal and would not be able to treat the words inside it as variables.  CEL only recognizes the expression

    "I love " + fruit

as having a variable:  "fruit".  Evaluation bindings will only be available for those things that are recognized as variables.  CEL won't substitute other bindings even if they match words within strings.

One thing you can do is skip the check phase, so you wouldn't have to provide the declaration.  Once you parse, just turn the parse results into a program and add the binding for "fruit".  Note that if you skip the declarations and checking, you might experience failures at runtime that wouldn't occur for a successfully checked expression.  For instance, if you bind "fruit" to the value 17 at runtime, evaluation will fail because we don't have a way to add strings and integers.  But if you declared "fruit" to be an int and tried to check it, you'd get a check failure and you'd know not to evaluate it.

I hope that helps.  If that doesn't quite address your needs, then please let us know a little more about what you're trying to do.

Jim
Google CEL Team



--
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 post to this group, send email to cel-go-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/15ef9d58-8091-4ea1-9240-a3f9b319083e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rodrigo Trujillo

unread,
Mar 20, 2019, 10:33:14 AM3/20/19
to Jim Larson, CEL Go Discussion Forum
Hi Jim,
thank you for the quick answer, I will try to skip the phases like you suggested.

About my use case: I am working in an ETL pipeline e driven by some microservices. In a nutshell, one of the services receive some template strings, like "I love fruit"  and a database query job.
"fruit" would be one of the columns of the query return, then I substitute in the string pass the result forward (for each row). In future we are going to need arithmetic calculation and problem check some conditions as well.
So, CEL seems to be a good choice for help in this processing.

As we already have some string templates ready, I was looking for a way to use them without rewrite. But thats ok, I will separate in strings and variables. 

Rodrigo



For more options, visit https://groups.google.com/d/optout.


--

Rodrigo Trujillo

SOFTWARE DEVELOPER

Auvik Networks Inc.

Jim Larson

unread,
Mar 20, 2019, 1:12:04 PM3/20/19
to Rodrigo Trujillo, CEL Go Discussion Forum
Rodrigo,

Thanks for the explanation, and yes, I think CEL would be a good fit for you.  I've looked at some template languages a while back and they're quite nice when your use case fits within their capabilities, but once you get into more advanced usage a more conventional programming language starts to be a better fit.  Converting your templates into string construction expressions will be useful not only for CEL but for many other languages too, so it seems like a good use of time.

Please let us know how we can be of help in the future.

Jim


Reply all
Reply to author
Forward
0 new messages