cel-go icon indicating copy to clipboard operation
cel-go copied to clipboard

Allow easily using variables such as `type` that are already defined by `Stdlib`

Open same-id opened this issue 2 months ago • 2 comments

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

  1. 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.
  2. Use //go:linkname stdTypes github.com/google/cel-go/common/stdlib.stdTypes and set it to an empty list stdTypes = []*decls.VariableDecl{} - this works but affects all CEL usages and generally should be avoided.

same-id avatar Dec 03 '25 14:12 same-id

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.

TristonianJones avatar Dec 05 '25 00:12 TristonianJones

Makes sense.

But still:

  1. When using NewCustomEnv - these are not a part of that environment.
  2. 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.

same-id avatar Dec 07 '25 06:12 same-id