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)
| 826 | // |
| 827 | // If using JSONFieldNames(), ensure that the option is set before DeclareContextProto is provided. |
| 828 | func 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 | // |