Skip to content

Commit 546b7b6

Browse files
authored
Reduce visibility of previously internal package (#309)
As part of refactoring, the previous internal package (where the validator implementation is found) was refactored to the build.buf.protovalidate package. As part of this change, we should ensure that all previously internal classes are final (to prevent subclassing) and that methods not overridden from an interface use package private or lower visibility.
1 parent 636af52 commit 546b7b6

35 files changed

Lines changed: 114 additions & 133 deletions

src/main/java/build/buf/protovalidate/AnyEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* com.google.protobuf.Any}'s within an expression, breaking evaluation if the type is unknown at
3333
* runtime.
3434
*/
35-
class AnyEvaluator implements Evaluator {
35+
final class AnyEvaluator implements Evaluator {
3636
private final RuleViolationHelper helper;
3737
private final Descriptors.FieldDescriptor typeURLDescriptor;
3838
private final Set<String> in;

src/main/java/build/buf/protovalidate/AstExpression.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import dev.cel.compiler.CelCompiler;
2323

2424
/** {@link AstExpression} is a compiled CEL {@link CelAbstractSyntaxTree}. */
25-
class AstExpression {
25+
final class AstExpression {
2626
/** The compiled CEL AST. */
27-
public final CelAbstractSyntaxTree ast;
27+
final CelAbstractSyntaxTree ast;
2828

2929
/** Contains the original expression from the proto file. */
30-
public final Expression source;
30+
final Expression source;
3131

3232
/** Constructs a new {@link AstExpression}. */
3333
private AstExpression(CelAbstractSyntaxTree ast, Expression source) {
@@ -43,7 +43,7 @@ private AstExpression(CelAbstractSyntaxTree ast, Expression source) {
4343
* @return The compiled {@link AstExpression}.
4444
* @throws CompilationException if the expression compilation fails.
4545
*/
46-
public static AstExpression newAstExpression(CelCompiler cel, Expression expr)
46+
static AstExpression newAstExpression(CelCompiler cel, Expression expr)
4747
throws CompilationException {
4848
CelValidationResult compileResult = cel.compile(expr.expression);
4949
if (!compileResult.getAllIssues().isEmpty()) {

src/main/java/build/buf/protovalidate/CelPrograms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.jspecify.annotations.Nullable;
2222

2323
/** Evaluator that executes a {@link CompiledProgram}. */
24-
class CelPrograms implements Evaluator {
24+
final class CelPrograms implements Evaluator {
2525
private final RuleViolationHelper helper;
2626

2727
/** A list of {@link CompiledProgram} that will be executed against the input message. */

src/main/java/build/buf/protovalidate/CompiledProgram.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* {@link CompiledProgram} is a parsed and type-checked {@link Program} along with the source {@link
2626
* Expression}.
2727
*/
28-
class CompiledProgram {
28+
final class CompiledProgram {
2929
/** A compiled CEL program that can be evaluated against a set of variable bindings. */
3030
private final Program program;
3131

@@ -52,7 +52,7 @@ class CompiledProgram {
5252
* @param rulePath The field path from the FieldRules to the rule value.
5353
* @param ruleValue The rule value.
5454
*/
55-
public CompiledProgram(
55+
CompiledProgram(
5656
Program program,
5757
Expression source,
5858
@Nullable FieldPath rulePath,
@@ -74,7 +74,7 @@ public CompiledProgram(
7474
* violations.
7575
* @throws ExecutionException If the evaluation of the CEL program fails with an error.
7676
*/
77-
public RuleViolation.@Nullable Builder eval(Value fieldValue, CelVariableResolver variables)
77+
RuleViolation.@Nullable Builder eval(Value fieldValue, CelVariableResolver variables)
7878
throws ExecutionException {
7979
Object value;
8080
try {

src/main/java/build/buf/protovalidate/CustomOverload.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ static List<CelFunctionBinding> create() {
7474
}
7575

7676
/**
77-
* This implementes that standard {@code bytes_to_string} function. We override it because the CEL
77+
* This implements that standard {@code bytes_to_string} function. We override it because the CEL
7878
* library doesn't validate that the bytes are valid utf-8.
7979
*
80-
* <p>Workround until https://github.com/google/cel-java/pull/717 lands.
80+
* <p>Workaround until <a href="https://github.com/google/cel-java/pull/717">cel-java issue
81+
* 717</a> lands.
8182
*/
8283
private static CelFunctionBinding celBytesToString() {
8384
return CelFunctionBinding.from(
@@ -202,9 +203,7 @@ private static CelFunctionBinding celContainsBytes() {
202203
"contains_bytes",
203204
ByteString.class,
204205
ByteString.class,
205-
(receiver, param) -> {
206-
return bytesContains(receiver.toByteArray(), param.toByteArray());
207-
});
206+
(receiver, param) -> bytesContains(receiver.toByteArray(), param.toByteArray()));
208207
}
209208

210209
static boolean bytesContains(byte[] arr, byte[] subArr) {
@@ -285,9 +284,7 @@ private static CelFunctionBinding celIsIpPrefixInt() {
285284
"is_ip_prefix_int",
286285
String.class,
287286
Long.class,
288-
(prefix, version) -> {
289-
return isIpPrefix(prefix, version, false);
290-
});
287+
(prefix, version) -> isIpPrefix(prefix, version, false));
291288
}
292289

293290
/**
@@ -300,9 +297,7 @@ private static CelFunctionBinding celIsIpPrefixBool() {
300297
"is_ip_prefix_bool",
301298
String.class,
302299
Boolean.class,
303-
(prefix, strict) -> {
304-
return isIpPrefix(prefix, 0L, strict);
305-
});
300+
(prefix, strict) -> isIpPrefix(prefix, 0L, strict));
306301
}
307302

308303
/**
@@ -390,9 +385,9 @@ private static CelFunctionBinding celIsHostAndPort() {
390385
* <p>The host can be one of:
391386
*
392387
* <ul>
393-
* <li>An IPv4 address in dotted decimal format, for example "192.168.0.1".
394-
* <li>An IPv6 address enclosed in square brackets, for example "[::1]".
395-
* <li>A hostname, for example "example.com".
388+
* <li>An IPv4 address in dotted decimal format, for example {@code 192.168.0.1}.
389+
* <li>An IPv6 address enclosed in square brackets, for example {@code [::1]}.
390+
* <li>A hostname, for example {@code example.com}.
396391
* </ul>
397392
*
398393
* <p>The port is separated by a colon. It must be non-empty, with a decimal number in the range
@@ -570,7 +565,8 @@ static boolean isIp(String addr, long ver) {
570565
}
571566

572567
/**
573-
* Returns true if the string is a URI, for example "https://example.com/foo/bar?baz=quux#frag".
568+
* Returns true if the string is a URI, for example {@code
569+
* https://example.com/foo/bar?baz=quux#frag}.
574570
*
575571
* <p>URI is defined in the internet standard RFC 3986. Zone Identifiers in IPv6 address literals
576572
* are supported (RFC 6874).
@@ -580,8 +576,9 @@ private static boolean isUri(String str) {
580576
}
581577

582578
/**
583-
* Returns true if the string is a URI Reference - a URI such as
584-
* "https://example.com/foo/bar?baz=quux#frag", or a Relative Reference such as "./foo/bar?query".
579+
* Returns true if the string is a URI Reference - a URI such as {@code
580+
* https://example.com/foo/bar?baz=quux#frag}, or a Relative Reference such as {@code
581+
* ./foo/bar?query}.
585582
*
586583
* <p>URI, URI Reference, and Relative Reference are defined in the internet standard RFC 3986.
587584
* Zone Identifiers in IPv6 address literals are supported (RFC 6874).

src/main/java/build/buf/protovalidate/DescriptorMappings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private DescriptorMappings() {}
105105
* @return The rules field descriptor for the specified wrapper fully qualified name.
106106
*/
107107
@Nullable
108-
public static FieldDescriptor expectedWrapperRules(String fqn) {
108+
static FieldDescriptor expectedWrapperRules(String fqn) {
109109
switch (fqn) {
110110
case "google.protobuf.BoolValue":
111111
return EXPECTED_STANDARD_RULES.get(FieldDescriptor.Type.BOOL);
@@ -136,7 +136,7 @@ public static FieldDescriptor expectedWrapperRules(String fqn) {
136136
* @param kind The protobuf field type.
137137
* @return The corresponding CEL type for the protobuf field.
138138
*/
139-
public static CelType protoKindToCELType(FieldDescriptor.Type kind) {
139+
static CelType protoKindToCELType(FieldDescriptor.Type kind) {
140140
switch (kind) {
141141
case FLOAT:
142142
case DOUBLE:

src/main/java/build/buf/protovalidate/EmbeddedMessageEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21-
class EmbeddedMessageEvaluator implements Evaluator {
21+
final class EmbeddedMessageEvaluator implements Evaluator {
2222
private final RuleViolationHelper helper;
2323
private final MessageEvaluator messageEvaluator;
2424

src/main/java/build/buf/protovalidate/EnumEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* {@link EnumEvaluator} checks an enum value being a member of the defined values exclusively. This
2929
* check is handled outside CEL as enums are completely type erased to integers.
3030
*/
31-
class EnumEvaluator implements Evaluator {
31+
final class EnumEvaluator implements Evaluator {
3232
private final RuleViolationHelper helper;
3333

3434
/** Captures all the defined values for this enum */

src/main/java/build/buf/protovalidate/EvaluatorBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.jspecify.annotations.Nullable;
4444

4545
/** A build-through cache of message evaluators keyed off the provided descriptor. */
46-
class EvaluatorBuilder {
46+
final class EvaluatorBuilder {
4747
private static final FieldPathElement CEL_FIELD_PATH_ELEMENT =
4848
FieldPathUtils.fieldPathElement(
4949
FieldRules.getDescriptor().findFieldByNumber(FieldRules.CEL_FIELD_NUMBER));
@@ -150,8 +150,7 @@ private DescriptorCacheBuilder(
150150
* @return Unmodifiable map of descriptors to evaluators.
151151
* @throws CompilationException If an error occurs compiling a rule on the cache.
152152
*/
153-
public Map<Descriptor, MessageEvaluator> build(Descriptor descriptor)
154-
throws CompilationException {
153+
Map<Descriptor, MessageEvaluator> build(Descriptor descriptor) throws CompilationException {
155154
createMessageEvaluator(descriptor);
156155
return Collections.unmodifiableMap(cache);
157156
}

src/main/java/build/buf/protovalidate/Expression.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
import java.util.List;
2020

2121
/** Expression represents a single CEL expression. */
22-
class Expression {
22+
final class Expression {
2323
/** The id of the rule. */
24-
public final String id;
24+
final String id;
2525

2626
/** The message of the rule. */
27-
public final String message;
27+
final String message;
2828

2929
/** The expression of the rule. */
30-
public final String expression;
30+
final String expression;
3131

3232
/**
3333
* Constructs a new Expression.
@@ -57,7 +57,7 @@ private Expression(Rule rule) {
5757
* @param rules The list of rules.
5858
* @return The list of expressions.
5959
*/
60-
public static List<Expression> fromRules(List<build.buf.validate.Rule> rules) {
60+
static List<Expression> fromRules(List<build.buf.validate.Rule> rules) {
6161
List<Expression> expressions = new ArrayList<>();
6262
for (build.buf.validate.Rule rule : rules) {
6363
expressions.add(new Expression(rule));

0 commit comments

Comments
 (0)