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

Add function accessor to Env #978

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ func (e *Env) Libraries() []string {
return libraries
}

// HasFunction returns whether a specific function has been configured in the environment
func (e *Env) HasFunction(functionName string) bool {
_, ok := e.functions[functionName]
return ok
}

// Functions returns map of Functions, keyed by function name, that have been configured in the environment.
func (e *Env) Functions() map[string]*decls.FunctionDecl {
return e.functions
}

// HasValidator returns whether a specific ASTValidator has been configured in the environment.
func (e *Env) HasValidator(name string) bool {
for _, v := range e.validators {
Expand Down
16 changes: 16 additions & 0 deletions cel/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ func TestLibraries(t *testing.T) {
}
}

func TestFunctions(t *testing.T) {
e, err := NewEnv(OptionalTypes())
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
for _, expected := range []string{"optional.of", "or"} {
TristonianJones marked this conversation as resolved.
Show resolved Hide resolved
if !e.HasFunction(expected) {
t.Errorf("Expected HasFunction() to return true for '%s'", expected)
}

if _, ok := e.Functions()[expected]; !ok {
t.Errorf("Expected Functions() to include '%s'", expected)
}
}
}

func BenchmarkNewCustomEnvLazy(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down