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

Method ToConfig

cel/env.go:177–332  ·  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

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

Callers 1

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 1

TestEnvToConfigFunction · 0.76