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

Function configToEnvOptions

cel/options.go:628–746  ·  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

626// configToEnvOptions generates a set of EnvOption values (or error) based on a config, a type provider,
627// and an optional set of environment options.
628func configToEnvOptions(config *env.Config, provider types.Provider, optFactories []ConfigOptionFactory) ([]EnvOption, error) {
629 envOpts := []EnvOption{}
630 // Configure the standard lib subset.
631 if config.StdLib != nil {
632 envOpts = append(envOpts, func(e *Env) (*Env, error) {
633 if e.HasLibrary("cel.lib.std") {
634 return nil, errors.New("invalid subset of stdlib: create a custom env")
635 }
636 return e, nil
637 })
638 if !config.StdLib.Disabled {
639 envOpts = append(envOpts, StdLib(StdLibSubset(config.StdLib)))
640 }
641 } else {
642 envOpts = append(envOpts, StdLib())
643 }
644
645 // Configure the container
646 if config.Container != "" {
647 envOpts = append(envOpts, Container(config.Container))
648 }
649
650 // Configure abbreviations
651 for _, imp := range config.Imports {
652 envOpts = append(envOpts, Abbrevs(imp.Name))
653 }
654
655 // Configure features and common limits.
656 for _, feat := range config.Features {
657 // Note, if a feature is not found, it is skipped as it is possible the feature
658 // is not intended to be supported publicly. In the future, a refinement of
659 // to this strategy to report unrecognized features and validators should probably
660 // be covered as a standard ConfigOptionFactory
661 if id, found := featureIDByName(feat.Name); found {
662 envOpts = append(envOpts, features(id, feat.Enabled))
663 }
664 }
665
666 // Configure the context variable declaration
667 if config.ContextVariable != nil {
668 typeName := config.ContextVariable.TypeName
669 if _, found := provider.FindStructType(typeName); !found {
670 return nil, fmt.Errorf("invalid context proto type: %q", typeName)
671 }
672 // Attempt to instantiate the proto in order to reflect to its descriptor
673 msg := provider.NewValue(typeName, map[string]ref.Val{})
674 pbMsg, ok := msg.Value().(proto.Message)
675 if !ok {
676 return nil, fmt.Errorf("unsupported context type: %T", msg.Value())
677 }
678 envOpts = append(envOpts, DeclareContextProto(pbMsg.ProtoReflect().Descriptor()))
679 }
680
681 // Configure variables
682 if len(config.Variables) != 0 {
683 vars := make([]*decls.VariableDecl, 0, len(config.Variables))
684 for _, v := range config.Variables {
685 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
astValidatorFactoriesFunction · 0.85
ASTValidatorsFunction · 0.85

Tested by

no test coverage detected