Repository navigation
Replies: 2 comments
|
A bit more detail: At go compile time, I don't know which variables are needed and I don't know which types are needed. At CEL compile time, I know which types are needed and which variables. The service I'm hoping to integrate CEL into is a workflow system where every node has a defined output described by a simplified version of JSON Schema. In some nodes I want to allow usage of CEL which has access to the output of all previous nodes. An example could be a if/then/else node where you describe the condition using CEL. So if a previous node called userLookup has a user as output, a if/then/else node could access that user in CEL like The node types in my system can be defined by plugins at runtime, so the node types and their output schema is not known at Go compile time, but it is at CEL compile time. |
|
Since you know the variable types when you compile the expression, build the env at that point rather than keeping one global env: opts := []cel.EnvOption{}
for name, typ := range schema { // typ comes from your simplified schema
opts = append(opts, cel.Variable(name, typ))
}
env, err := cel.NewEnv(opts...)
ast, iss := env.Compile(expr)For a structured node output, use cel.ObjectType with a registered type description so field access stays type checked. Use cel.MapType(cel.StringType, cel.DynType) when the schema is too loose to describe, and cel.DynType only for the subtree that is genuinely unknown. Every DynType you add gives up static checking there. For the stretch goal, there is no lazy variable declaration hook. The usual pattern is to cache one env per schema signature: compute a key from the variable names and types you saw, and reuse the env when the key matches. Compilation stays cheap and the checker still sees all the types. |
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to figure out if I can use cel-go for my project, but I keep going around in circles and need some help.
I'm in a situation where I don't know the types of variables at Go compile time so I need dynamic types. I do however know the variable types at CEL compile time, and would love to get compile and type errors from the CEL parser/checker.
I feel like this should be possible, but I can't figure it out.
Stretch goal: The number of variables can be quite large, so it would be awesome if I don't have to specify them all in the env, but rather supply the types on demand during CEL compilation.
All reactions