Skip to content

Commit

Permalink
Refactor the API for v1 (#226)
Browse files Browse the repository at this point in the history
It's a bit ugly but seems like our best option since Java doesn't have
any concept of `internal` packages. This mirrors the Go protovalidate
package structure changes we did in
bufbuild/protovalidate-go#173.

The primary goal of this PR is to reduce the surface area of incidental
public API. We don't want to expose our internal logic since it is very
likely to be refactored in incompatible ways, but many things in the
`internal` package are currently publicly accessible.
  • Loading branch information
jchadwick-buf authored Jan 23, 2025
1 parent 229c084 commit 967e898
Show file tree
Hide file tree
Showing 36 changed files with 57 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.validate.AnyRules;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.expression;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.CompilationException;
import com.google.api.expr.v1alpha1.Type;
import org.projectnessie.cel.Ast;
import org.projectnessie.cel.Env;

/** {@link AstExpression} is a compiled CEL {@link Ast}. */
public class AstExpression {
class AstExpression {
/** The compiled CEL AST. */
public final Ast ast;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.protovalidate.internal.expression.CompiledProgram;
import build.buf.protovalidate.internal.expression.Variable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.expression;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.evaluator.Value;
import build.buf.validate.FieldPath;
import javax.annotation.Nullable;
import org.projectnessie.cel.Program;
Expand All @@ -27,7 +25,7 @@
* {@link CompiledProgram} is a parsed and type-checked {@link Program} along with the source {@link
* Expression}.
*/
public class CompiledProgram {
class CompiledProgram {
/** A compiled CEL program that can be evaluated against a set of variable bindings. */
private final Program program;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.constraints;
package build.buf.protovalidate;

import build.buf.protovalidate.Config;
import build.buf.protovalidate.exceptions.CompilationException;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.protovalidate.internal.evaluator.ObjectValue;
import build.buf.protovalidate.internal.evaluator.Value;
import build.buf.protovalidate.internal.expression.AstExpression;
import build.buf.protovalidate.internal.expression.CompiledProgram;
import build.buf.protovalidate.internal.expression.Expression;
import build.buf.protovalidate.internal.expression.Variable;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
import build.buf.validate.ValidateProto;
Expand Down Expand Up @@ -52,7 +44,7 @@
import org.projectnessie.cel.interpreter.Activation;

/** A build-through cache for computed standard constraints. */
public class ConstraintCache {
class ConstraintCache {
private static class CelRule {
public final AstExpression astExpression;
public final FieldDescriptor field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.CompilationException;
import build.buf.validate.FieldConstraints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.errors;
package build.buf.protovalidate;

import build.buf.protovalidate.Violation;
import build.buf.protovalidate.internal.evaluator.Value;
import build.buf.validate.FieldPath;
import build.buf.validate.FieldPathElement;
import com.google.protobuf.Descriptors;
Expand All @@ -31,7 +29,7 @@
* {@link ConstraintViolation} contains all of the collected information about an individual
* constraint violation.
*/
public class ConstraintViolation implements Violation {
class ConstraintViolation implements Violation {
/** Static value to return when there are no violations. */
public static final List<Builder> NO_VIOLATIONS = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.validate.FieldPath;
import build.buf.validate.FieldPathElement;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.celext;
package build.buf.protovalidate;

import com.google.api.expr.v1alpha1.Decl;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.celext;
package build.buf.protovalidate;

import com.google.common.base.Ascii;
import com.google.common.base.Splitter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.constraints;
package build.buf.protovalidate;

import build.buf.validate.FieldConstraints;
import com.google.api.expr.v1alpha1.Type;
Expand All @@ -27,7 +27,7 @@
/**
* DescriptorMappings provides mappings between protocol buffer descriptors and CEL declarations.
*/
public final class DescriptorMappings {
final class DescriptorMappings {
/** Provides a {@link Descriptor} for {@link FieldConstraints}. */
static final Descriptor FIELD_CONSTRAINTS_DESC = FieldConstraints.getDescriptor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import java.util.Collections;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.ValidationResult;
import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.validate.EnumRules;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import java.util.List;

/**
* {@link Evaluator} defines a validation evaluator. evaluator implementations may elide type
* checking of the passed in value, as the types have been guaranteed during the build phase.
*/
public interface Evaluator {
interface Evaluator {
/**
* Tautology returns true if the evaluator always succeeds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.Config;
import build.buf.protovalidate.exceptions.CompilationException;
import build.buf.protovalidate.internal.constraints.ConstraintCache;
import build.buf.protovalidate.internal.constraints.DescriptorMappings;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.protovalidate.internal.expression.AstExpression;
import build.buf.protovalidate.internal.expression.CompiledProgram;
import build.buf.protovalidate.internal.expression.Expression;
import build.buf.protovalidate.internal.expression.Variable;
import build.buf.validate.Constraint;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
Expand Down Expand Up @@ -50,7 +42,7 @@
import org.projectnessie.cel.checker.Decls;

/** A build-through cache of message evaluators keyed off the provided descriptor. */
public class EvaluatorBuilder {
class EvaluatorBuilder {
private static final FieldPathElement CEL_FIELD_PATH_ELEMENT =
FieldPathUtils.fieldPathElement(
FieldConstraints.getDescriptor().findFieldByNumber(FieldConstraints.CEL_FIELD_NUMBER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.expression;
package build.buf.protovalidate;

import build.buf.validate.Constraint;
import java.util.ArrayList;
import java.util.List;

/** Expression represents a single CEL expression. */
public class Expression {
class Expression {
/** The id of the constraint. */
public final String id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
import com.google.protobuf.Descriptors.FieldDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.errors;
package build.buf.protovalidate;

import build.buf.validate.FieldPath;
import build.buf.validate.FieldPathElement;
Expand All @@ -21,7 +21,7 @@
import javax.annotation.Nullable;

/** Utility class for manipulating error paths in violations. */
public final class FieldPathUtils {
final class FieldPathUtils {
private FieldPathUtils() {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.celext;
package build.buf.protovalidate;

import static java.time.format.DateTimeFormatter.ISO_INSTANT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
import build.buf.validate.FieldPathElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import build.buf.protovalidate.internal.errors.FieldPathUtils;
import build.buf.validate.FieldConstraints;
import build.buf.validate.FieldPath;
import build.buf.validate.FieldPathElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import build.buf.protovalidate.exceptions.ExecutionException;
import build.buf.protovalidate.internal.errors.ConstraintViolation;
import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.evaluator;
package build.buf.protovalidate;

import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
Expand All @@ -21,11 +21,8 @@
import java.util.Map;
import javax.annotation.Nullable;

/**
* The {@link build.buf.protovalidate.internal.evaluator.Value} type that contains a {@link
* com.google.protobuf.Message}.
*/
public final class MessageValue implements Value {
/** The {@link Value} type that contains a {@link com.google.protobuf.Message}. */
final class MessageValue implements Value {

/** Object type since the object type is inferred from the field descriptor. */
private final Object value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate.internal.expression;
package build.buf.protovalidate;

import java.time.Instant;
import javax.annotation.Nullable;
Expand All @@ -24,7 +24,7 @@
* {@link NowVariable} implements {@link Activation}, providing a lazily produced timestamp for
* accessing the variable `now` that's constant within an evaluation.
*/
public class NowVariable implements Activation {
class NowVariable implements Activation {
/** The name of the 'now' variable. */
private static final String NOW_NAME = "now";

Expand Down
Loading

0 comments on commit 967e898

Please sign in to comment.