MCPcopy Create free account
hub / github.com/cel-expr/cel-go / ToConfig

Method ToConfig

cel/env.go:198–353  ·  view source on GitHub ↗

ToConfig produces a YAML-serializable env.Config object from the given environment. The serialized configuration value is intended to represent a baseline set of config options which could be used as input to an EnvOption to configure the majority of the environment from a file. Note: validators,

(name string)

Source from the content-addressed store, hash-verified

196// and the standard expression components (parse, check, evalute), they are also not
197// supported by the serialize method.
198func (e *Env) ToConfig(name string) (*env.Config, error) {
199 conf := env.NewConfig(name)
200 // Container settings
201 if e.Container != containers.DefaultContainer {
202 conf.SetContainer(e.Container.Name())
203 }
204 for _, typeName := range e.Container.AliasSet() {
205 conf.AddImports(env.NewImport(typeName))
206 }
207
208 // Serialize features
209 for featID, enabled := range e.features {
210 featName, found := featureNameByID(featID)
211 if !found {
212 // If the feature isn't named, it isn't intended to be publicly exposed
213 continue
214 }
215 conf.AddFeatures(env.NewFeature(featName, enabled))
216 }
217
218 libOverloads := map[string][]string{}
219 for libName, lib := range e.libraries {
220 // Track the options which have been configured by a library and
221 // then diff the library version against the configured function
222 // to detect incremental overloads or rewrites.
223 libEnv, _ := NewCustomEnv()
224 libEnv, _ = Lib(lib)(libEnv)
225 for fnName, fnDecl := range libEnv.Functions() {
226 if len(fnDecl.OverloadDecls()) == 0 {
227 continue
228 }
229 overloads, exist := libOverloads[fnName]
230 if !exist {
231 overloads = make([]string, 0, len(fnDecl.OverloadDecls()))
232 }
233 for _, o := range fnDecl.OverloadDecls() {
234 overloads = append(overloads, o.ID())
235 }
236 libOverloads[fnName] = overloads
237 }
238 subsetLib, canSubset := lib.(LibrarySubsetter)
239 alias := ""
240 if aliasLib, canAlias := lib.(LibraryAliaser); canAlias {
241 alias = aliasLib.LibraryAlias()
242 libName = alias
243 }
244 if libName == "stdlib" && canSubset {
245 conf.SetStdLib(subsetLib.LibrarySubset())
246 continue
247 }
248 version := uint32(math.MaxUint32)
249 if versionLib, isVersioned := lib.(LibraryVersioner); isVersioned {
250 version = versionLib.LibraryVersion()
251 }
252 conf.AddExtensions(env.NewExtension(libName, version))
253 }
254
255 // If this is a custom environment without the standard env, mark the stdlib as disabled.

Callers 2

TestEnvToConfigFunction · 0.95

Calls 15

SetContainerMethod · 0.95
AddImportsMethod · 0.95
AddFeaturesMethod · 0.95
FunctionsMethod · 0.95
SetStdLibMethod · 0.95
AddExtensionsMethod · 0.95
HasLibraryMethod · 0.95
VariablesMethod · 0.95
SetContextVariableMethod · 0.95
HasFeatureMethod · 0.95
AddVariableDeclsMethod · 0.95
AddFunctionDeclsMethod · 0.95

Tested by 2

TestEnvToConfigFunction · 0.76