Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we allow users to disable specific functions in Checker? #899

Open
j2gg0s opened this issue Feb 21, 2024 · 2 comments
Open

Can we allow users to disable specific functions in Checker? #899

j2gg0s opened this issue Feb 21, 2024 · 2 comments

Comments

@j2gg0s
Copy link

j2gg0s commented Feb 21, 2024

We only want to use a subset of CEL.

@j2gg0s
Copy link
Author

j2gg0s commented Feb 21, 2024

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{}
}

@TristonianJones
Copy link
Collaborator

TristonianJones commented Feb 21, 2024

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 {
 ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants