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 Libraries() function to Env #822

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,15 @@ func (e *Env) HasLibrary(libName string) bool {
return exists && configured
}

// Libraries returns a list of SingletonLibrary that have been configured in the environment.
func (e *Env) Libraries() []string {
libraries := make([]string, len(e.libraries))
for libName := range e.libraries {
libraries = append(libraries, libName)
}
return libraries
}

// 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
20 changes: 20 additions & 0 deletions cel/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ func TestTypeProviderInterop(t *testing.T) {
}
}

func TestLibraries(t *testing.T) {
e, err := NewEnv(OptionalTypes())
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
for _, expected := range []string{"cel.lib.std", "cel.lib.optional"} {
if !e.HasLibrary(expected) {
t.Errorf("Expected HasLibrary() to return true for '%s'", expected)
}
libMap := map[string]struct{}{}
for _, lib := range e.Libraries() {
libMap[lib] = struct{}{}
}

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

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