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

Method ToConfig

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

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