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

Can we allow users to disable specific functions in Checker?

Open j2gg0s opened this issue 1 year ago • 2 comments

We only want to use a subset of CEL.

j2gg0s avatar Feb 21 '24 06:02 j2gg0s

Current, we define ourself Library to replace the StdLib.

type filterLibrary struct {
	functions map[string]bool
}

func (filterLibrary) LibraryName() string {
	return "cel.j2gg0s.filter"
}

func (lib filterLibrary) CompileOptions() []cel.EnvOption {
	opts := []cel.EnvOption{}
	for _, fn := range stdlib.Functions() {
		if !lib.functions[fn.Name()] {
			continue
		}
		fn := fn
		opts = append(opts, cel.Function(fn.Name(),
			func(*decls.FunctionDecl) (*decls.FunctionDecl, error) {
				return fn, nil
			}))
	}
	for _, typ := range stdlib.Types() {
		typ := typ
		opts = append(opts, cel.Variable(typ.Name(), typ.Type()))
	}
	opts = append(opts, cel.Macros(cel.StandardMacros...))
	return opts
}

func (filterLibrary) ProgramOptions() []cel.ProgramOption {
	return []cel.ProgramOption{}
}

j2gg0s avatar Feb 21 '24 13:02 j2gg0s

The ideal state would be to offer some options for subsetting on the StdLib. If you have suggestions for what you'd like to see there, I'd be happy to hear them and review a PR along these lines.

Since the standard library is a fixed set of functions and types, how people approach subsetting can sometimes be inclusive or exclusive, e.g. only these functions / overloads, everything except these functions / overloads.

Perhaps something like this:

type StdLibSubsetConfig struct {
   IncludeFunctions []string
   ExcludeFunctions []string
   IncludeOverloads []string
   ExcludeOverloads[]string
   IncludeMacros []string
   ExcludeMacros []string
}

func StdLibSubset(config *StdLibSubsetConfig) StdLibOption {
  ...
}

I'm tempted to package multiple configuration settings together rather than have separate options given how they can interact, but I'm open to suggestions.

The alternative might look like this:

func StdLibIncludeFunctions(names ...string) StdLibOption {
 ...
}
func StdLibExcludeFunctions(names ...string) StdLibOption {
 ...
}

TristonianJones avatar Feb 21 '24 16:02 TristonianJones