using inline vars in expressions

26 views
Skip to first unread message

Mohit Nagpal

unread,
Nov 11, 2023, 3:21:49 PM11/11/23
to CEL Go Discussion Forum


trying to use inline vars in expressions. 
code to declare inline vars does not compile

// var glossList: 'payload.glossary.GlossDiv.GlossList'
// expression : glossList.GlossEntry.ID == 'SGML'
ast, iss := env.Compile("payload.glossary.GlossDiv.GlossList")
if iss.Err() != nil {
fmt.Printf("env.Compile(%s) failed: %s", "variable", iss.Err())
}
cel.NewInlineVariable("glossList", ast) // does not compile.

full code
playground link https://go.dev/play/p/YgxDTtUtRQK   

Tristan Swadell

unread,
Nov 11, 2023, 4:11:16 PM11/11/23
to Mohit Nagpal, CEL Go Discussion Forum
Hi Mohit,

You will need to setup an InliningOptimizer for the inlining to work:

inlineAST, iss := env.Compile("payload.glossary.GlossDiv.GlossList")
inlinedVars = append(inlinedVars, cel.NewInlineVariable("glossList", inlineAST))
exprAST, iss := env.Compile("glossList.Glossary.ID == 'SGML')
opt := cel.NewStaticOptimizer(cel.NewInliningOptimizer(inlinedVars...))
optimized, iss := opt.Optimize(env, exprAST)

-Tristan

--
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/1c0ea8ee-0140-4c51-8ab5-3f492f4b81dcn%40googlegroups.com.

Mohit Nagpal

unread,
Nov 11, 2023, 7:43:06 PM11/11/23
to CEL Go Discussion Forum
cel.NewInlineVariable does not compile. is there any goplayground link for sample to run expression end to end with inline vars and optimizer.

Tristan Swadell

unread,
Nov 13, 2023, 11:40:01 AM11/13/23
to Mohit Nagpal, CEL Go Discussion Forum
Hi Mohit,

You are welcome to look at the structure of the `cel/inlining_test.go` which should give you a couple of examples of the inlining setup and execution. I don't typically put together go playground examples for CEL since they usually timeout and end up not being that useful.

The key thing to remember is that you need to declare the `payload` variable in the first `cel.Env` instance, and the `glossList` variable in the second `cel.Env` instance. Only after both expression compile can one be inlined into the other.

Regards,

-Tristan

Reply all
Reply to author
Forward
0 new messages