Skip to content

Commit ffffaa6

Browse files
jnthntatumcopybara-github
authored andcommitted
Add a cel::Source overload for cel::Compiler.
PiperOrigin-RevId: 934555794
1 parent 16074dc commit ffffaa6

4 files changed

Lines changed: 57 additions & 18 deletions

File tree

compiler/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cc_library(
2525
"//checker:type_checker",
2626
"//checker:type_checker_builder",
2727
"//checker:validation_result",
28+
"//common:source",
2829
"//parser:options",
2930
"//parser:parser_interface",
3031
"//validator",

compiler/compiler.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "checker/type_checker.h"
2828
#include "checker/type_checker_builder.h"
2929
#include "checker/validation_result.h"
30+
#include "common/source.h"
3031
#include "parser/options.h"
3132
#include "parser/parser_interface.h"
3233
#include "validator/validator.h"
@@ -129,9 +130,18 @@ class Compiler {
129130
public:
130131
virtual ~Compiler() = default;
131132

132-
virtual absl::StatusOr<ValidationResult> Compile(
133+
absl::StatusOr<ValidationResult> Compile(
134+
const Source& source, google::protobuf::Arena* absl_nullable arena) const {
135+
return CompileImpl(source, arena);
136+
}
137+
138+
absl::StatusOr<ValidationResult> Compile(const Source& source) const {
139+
return CompileImpl(source, nullptr);
140+
}
141+
142+
absl::StatusOr<ValidationResult> Compile(
133143
absl::string_view source, absl::string_view description,
134-
google::protobuf::Arena* absl_nullable arena) const = 0;
144+
google::protobuf::Arena* absl_nullable arena) const;
135145

136146
absl::StatusOr<ValidationResult> Compile(absl::string_view source) const {
137147
return Compile(source, "<input>", nullptr);
@@ -159,8 +169,27 @@ class Compiler {
159169
// The returned builder does not share state with the compiler and may be
160170
// modified independently.
161171
virtual std::unique_ptr<CompilerBuilder> ToBuilder() const = 0;
172+
173+
protected:
174+
virtual absl::StatusOr<ValidationResult> CompileImpl(
175+
const Source& source, google::protobuf::Arena* absl_nullable arena) const = 0;
162176
};
163177

178+
inline absl::StatusOr<ValidationResult> Compiler::Compile(
179+
absl::string_view source, absl::string_view description,
180+
google::protobuf::Arena* absl_nullable arena) const {
181+
absl::StatusOr<SourcePtr> source_obj =
182+
NewSource(source, std::string(description));
183+
if (!source_obj.ok()) {
184+
return source_obj.status();
185+
}
186+
absl::StatusOr<ValidationResult> result = CompileImpl(**source_obj, arena);
187+
if (result.ok()) {
188+
result->SetSource(std::move(*source_obj));
189+
}
190+
return result;
191+
}
192+
164193
} // namespace cel
165194

166195
#endif // THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_

compiler/compiler_factory.cc

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ class CompilerImpl : public Compiler {
5454
validator_(std::move(validator)),
5555
options_(options) {}
5656

57-
absl::StatusOr<ValidationResult> Compile(
58-
absl::string_view expression, absl::string_view description,
59-
google::protobuf::Arena* arena) const override {
60-
CEL_ASSIGN_OR_RETURN(auto source,
61-
cel::NewSource(expression, std::string(description)));
57+
std::unique_ptr<CompilerBuilder> ToBuilder() const override;
58+
59+
const TypeChecker& GetTypeChecker() const override { return *type_checker_; }
60+
const Parser& GetParser() const override { return *parser_; }
61+
const Validator& GetValidator() const override { return validator_; }
62+
63+
protected:
64+
absl::StatusOr<ValidationResult> CompileImpl(
65+
const Source& source, google::protobuf::Arena* arena) const override {
6266
std::vector<cel::ParseIssue> parse_issues;
6367
absl::StatusOr<std::unique_ptr<cel::Ast>> ast =
64-
parser_->Parse(*source, &parse_issues);
68+
parser_->Parse(source, &parse_issues);
6569
if (!ast.ok()) {
6670
if (!options_.adapt_parser_errors ||
6771
ast.status().code() != absl::StatusCode::kInvalidArgument ||
@@ -74,26 +78,17 @@ class CompilerImpl : public Compiler {
7478
check_issues.push_back(TypeCheckIssue::CreateError(
7579
issue.location(), std::string(issue.message())));
7680
}
77-
ValidationResult result(std::move(check_issues));
78-
result.SetSource(std::move(source));
79-
return result;
81+
return ValidationResult(std::move(check_issues));
8082
}
8183
CEL_ASSIGN_OR_RETURN(ValidationResult result,
8284
type_checker_->Check(*std::move(ast), arena));
8385

84-
result.SetSource(std::move(source));
8586
if (!validator_.validations().empty()) {
8687
validator_.UpdateValidationResult(result);
8788
}
8889
return result;
8990
}
9091

91-
std::unique_ptr<CompilerBuilder> ToBuilder() const override;
92-
93-
const TypeChecker& GetTypeChecker() const override { return *type_checker_; }
94-
const Parser& GetParser() const override { return *parser_; }
95-
const Validator& GetValidator() const override { return validator_; }
96-
9792
private:
9893
std::unique_ptr<TypeChecker> type_checker_;
9994
std::unique_ptr<Parser> parser_;

compiler/compiler_factory_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,5 +427,19 @@ TEST(CompilerFactoryTest, ReturnsIssuesFromParser) {
427427
EXPECT_THAT(result.GetIssues(), testing::Not(testing::IsEmpty()));
428428
}
429429

430+
TEST(CompilerFactoryTest, CompileSourceOverload) {
431+
ASSERT_OK_AND_ASSIGN(
432+
auto builder,
433+
NewCompilerBuilder(cel::internal::GetSharedTestingDescriptorPool()));
434+
435+
ASSERT_THAT(builder->AddLibrary(StandardCompilerLibrary()), IsOk());
436+
ASSERT_OK_AND_ASSIGN(auto compiler, builder->Build());
437+
438+
ASSERT_OK_AND_ASSIGN(auto source, cel::NewSource("1 + 2"));
439+
ASSERT_OK_AND_ASSIGN(ValidationResult result, compiler->Compile(*source));
440+
441+
EXPECT_TRUE(result.IsValid());
442+
}
443+
430444
} // namespace
431445
} // namespace cel

0 commit comments

Comments
 (0)