Skip to content

Commit ad2d011

Browse files
Rename constraint -> rule globally (#91)
Following the change from protovalidate v0.11.0 onward, the term "constraint" is now removed in places where it referred to protovalidate rules, using the following nomenclature: - ConstraintRule -> ValidationRule - Constraint -> Rule etc. This is an API breaking change, since names of public structures have changed.
1 parent a254193 commit ad2d011

16 files changed

+308
-316
lines changed

buf/validate/internal/BUILD.bazel

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package(default_visibility = ["//:__subpackages__"])
1616

1717
cc_library(
18-
name = "cel_constraint_rules",
19-
srcs = ["cel_constraint_rules.cc"],
20-
hdrs = ["cel_constraint_rules.h"],
18+
name = "cel_validation_rules",
19+
srcs = ["cel_validation_rules.cc"],
20+
hdrs = ["cel_validation_rules.h"],
2121
deps = [
22-
":constraint_rules",
22+
":validation_rules",
2323
"@com_google_cel_cpp//eval/public:activation",
2424
"@com_google_cel_cpp//eval/public:cel_expression",
2525
"@com_google_cel_cpp//eval/public:cel_value",
@@ -43,8 +43,8 @@ cc_library(
4343
)
4444

4545
cc_library(
46-
name = "constraint_rules",
47-
hdrs = ["constraint_rules.h"],
46+
name = "validation_rules",
47+
hdrs = ["validation_rules.h"],
4848
deps = [
4949
"@com_github_bufbuild_protovalidate//proto/protovalidate/buf/validate:validate_proto_cc",
5050
":proto_field",
@@ -93,18 +93,18 @@ cc_library(
9393
hdrs = ["field_rules.h"],
9494
deps = [
9595
":cel_rules",
96-
":constraint",
96+
":rules",
9797
"@com_google_absl//absl/status:statusor",
9898
"@com_google_protobuf//:protobuf",
9999
],
100100
)
101101

102102
cc_library(
103-
name = "constraint",
104-
srcs = ["constraints.cc"],
105-
hdrs = ["constraints.h"],
103+
name = "rules",
104+
srcs = ["rules.cc"],
105+
hdrs = ["rules.h"],
106106
deps = [
107-
":cel_constraint_rules",
107+
":cel_validation_rules",
108108
":extra_func",
109109
"@com_github_bufbuild_protovalidate//proto/protovalidate/buf/validate:validate_proto_cc",
110110
"@com_google_cel_cpp//eval/public:activation",
@@ -119,10 +119,10 @@ cc_library(
119119
)
120120

121121
cc_test(
122-
name = "constraint_test",
123-
srcs = ["constraints_test.cc"],
122+
name = "rules_test",
123+
srcs = ["rules_test.cc"],
124124
deps = [
125-
":constraint",
125+
":rules",
126126
"@com_google_cel_cpp//eval/public:activation",
127127
"@com_google_googletest//:gtest_main",
128128
],

buf/validate/internal/cel_rules.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#pragma once
1616

1717
#include "absl/status/status.h"
18-
#include "buf/validate/internal/cel_constraint_rules.h"
19-
#include "buf/validate/internal/constraints.h"
18+
#include "buf/validate/internal/cel_validation_rules.h"
2019
#include "buf/validate/internal/message_factory.h"
20+
#include "buf/validate/internal/rules.h"
2121
#include "buf/validate/validate.pb.h"
2222
#include "google/protobuf/arena.h"
2323
#include "google/protobuf/descriptor.h"
@@ -64,8 +64,8 @@ absl::Status BuildCelRules(
6464
google::protobuf::Arena* arena,
6565
google::api::expr::runtime::CelExpressionBuilder& builder,
6666
const R& rules,
67-
CelConstraintRules& result) {
68-
// Look for constraints on the set fields.
67+
CelValidationRules& result) {
68+
// Look for rules on the set fields.
6969
std::vector<const google::protobuf::FieldDescriptor*> fields;
7070
google::protobuf::Message* reparsedRules{};
7171
if (messageFactory && rules.unknown_fields().field_count() > 0) {
@@ -81,14 +81,13 @@ absl::Status BuildCelRules(
8181
if (!allowUnknownFields &&
8282
!reparsedRules->GetReflection()->GetUnknownFields(*reparsedRules).empty()) {
8383
return absl::FailedPreconditionError(
84-
absl::StrCat("unknown constraints in ", reparsedRules->GetTypeName()));
84+
absl::StrCat("unknown rules in ", reparsedRules->GetTypeName()));
8585
}
8686
result.setRules(reparsedRules, arena);
8787
reparsedRules->GetReflection()->ListFields(*reparsedRules, &fields);
8888
} else {
8989
if (!allowUnknownFields && !R::GetReflection()->GetUnknownFields(rules).empty()) {
90-
return absl::FailedPreconditionError(
91-
absl::StrCat("unknown constraints in ", rules.GetTypeName()));
90+
return absl::FailedPreconditionError(absl::StrCat("unknown rules in ", rules.GetTypeName()));
9291
}
9392
result.setRules(&rules, arena);
9493
R::GetReflection()->ListFields(rules, &fields);
@@ -102,9 +101,9 @@ absl::Status BuildCelRules(
102101
continue;
103102
}
104103
const auto& fieldLvl = field->options().GetExtension(buf::validate::predefined);
105-
for (const auto& constraint : fieldLvl.cel()) {
106-
auto status = result.Add(
107-
builder, constraint.id(), constraint.message(), constraint.expression(), rulePath, field);
104+
for (const auto& rule : fieldLvl.cel()) {
105+
auto status =
106+
result.Add(builder, rule.id(), rule.message(), rule.expression(), rulePath, field);
108107
if (!status.ok()) {
109108
return status;
110109
}

buf/validate/internal/cel_constraint_rules.cc renamed to buf/validate/internal/cel_validation_rules.cc

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "buf/validate/internal/cel_constraint_rules.h"
15+
#include "buf/validate/internal/cel_validation_rules.h"
1616

1717
#include "common/values/struct_value.h"
1818
#include "eval/public/containers/field_access.h"
@@ -26,21 +26,21 @@ namespace cel = google::api::expr;
2626

2727
namespace {
2828

29-
absl::Status ProcessConstraint(
30-
ConstraintContext& ctx,
29+
absl::Status ProcessRule(
30+
RuleContext& ctx,
3131
const google::api::expr::runtime::BaseActivation& activation,
32-
const CompiledConstraint& expr) {
33-
auto result_or= expr.expr->Evaluate(activation, ctx.arena);
32+
const CompiledRule& expr) {
33+
auto result_or = expr.expr->Evaluate(activation, ctx.arena);
3434
if (!result_or.ok()) {
3535
return result_or.status();
3636
}
3737
cel::runtime::CelValue result = std::move(result_or).value();
3838
if (result.IsBool()) {
3939
if (!result.BoolOrDie()) {
40-
// Add violation with the constraint message.
40+
// Add violation with the rule message.
4141
Violation violation;
42-
violation.set_message(expr.constraint.message());
43-
violation.set_rule_id(expr.constraint.id());
42+
violation.set_message(expr.rule.message());
43+
violation.set_rule_id(expr.rule.id());
4444
if (expr.rulePath.has_value()) {
4545
*violation.mutable_rule() = *expr.rulePath;
4646
}
@@ -51,7 +51,7 @@ absl::Status ProcessConstraint(
5151
// Add violation with custom message.
5252
Violation violation;
5353
violation.set_message(std::string(result.StringOrDie().value()));
54-
violation.set_rule_id(expr.constraint.id());
54+
violation.set_rule_id(expr.rule.id());
5555
if (expr.rulePath.has_value()) {
5656
*violation.mutable_rule() = *expr.rulePath;
5757
}
@@ -87,12 +87,12 @@ cel::runtime::CelValue ProtoFieldToCelValue(
8787

8888
} // namespace
8989

90-
absl::Status CelConstraintRules::Add(
90+
absl::Status CelValidationRules::Add(
9191
google::api::expr::runtime::CelExpressionBuilder& builder,
92-
Rule constraint,
92+
Rule rule,
9393
absl::optional<FieldPath> rulePath,
94-
const google::protobuf::FieldDescriptor* rule) {
95-
auto pexpr_or = cel::parser::Parse(constraint.expression());
94+
const google::protobuf::FieldDescriptor* ruleField) {
95+
auto pexpr_or = cel::parser::Parse(rule.expression());
9696
if (!pexpr_or.ok()) {
9797
return pexpr_or.status();
9898
}
@@ -102,40 +102,40 @@ absl::Status CelConstraintRules::Add(
102102
return expr_or.status();
103103
}
104104
std::unique_ptr<cel::runtime::CelExpression> expr = std::move(expr_or).value();
105-
exprs_.emplace_back(CompiledConstraint{std::move(constraint), std::move(expr), std::move(rulePath), rule});
105+
exprs_.emplace_back(
106+
CompiledRule{std::move(rule), std::move(expr), std::move(rulePath), ruleField});
106107
return absl::OkStatus();
107108
}
108109

109-
absl::Status CelConstraintRules::Add(
110+
absl::Status CelValidationRules::Add(
110111
google::api::expr::runtime::CelExpressionBuilder& builder,
111112
std::string_view id,
112113
std::string_view message,
113114
std::string_view expression,
114115
absl::optional<FieldPath> rulePath,
115-
const google::protobuf::FieldDescriptor* rule) {
116-
Rule constraint;
117-
*constraint.mutable_id() = id;
118-
*constraint.mutable_message() = message;
119-
*constraint.mutable_expression() = expression;
120-
return Add(builder, constraint, std::move(rulePath), rule);
116+
const google::protobuf::FieldDescriptor* ruleField) {
117+
Rule rule;
118+
*rule.mutable_id() = id;
119+
*rule.mutable_message() = message;
120+
*rule.mutable_expression() = expression;
121+
return Add(builder, rule, std::move(rulePath), ruleField);
121122
}
122123

123-
absl::Status CelConstraintRules::ValidateCel(
124-
ConstraintContext& ctx,
125-
google::api::expr::runtime::Activation& activation) const {
124+
absl::Status CelValidationRules::ValidateCel(
125+
RuleContext& ctx, google::api::expr::runtime::Activation& activation) const {
126126
activation.InsertValue("rules", rules_);
127127
activation.InsertValue("now", cel::runtime::CelValue::CreateTimestamp(absl::Now()));
128128
absl::Status status = absl::OkStatus();
129129

130130
for (const auto& expr : exprs_) {
131-
if (rules_.IsMessage() && expr.rule != nullptr) {
131+
if (rules_.IsMessage() && expr.ruleField != nullptr) {
132132
activation.InsertValue(
133-
"rule", ProtoFieldToCelValue(rules_.MessageOrDie(), expr.rule, ctx.arena));
133+
"rule", ProtoFieldToCelValue(rules_.MessageOrDie(), expr.ruleField, ctx.arena));
134134
}
135135
int pos = ctx.violations.size();
136-
status = ProcessConstraint(ctx, activation, expr);
137-
if (rules_.IsMessage() && expr.rule != nullptr && ctx.violations.size() > pos) {
138-
ctx.setRuleValue(ProtoField{rules_.MessageOrDie(), expr.rule}, pos);
136+
status = ProcessRule(ctx, activation, expr);
137+
if (rules_.IsMessage() && expr.ruleField != nullptr && ctx.violations.size() > pos) {
138+
ctx.setRuleValue(ProtoField{rules_.MessageOrDie(), expr.ruleField}, pos);
139139
}
140140
if (ctx.shouldReturn(status)) {
141141
break;
@@ -146,7 +146,7 @@ absl::Status CelConstraintRules::ValidateCel(
146146
return status;
147147
}
148148

149-
void CelConstraintRules::setRules(
149+
void CelValidationRules::setRules(
150150
const google::protobuf::Message* rules, google::protobuf::Arena* arena) {
151151
rules_ = cel::runtime::CelProtoWrapper::CreateMessage(rules, arena);
152152
}

buf/validate/internal/cel_constraint_rules.h renamed to buf/validate/internal/cel_validation_rules.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,52 @@
1616

1717
#include <string_view>
1818

19+
#include "buf/validate/internal/validation_rules.h"
1920
#include "buf/validate/validate.pb.h"
20-
#include "buf/validate/internal/constraint_rules.h"
2121
#include "eval/public/activation.h"
2222
#include "eval/public/cel_expression.h"
2323
#include "eval/public/cel_value.h"
2424

2525
namespace buf::validate::internal {
2626

27-
// A compiled constraint expression.
28-
struct CompiledConstraint {
29-
buf::validate::Rule constraint;
27+
// A compiled rule expression.
28+
struct CompiledRule {
29+
buf::validate::Rule rule;
3030
std::unique_ptr<google::api::expr::runtime::CelExpression> expr;
3131
const absl::optional<FieldPath> rulePath;
32-
const google::protobuf::FieldDescriptor* rule;
32+
const google::protobuf::FieldDescriptor* ruleField;
3333
};
3434

35-
// An abstract base class for constraint with rules that are compiled into CEL expressions.
36-
class CelConstraintRules : public ConstraintRules {
37-
using Base = ConstraintRules;
35+
// An abstract base class for rules that are compiled into CEL expressions.
36+
class CelValidationRules : public ValidationRules {
37+
using Base = ValidationRules;
3838

3939
public:
4040
using Base::Base;
4141

4242
absl::Status Add(
4343
google::api::expr::runtime::CelExpressionBuilder& builder,
44-
Rule constraint,
44+
Rule rule,
4545
absl::optional<FieldPath> rulePath,
46-
const google::protobuf::FieldDescriptor* rule);
46+
const google::protobuf::FieldDescriptor* ruleField);
4747
absl::Status Add(
4848
google::api::expr::runtime::CelExpressionBuilder& builder,
4949
std::string_view id,
5050
std::string_view message,
5151
std::string_view expression,
5252
absl::optional<FieldPath> rulePath,
53-
const google::protobuf::FieldDescriptor* rule);
53+
const google::protobuf::FieldDescriptor* ruleField);
5454

5555
// Validate all the cel rules given the activation that already has 'this' bound.
5656
absl::Status ValidateCel(
57-
ConstraintContext& ctx,
58-
google::api::expr::runtime::Activation& activation) const;
57+
RuleContext& ctx, google::api::expr::runtime::Activation& activation) const;
5958

6059
void setRules(google::api::expr::runtime::CelValue rules) { rules_ = rules; }
6160
void setRules(const google::protobuf::Message* rules, google::protobuf::Arena* arena);
6261

6362
protected:
6463
google::api::expr::runtime::CelValue rules_;
65-
std::vector<CompiledConstraint> exprs_;
64+
std::vector<CompiledRule> exprs_;
6665
};
6766

6867
} // namespace buf::validate::internal

0 commit comments

Comments
 (0)