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