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

Function EnvironmentFile

tools/compiler/compiler.go:224–268  ·  view source on GitHub ↗

EnvironmentFile returns an EnvOption which loads a serialized CEL environment from a file. The file must be in one of the following formats: - Textproto - YAML - Binarypb

(path string)

Source from the content-addressed store, hash-verified

222// - YAML
223// - Binarypb
224func EnvironmentFile(path string) cel.EnvOption {
225 return func(e *cel.Env) (*cel.Env, error) {
226 format := InferFileFormat(path)
227 if format != TextProto && format != TextYAML && format != BinaryProto {
228 return nil, fmt.Errorf("file extension must be one of .textproto, .yaml, .binarypb: found %v", format)
229 }
230 var envConfig *env.Config
231 var fileDescriptorSet *descpb.FileDescriptorSet
232 switch format {
233 case TextProto, BinaryProto:
234 pbEnv := &configpb.Environment{}
235 var err error
236 if err = loadProtoFile(path, format, pbEnv); err != nil {
237 return nil, err
238 }
239 envConfig, fileDescriptorSet, err = envProtoToConfig(pbEnv)
240 if err != nil {
241 return nil, err
242 }
243 case TextYAML:
244 envConfig = &env.Config{}
245 data, err := loadFile(path)
246 if err != nil {
247 return nil, err
248 }
249 err = yaml.Unmarshal(data, envConfig)
250 if err != nil {
251 return nil, fmt.Errorf("yaml.Unmarshal failed to map CEL environment: %w", err)
252 }
253 default:
254 return nil, fmt.Errorf("unsupported format: %v, file: %s", format, path)
255 }
256 var envOpts []cel.EnvOption
257 if fileDescriptorSet != nil {
258 envOpts = append(envOpts, cel.TypeDescs(fileDescriptorSet))
259 }
260 envOpts = append(envOpts, cel.FromConfig(envConfig, ext.ExtensionOptionFactory))
261 var err error
262 e, err = e.Extend(envOpts...)
263 if err != nil {
264 return nil, fmt.Errorf("e.Extend() with env options %v failed: %w", envOpts, err)
265 }
266 return e, nil
267 }
268}
269
270func envProtoToConfig(pbEnv *configpb.Environment) (*env.Config, *descpb.FileDescriptorSet, error) {
271 if pbEnv == nil {

Callers 9

TestCoverageStatsFunction · 0.92
TestTriggerTestsFunction · 0.92
runTestSuiteFunction · 0.92
parseEnvFunction · 0.85

Calls 7

TypeDescsFunction · 0.92
FromConfigFunction · 0.92
InferFileFormatFunction · 0.85
loadProtoFileFunction · 0.85
envProtoToConfigFunction · 0.85
loadFileFunction · 0.85
ExtendMethod · 0.45

Tested by 8

TestCoverageStatsFunction · 0.74
TestTriggerTestsFunction · 0.74
runTestSuiteFunction · 0.74
parseEnvFunction · 0.68