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

Function configToEnvOptions

cel/options.go:569–687  ·  view source on GitHub ↗

configToEnvOptions generates a set of EnvOption values (or error) based on a config, a type provider, and an optional set of environment options.

(config *env.Config, provider types.Provider, optFactories []ConfigOptionFactory)

Source from the content-addressed store, hash-verified

567// configToEnvOptions generates a set of EnvOption values (or error) based on a config, a type provider,
568// and an optional set of environment options.
569func configToEnvOptions(config *env.Config, provider types.Provider, optFactories []ConfigOptionFactory) ([]EnvOption, error) {
570 envOpts := []EnvOption{}
571 // Configure the standard lib subset.
572 if config.StdLib != nil {
573 envOpts = append(envOpts, func(e *Env) (*Env, error) {
574 if e.HasLibrary("cel.lib.std") {
575 return nil, errors.New("invalid subset of stdlib: create a custom env")
576 }
577 return e, nil
578 })
579 if !config.StdLib.Disabled {
580 envOpts = append(envOpts, StdLib(StdLibSubset(config.StdLib)))
581 }
582 } else {
583 envOpts = append(envOpts, StdLib())
584 }
585
586 // Configure the container
587 if config.Container != "" {
588 envOpts = append(envOpts, Container(config.Container))
589 }
590
591 // Configure abbreviations
592 for _, imp := range config.Imports {
593 envOpts = append(envOpts, Abbrevs(imp.Name))
594 }
595
596 // Configure features and common limits.
597 for _, feat := range config.Features {
598 // Note, if a feature is not found, it is skipped as it is possible the feature
599 // is not intended to be supported publicly. In the future, a refinement of
600 // to this strategy to report unrecognized features and validators should probably
601 // be covered as a standard ConfigOptionFactory
602 if id, found := featureIDByName(feat.Name); found {
603 envOpts = append(envOpts, features(id, feat.Enabled))
604 }
605 }
606
607 // Configure the context variable declaration
608 if config.ContextVariable != nil {
609 typeName := config.ContextVariable.TypeName
610 if _, found := provider.FindStructType(typeName); !found {
611 return nil, fmt.Errorf("invalid context proto type: %q", typeName)
612 }
613 // Attempt to instantiate the proto in order to reflect to its descriptor
614 msg := provider.NewValue(typeName, map[string]ref.Val{})
615 pbMsg, ok := msg.Value().(proto.Message)
616 if !ok {
617 return nil, fmt.Errorf("unsupported context type: %T", msg.Value())
618 }
619 envOpts = append(envOpts, DeclareContextProto(pbMsg.ProtoReflect().Descriptor()))
620 }
621
622 // Configure variables
623 if len(config.Variables) != 0 {
624 vars := make([]*decls.VariableDecl, 0, len(config.Variables))
625 for _, v := range config.Variables {
626 vDef, err := v.AsCELVariable(provider)

Callers 1

FromConfigFunction · 0.85

Calls 15

StdLibFunction · 0.85
StdLibSubsetFunction · 0.85
featureIDByNameFunction · 0.85
featuresFunction · 0.85
DeclareContextProtoFunction · 0.85
VariableDeclsFunction · 0.85
FunctionDeclsFunction · 0.85
limitIDByNameFunction · 0.85
setLimitFunction · 0.85
ASTValidatorsFunction · 0.85
OptionalTypesFunction · 0.85

Tested by

no test coverage detected