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

Function DeclareContextProto

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

882//
883// If using JSONFieldNames(), ensure that the option is set before DeclareContextProto is provided.
884func DeclareContextProto(descriptor protoreflect.MessageDescriptor) EnvOption {
885 return func(e *Env) (*Env, error) {
886 if e.contextProto != nil {
887 return nil, fmt.Errorf("context proto already declared as %q, got %q",
888 e.contextProto.FullName(), descriptor.FullName())
889 }
890 e.contextProto = descriptor
891 fields := descriptor.Fields()
892 vars := make([]*decls.VariableDecl, 0, fields.Len())
893 jsonFieldNames := e.HasFeature(featureJSONFieldNames)
894 for i := 0; i < fields.Len(); i++ {
895 field := fields.Get(i)
896 variable, err := fieldToVariable(field, jsonFieldNames)
897 if err != nil {
898 return nil, err
899 }
900 vars = append(vars, variable)
901 }
902 var err error
903 e, err = VariableDecls(vars...)(e)
904 if err != nil {
905 return nil, err
906 }
907 return Types(dynamicpb.NewMessage(descriptor))(e)
908 }
909}
910
911// ContextProtoVars uses the fields of the input proto.Messages as top-level variables within an Activation.
912//

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