@@ -22,6 +22,8 @@ import (
2222 "time"
2323
2424 "github.com/google/cel-go/common/ast"
25+ "github.com/google/cel-go/common/decls"
26+ "github.com/google/cel-go/common/env"
2527 "github.com/google/cel-go/common/operators"
2628 "github.com/google/cel-go/common/overloads"
2729 "github.com/google/cel-go/common/stdlib"
@@ -94,26 +96,61 @@ func Lib(l Library) EnvOption {
9496 }
9597}
9698
99+ // StdLibOption specifies a functional option for configuring the standard CEL library.
100+ type StdLibOption func (* stdLibrary ) * stdLibrary
101+
102+ // StdLibSubset configures the standard library to use a subset of its functions and macros.
103+ func StdLibSubset (subset * env.LibrarySubset ) StdLibOption {
104+ return func (lib * stdLibrary ) * stdLibrary {
105+ lib .subset = subset
106+ return lib
107+ }
108+ }
109+
97110// StdLib returns an EnvOption for the standard library of CEL functions and macros.
98- func StdLib () EnvOption {
99- return Lib (stdLibrary {})
111+ func StdLib (opts ... StdLibOption ) EnvOption {
112+ lib := & stdLibrary {}
113+ for _ , o := range opts {
114+ lib = o (lib )
115+ }
116+ return Lib (lib )
100117}
101118
102119// stdLibrary implements the Library interface and provides functional options for the core CEL
103120// features documented in the specification.
104- type stdLibrary struct {}
121+ type stdLibrary struct {
122+ subset * env.LibrarySubset
123+ }
105124
106125// LibraryName implements the SingletonLibrary interface method.
107- func (stdLibrary ) LibraryName () string {
126+ func (* stdLibrary ) LibraryName () string {
108127 return "cel.lib.std"
109128}
110129
111130// CompileOptions returns options for the standard CEL function declarations and macros.
112- func (stdLibrary ) CompileOptions () []EnvOption {
131+ func (lib * stdLibrary ) CompileOptions () []EnvOption {
132+ funcs := stdlib .Functions ()
133+ macros := StandardMacros
134+ if lib .subset != nil {
135+ subMacros := []Macro {}
136+ for _ , m := range macros {
137+ if lib .subset .SubsetMacro (m .Function ()) {
138+ subMacros = append (subMacros , m )
139+ }
140+ }
141+ macros = subMacros
142+ subFuncs := []* decls.FunctionDecl {}
143+ for _ , fn := range funcs {
144+ if f , include := lib .subset .SubsetFunction (fn ); include {
145+ subFuncs = append (subFuncs , f )
146+ }
147+ }
148+ funcs = subFuncs
149+ }
113150 return []EnvOption {
114151 func (e * Env ) (* Env , error ) {
115152 var err error
116- for _ , fn := range stdlib . Functions () {
153+ for _ , fn := range funcs {
117154 existing , found := e .functions [fn .Name ()]
118155 if found {
119156 fn , err = existing .Merge (fn )
@@ -125,16 +162,12 @@ func (stdLibrary) CompileOptions() []EnvOption {
125162 }
126163 return e , nil
127164 },
128- func (e * Env ) (* Env , error ) {
129- e .variables = append (e .variables , stdlib .Types ()... )
130- return e , nil
131- },
132- Macros (StandardMacros ... ),
165+ Macros (macros ... ),
133166 }
134167}
135168
136169// ProgramOptions returns function implementations for the standard CEL functions.
137- func (stdLibrary ) ProgramOptions () []ProgramOption {
170+ func (* stdLibrary ) ProgramOptions () []ProgramOption {
138171 return []ProgramOption {}
139172}
140173
0 commit comments