Allow easily using variables such as `type` that are already defined by `Stdlib`
Change Today it is not possible to add a variable that is contained in the following list:
stdTypes = []*decls.VariableDecl{
decls.TypeVariable(types.BoolType),
decls.TypeVariable(types.BytesType),
decls.TypeVariable(types.DoubleType),
decls.TypeVariable(types.DurationType),
decls.TypeVariable(types.IntType),
decls.TypeVariable(listOfA),
decls.TypeVariable(mapOfAB),
decls.TypeVariable(types.NullType),
decls.TypeVariable(types.StringType),
decls.TypeVariable(types.TimestampType),
decls.TypeVariable(types.TypeType),
decls.TypeVariable(types.UintType),
}
It could have been nice to allow, for example, adding type as a variable - it should work since if it is not used as a function type() CEL knows to differentiate between the two.
Example
type == 'my-variable'
Alternatives considered
- Use
NewCustomEnv- I tried this, but this was very complicated since it disabled many functions such as_==_for all types. I basically just want to free the above types as variables. - Use
//go:linkname stdTypes github.com/google/cel-go/common/stdlib.stdTypesand set it to an empty liststdTypes = []*decls.VariableDecl{}- this works but affects all CEL usages and generally should be avoided.
The type identifier is part of the CEL specification, e.g. type(string) == type, so it could be quite confusing if this were overridden and would make certain parts of CEL inoperable.
Makes sense.
But still:
- When using
NewCustomEnv- these are not a part of that environment. - Also there are options to take the default environment and customize it (i.e. entirely remove the
type()function)
So I would assume it is possible to remove those type informations from the env via some option.
In some way it also makes sense because custom types also do not have identifiers added to the CEL by default.