diff --git a/cel/env.go b/cel/env.go index 473604bc..0d9553ff 100644 --- a/cel/env.go +++ b/cel/env.go @@ -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 { diff --git a/cel/env_test.go b/cel/env_test.go index ed1f836e..54be88ce 100644 --- a/cel/env_test.go +++ b/cel/env_test.go @@ -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++ {