Skip to content

Commit ff98bc0

Browse files
authored
Add Libraries() function to Env (#823)
1 parent f118dce commit ff98bc0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cel/env.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,15 @@ func (e *Env) HasLibrary(libName string) bool {
388388
return exists && configured
389389
}
390390

391+
// Libraries returns a list of SingletonLibrary that have been configured in the environment.
392+
func (e *Env) Libraries() []string {
393+
libraries := make([]string, len(e.libraries))
394+
for libName := range e.libraries {
395+
libraries = append(libraries, libName)
396+
}
397+
return libraries
398+
}
399+
391400
// HasValidator returns whether a specific ASTValidator has been configured in the environment.
392401
func (e *Env) HasValidator(name string) bool {
393402
for _, v := range e.validators {

cel/env_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,26 @@ func TestTypeProviderInterop(t *testing.T) {
228228
}
229229
}
230230

231+
func TestLibraries(t *testing.T) {
232+
e, err := NewEnv(OptionalTypes())
233+
if err != nil {
234+
t.Fatalf("NewEnv() failed: %v", err)
235+
}
236+
for _, expected := range []string{"cel.lib.std", "cel.lib.optional"} {
237+
if !e.HasLibrary(expected) {
238+
t.Errorf("Expected HasLibrary() to return true for '%s'", expected)
239+
}
240+
libMap := map[string]struct{}{}
241+
for _, lib := range e.Libraries() {
242+
libMap[lib] = struct{}{}
243+
}
244+
245+
if _, ok := libMap[expected]; !ok {
246+
t.Errorf("Expected Libraries() to include '%s'", expected)
247+
}
248+
}
249+
}
250+
231251
func BenchmarkNewCustomEnvLazy(b *testing.B) {
232252
b.ResetTimer()
233253
for i := 0; i < b.N; i++ {

0 commit comments

Comments
 (0)