Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ object CoreJvmCompiler {
/**
* The version used to in the build classpath.
*/
const val dogfoodingVersion = "2.0.0-SNAPSHOT.042"
const val dogfoodingVersion = "2.0.0-SNAPSHOT.050"

/**
* The version to be used for integration tests.
*/
const val version = "2.0.0-SNAPSHOT.042"
const val version = "2.0.0-SNAPSHOT.050"

/**
* The ID of the Gradle plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Validation {
/**
* The version of the Validation library artifacts.
*/
const val version = "2.0.0-SNAPSHOT.383"
const val version = "2.0.0-SNAPSHOT.390"

/**
* The last version of Validation compatible with ProtoData.
Expand Down
188 changes: 30 additions & 158 deletions dependencies.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion jvm-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ dependencies {
compileOnly(AutoService.annotations)

implementation(Base.lib)
implementation(Logging.lib)

testImplementation(TestLib.lib)
}
Expand Down
40 changes: 12 additions & 28 deletions jvm-runtime/src/main/java/io/spine/validation/FieldConstraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import com.google.common.collect.ImmutableSet;
import io.spine.code.proto.FieldContext;
import io.spine.logging.Logger;
import io.spine.logging.LoggingFactory;
import io.spine.validation.option.FieldValidatingOption;
import io.spine.validation.option.StandardOptionFactory;
import io.spine.validation.option.ValidatingOptionFactory;
Expand All @@ -41,14 +39,12 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.lang.String.format;

/**
* A factory of field validation {@link Constraint}s.
*/
final class FieldConstraints {

private static final Logger logger = LoggingFactory.forEnclosingClass();
private static final ImmutableSet<ValidatingOptionFactory> allFactories =
ValidatingOptionsLoader.INSTANCE.implementations();
private static final ImmutableSet<ValidatingOptionFactory> customFactories =
Expand Down Expand Up @@ -87,30 +83,18 @@ static Stream<Constraint> customConstraintsFor(FieldContext field) {
checkNotNull(field);
var declaration = field.targetDeclaration();
var type = declaration.javaType();
switch (type) {
case INT:
return constraintsFrom(factories, ValidatingOptionFactory::forInt, field);
case LONG:
return constraintsFrom(factories, ValidatingOptionFactory::forLong, field);
case FLOAT:
return constraintsFrom(factories, ValidatingOptionFactory::forFloat, field);
case DOUBLE:
return constraintsFrom(factories, ValidatingOptionFactory::forDouble, field);
case BOOLEAN:
return constraintsFrom(factories, ValidatingOptionFactory::forBoolean, field);
case STRING:
return constraintsFrom(factories, ValidatingOptionFactory::forString, field);
case BYTE_STRING:
return constraintsFrom(factories, ValidatingOptionFactory::forByteString, field);
case ENUM:
return constraintsFrom(factories, ValidatingOptionFactory::forEnum, field);
case MESSAGE:
return constraintsFrom(factories, ValidatingOptionFactory::forMessage, field);
default:
logger.atWarning()
.log(() -> format("Unknown field type `%s` at `%s`.", type, declaration));
return Stream.of();
}
return switch (type) {
case INT -> constraintsFrom(factories, ValidatingOptionFactory::forInt, field);
case LONG -> constraintsFrom(factories, ValidatingOptionFactory::forLong, field);
case FLOAT -> constraintsFrom(factories, ValidatingOptionFactory::forFloat, field);
case DOUBLE -> constraintsFrom(factories, ValidatingOptionFactory::forDouble, field);
case BOOLEAN -> constraintsFrom(factories, ValidatingOptionFactory::forBoolean, field);
case STRING -> constraintsFrom(factories, ValidatingOptionFactory::forString, field);
case BYTE_STRING ->
constraintsFrom(factories, ValidatingOptionFactory::forByteString, field);
case ENUM -> constraintsFrom(factories, ValidatingOptionFactory::forEnum, field);
case MESSAGE -> constraintsFrom(factories, ValidatingOptionFactory::forMessage, field);
};
}

private static Stream<Constraint>
Expand Down
21 changes: 10 additions & 11 deletions jvm-runtime/src/main/java/io/spine/validation/Validate.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.InlineMe;
import com.google.protobuf.Any;
import com.google.protobuf.Message;
import io.spine.annotation.Internal;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.FieldDeclaration;
import io.spine.logging.Logger;
import io.spine.logging.LoggingFactory;
import io.spine.protobuf.Diff;
import io.spine.type.KnownTypes;
import io.spine.type.MessageType;
Expand All @@ -51,18 +48,20 @@
import static io.spine.protobuf.AnyPacker.unpack;
import static io.spine.validation.RuntimeErrorPlaceholder.FIELD_PATH;
import static io.spine.validation.RuntimeErrorPlaceholder.PARENT_TYPE;
import static java.lang.String.format;

/**
* This class provides general validation routines.
*/
@SuppressWarnings("UseOfSystemOutOrSystemErr" /*
We do not want the dependency of Validation Runtime on Spine Logging.
So we use `System.err` for warnings and errors. */
)
public final class Validate {

private static final String SET_ONCE_ERROR_MESSAGE =
"Attempted to change the value of the field " +
"`${" + PARENT_TYPE + "}.${" + FIELD_PATH + "}` which has " +
"`(set_once) = true` and already has a non-default value.";
private static final Logger logger = LoggingFactory.forEnclosingClass();

/** Prevents instantiation of this utility class. */
private Validate() {
Expand Down Expand Up @@ -98,9 +97,9 @@ public static List<ConstraintViolation> violationsOf(Message message) {
if (KnownTypes.instance().contains(TypeUrl.ofEnclosed(packed))) {
msg = unpack(packed);
} else {
logger.atWarning().log(() -> format(
"Could not validate packed message of an unknown type `%s`.",
packed.getTypeUrl()));
System.err.printf(
"Could not validate packed message of an unknown type `%s`.%n",
packed.getTypeUrl());
}
}
if (msg instanceof ValidatableMessage validatable) {
Expand Down Expand Up @@ -262,10 +261,10 @@ private static boolean markedSetOnce(FieldDeclaration declaration) {

private static void onSetOnceMisuse(FieldDeclaration field) {
var fieldName = field.name();
logger.atError().log(() -> format(
System.err.printf(
"Error found in `%s`. " +
"Repeated and map fields cannot be marked as `(set_once) = true`.",
fieldName));
"Repeated and map fields cannot be marked as `(set_once) = true`.%n",
fieldName);
}

private static ConstraintViolation violatedSetOnce(FieldDeclaration declaration) {
Expand Down
14 changes: 7 additions & 7 deletions jvm-runtime/src/main/java/io/spine/validation/option/Goes.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.errorprone.annotations.Immutable;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.FieldDeclaration;
import io.spine.logging.WithLogging;
import io.spine.option.GoesOption;
import io.spine.option.OptionsProto;
import io.spine.validation.Constraint;
Expand All @@ -41,8 +40,7 @@
*/
@Immutable
public final class Goes
extends FieldValidatingOption<GoesOption>
implements WithLogging {
extends FieldValidatingOption<GoesOption> {

private Goes() {
super(OptionsProto.goes);
Expand All @@ -64,7 +62,7 @@ && canBeRequired(field)
&& canPairedBeRequired(field);
}

private boolean canBeRequired(FieldContext context) {
private static boolean canBeRequired(FieldContext context) {
var field = context.targetDeclaration();
var warning = format(
"Field `%s` cannot be checked for presence. `(goes).with` is obsolete.",
Expand All @@ -90,13 +88,15 @@ private boolean canPairedBeRequired(FieldContext context) {
return checkType(pairedField, warningMessage);
}

@SuppressWarnings("FloggerLogString")
private boolean checkType(FieldDeclaration field, String warningMessage) {
@SuppressWarnings("UseOfSystemOutOrSystemErr") /* We're migrating off runtime validation. AND
we do not want the dependency of Validation Runtime on Spine Logging.
So we use `System.err` for warnings. */
private static boolean checkType(FieldDeclaration field, String warningMessage) {
var type = field.javaType();
if (field.isCollection() || Required.CAN_BE_REQUIRED.contains(type)) {
return true;
} else {
logger().atWarning().log(() -> warningMessage);
System.err.println(warningMessage);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,21 @@
import io.spine.base.EntityState;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.FieldDeclaration;
import io.spine.logging.WithLogging;
import io.spine.option.OptionsProto;
import io.spine.validation.Constraint;

import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.BYTE_STRING;
import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.ENUM;
import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.MESSAGE;
import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.STRING;
import static java.lang.String.format;

/**
* An option that makes a field {@code required}.
*
* <p>If a {@code required} field is missing, an error is produced.
*/
@Immutable
public class Required extends FieldValidatingOption<Boolean> implements WithLogging {
public class Required extends FieldValidatingOption<Boolean> {

static final ImmutableSet<JavaType> CAN_BE_REQUIRED = ImmutableSet.of(
MESSAGE, ENUM, STRING, BYTE_STRING
Expand Down Expand Up @@ -100,6 +98,9 @@ public boolean shouldValidate(FieldContext context) {
* @param field
* a value that the option is applied to
*/
@SuppressWarnings("UseOfSystemOutOrSystemErr") /* We're migrating off runtime validation
AND we do not want dependency of Validation Runtime on Spine Logging.
So we use `System.err` for the warnings. */
void checkUsage(FieldDeclaration field) {
var type = field.javaType();
if (!CAN_BE_REQUIRED.contains(type) && field.isNotCollection()) {
Expand All @@ -116,10 +117,10 @@ void checkUsage(FieldDeclaration field) {
}
}
var typeName = field.descriptor().getType().name();
logger().atWarning().log(() -> format(
System.err.printf(
"The field `%s.%s` has the type %s and" +
" should not be declared as `(required)`.",
field.declaringType().name(), field.name(), typeName));
" should not be declared as `(required)`.%n",
field.declaringType().name(), field.name(), typeName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import io.spine.annotation.Internal
public class NonPrimitiveOptionFactory : StandardOptionFactory {

override fun forString(): MutableSet<FieldValidatingOption<*>> {
return Sets.union<FieldValidatingOption<*>>(stringOptions, collectionOptions)
return Sets.union(stringOptions, collectionOptions)
}

override fun forByteString(): Set<FieldValidatingOption<*>> = collectionOptions
Expand Down
22 changes: 11 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ all modules and does not describe the project structure per-subproject.
-->
<groupId>io.spine.tools</groupId>
<artifactId>validation</artifactId>
<version>2.0.0-SNAPSHOT.390</version>
<version>2.0.0-SNAPSHOT.391</version>

<inceptionYear>2015</inceptionYear>

Expand Down Expand Up @@ -53,12 +53,6 @@ all modules and does not describe the project structure per-subproject.
<version>2.0.0-SNAPSHOT.384</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.spine</groupId>
<artifactId>spine-logging</artifactId>
<version>2.0.0-SNAPSHOT.411</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.spine</groupId>
<artifactId>spine-time</artifactId>
Expand All @@ -68,7 +62,7 @@ all modules and does not describe the project structure per-subproject.
<dependency>
<groupId>io.spine</groupId>
<artifactId>spine-validation-jvm-runtime</artifactId>
<version>2.0.0-SNAPSHOT.383</version>
<version>2.0.0-SNAPSHOT.390</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -167,6 +161,12 @@ all modules and does not describe the project structure per-subproject.
<version>6.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.spine</groupId>
<artifactId>spine-logging</artifactId>
<version>2.0.0-SNAPSHOT.411</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.spine.tools</groupId>
<artifactId>compiler-api</artifactId>
Expand Down Expand Up @@ -281,12 +281,12 @@ all modules and does not describe the project structure per-subproject.
<dependency>
<groupId>io.spine.tools</groupId>
<artifactId>core-jvm-gradle-plugins</artifactId>
<version>2.0.0-SNAPSHOT.042</version>
<version>2.0.0-SNAPSHOT.050</version>
</dependency>
<dependency>
<groupId>io.spine.tools</groupId>
<artifactId>core-jvm-routing</artifactId>
<version>2.0.0-SNAPSHOT.042</version>
<version>2.0.0-SNAPSHOT.050</version>
</dependency>
<dependency>
<groupId>io.spine.tools</groupId>
Expand All @@ -301,7 +301,7 @@ all modules and does not describe the project structure per-subproject.
<dependency>
<groupId>io.spine.tools</groupId>
<artifactId>validation-java-bundle</artifactId>
<version>2.0.0-SNAPSHOT.383</version>
<version>2.0.0-SNAPSHOT.390</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
Expand Down
2 changes: 1 addition & 1 deletion version.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
*
* For Spine-based dependencies please see [io.spine.dependency.local.Spine].
*/
val validationVersion by extra("2.0.0-SNAPSHOT.390")
val validationVersion by extra("2.0.0-SNAPSHOT.391")
Loading