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

Function DeclareContextProto

cel/options.go:828–853  ·  view source on GitHub ↗

DeclareContextProto returns an option to extend CEL environment with declarations from the given context proto. Each field of the proto defines a variable of the same name in the environment. https://github.com/google/cel-spec/blob/master/doc/langdef.md#evaluation-environment If using JSONFieldName

(descriptor protoreflect.MessageDescriptor)

Source from the content-addressed store, hash-verified

826//
827// If using JSONFieldNames(), ensure that the option is set before DeclareContextProto is provided.
828func DeclareContextProto(descriptor protoreflect.MessageDescriptor) EnvOption {
829 return func(e *Env) (*Env, error) {
830 if e.contextProto != nil {
831 return nil, fmt.Errorf("context proto already declared as %q, got %q",
832 e.contextProto.FullName(), descriptor.FullName())
833 }
834 e.contextProto = descriptor
835 fields := descriptor.Fields()
836 vars := make([]*decls.VariableDecl, 0, fields.Len())
837 jsonFieldNames := e.HasFeature(featureJSONFieldNames)
838 for i := 0; i < fields.Len(); i++ {
839 field := fields.Get(i)
840 variable, err := fieldToVariable(field, jsonFieldNames)
841 if err != nil {
842 return nil, err
843 }
844 vars = append(vars, variable)
845 }
846 var err error
847 e, err = VariableDecls(vars...)(e)
848 if err != nil {
849 return nil, err
850 }
851 return Types(dynamicpb.NewMessage(descriptor))(e)
852 }
853}
854
855// ContextProtoVars uses the fields of the input proto.Messages as top-level variables within an Activation.
856//

Callers 5

configToEnvOptionsFunction · 0.85
TestEnvToConfigFunction · 0.85
TestContextProtoFunction · 0.85

Calls 7

fieldToVariableFunction · 0.85
VariableDeclsFunction · 0.85
HasFeatureMethod · 0.80
TypesFunction · 0.70
FieldsMethod · 0.65
LenMethod · 0.65
GetMethod · 0.65

Tested by 4

TestEnvToConfigFunction · 0.68
TestContextProtoFunction · 0.68