Skip to content

Commit

Permalink
Add Libraries() function to Env (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbetz authored Aug 23, 2023
1 parent 78039f1 commit 8a45955
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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

0 comments on commit 8a45955

Please sign in to comment.