@@ -22,6 +22,8 @@ import (
22
22
"time"
23
23
24
24
"github.com/google/cel-go/common/ast"
25
+ "github.com/google/cel-go/common/decls"
26
+ "github.com/google/cel-go/common/env"
25
27
"github.com/google/cel-go/common/operators"
26
28
"github.com/google/cel-go/common/overloads"
27
29
"github.com/google/cel-go/common/stdlib"
@@ -94,26 +96,61 @@ func Lib(l Library) EnvOption {
94
96
}
95
97
}
96
98
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
+
97
110
// 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 )
100
117
}
101
118
102
119
// stdLibrary implements the Library interface and provides functional options for the core CEL
103
120
// features documented in the specification.
104
- type stdLibrary struct {}
121
+ type stdLibrary struct {
122
+ subset * env.LibrarySubset
123
+ }
105
124
106
125
// LibraryName implements the SingletonLibrary interface method.
107
- func (stdLibrary ) LibraryName () string {
126
+ func (* stdLibrary ) LibraryName () string {
108
127
return "cel.lib.std"
109
128
}
110
129
111
130
// 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
+ }
113
150
return []EnvOption {
114
151
func (e * Env ) (* Env , error ) {
115
152
var err error
116
- for _ , fn := range stdlib . Functions () {
153
+ for _ , fn := range funcs {
117
154
existing , found := e .functions [fn .Name ()]
118
155
if found {
119
156
fn , err = existing .Merge (fn )
@@ -125,16 +162,12 @@ func (stdLibrary) CompileOptions() []EnvOption {
125
162
}
126
163
return e , nil
127
164
},
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 ... ),
133
166
}
134
167
}
135
168
136
169
// ProgramOptions returns function implementations for the standard CEL functions.
137
- func (stdLibrary ) ProgramOptions () []ProgramOption {
170
+ func (* stdLibrary ) ProgramOptions () []ProgramOption {
138
171
return []ProgramOption {}
139
172
}
140
173
0 commit comments