| 76 | } |
| 77 | |
| 78 | absl::StatusOr<std::unique_ptr<cel::Compiler>> MakeConfiguredCompiler() { |
| 79 | // Setup for handling for protobuf types. |
| 80 | // Using the generated descriptor pool is simpler to configure, but often |
| 81 | // adds more types than necessary. |
| 82 | google::protobuf::LinkMessageReflection<AttributeContext>(); |
| 83 | CEL_ASSIGN_OR_RETURN( |
| 84 | std::unique_ptr<cel::CompilerBuilder> builder, |
| 85 | cel::NewCompilerBuilder(google::protobuf::DescriptorPool::generated_pool())); |
| 86 | CEL_RETURN_IF_ERROR(builder->AddLibrary(cel::StandardCompilerLibrary())); |
| 87 | // Adds fields of AttributeContext as variables. |
| 88 | CEL_RETURN_IF_ERROR(builder->GetCheckerBuilder().AddContextDeclaration( |
| 89 | AttributeContext::descriptor()->full_name())); |
| 90 | |
| 91 | // Codelab part 1: |
| 92 | // Add a declaration for the map<string, V>.contains(string, V) function. |
| 93 | auto& checker_builder = builder->GetCheckerBuilder(); |
| 94 | // Note: we use MakeMemberOverloadDecl instead of MakeOverloadDecl |
| 95 | // because the function is receiver style, meaning that it is called as |
| 96 | // e1.f(e2) instead of f(e1, e2). |
| 97 | CEL_ASSIGN_OR_RETURN( |
| 98 | cel::FunctionDecl decl, |
| 99 | cel::MakeFunctionDecl( |
| 100 | "contains", |
| 101 | cel::MakeMemberOverloadDecl( |
| 102 | "map_contains_string_string", cel::BoolType(), |
| 103 | cel::MapType(checker_builder.arena(), cel::StringType(), |
| 104 | cel::TypeParamType("V")), |
| 105 | cel::StringType(), cel::TypeParamType("V")))); |
| 106 | // Note: we use MergeFunction instead of AddFunction because we are adding |
| 107 | // an overload to an already declared function with the same name. |
| 108 | CEL_RETURN_IF_ERROR(checker_builder.MergeFunction(decl)); |
| 109 | return builder->Build(); |
| 110 | } |
| 111 | |
| 112 | class Evaluator { |
| 113 | public: |
no test coverage detected