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
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 2 additions & 13 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.code.gen.java;
package io.spine.code.proto;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.Descriptors.OneofDescriptor;
import io.spine.code.java.ClassName;
import io.spine.code.java.SimpleClassName;
import io.spine.code.proto.FieldName;
import io.spine.type.MessageType;

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

/**
* A declaration of a {@code oneof} field.
* A declaration of a {@code oneof} field group.
*/
@Immutable
public final class OneofDeclaration {

private final OneofDescriptor oneof;
Expand All @@ -44,17 +40,6 @@ public OneofDeclaration(OneofDescriptor oneof, MessageType type) {
this.declaringType = checkNotNull(type);
}

public static ImmutableSet<OneofDeclaration> allFromType(MessageType declaringType) {
checkNotNull(declaringType);
ImmutableSet<OneofDeclaration> result =
declaringType.descriptor()
.getOneofs()
.stream()
.map(oneof -> new OneofDeclaration(oneof, declaringType))
.collect(toImmutableSet());
return result;
}

/**
* Obtains the name of the {@code oneof} field.
*/
Expand All @@ -63,21 +48,16 @@ public FieldName name() {
}

/**
* Obtains the name of an enum which represents cases of the {@code oneof} field.
*
* <p>Such an enum should be nested in the declaring message class.
*
* <p>If the declaring message class name is {@code com.acme.cms.Customer} and the {@code oneof}
* name is {@code auth_provider}, the resulting class name would be
* {@code com.acme.cms.Customer$AuthProviderCase}.
*
* @return the case enum FQN
* Obtains the {@code oneof} descriptor.
*/
public OneofDescriptor descriptor() {
return oneof;
}

/**
* Obtains the type containing this group.
*/
public ClassName javaCaseEnum() {
ClassName declaringClassName = declaringType.javaClassName();
io.spine.code.gen.java.FieldName oneofName =
io.spine.code.gen.java.FieldName.from(name());
SimpleClassName enumName = SimpleClassName.create(format("%sCase", oneofName.capitalize()));
return declaringClassName.withNested(enumName);
public MessageType declaringType() {
return declaringType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.spine.validate.option.DistinctConstraint;
import io.spine.validate.option.GoesConstraint;
import io.spine.validate.option.IsRequiredConstraint;
import io.spine.validate.option.PatternConstraint;
import io.spine.validate.option.RangedConstraint;
import io.spine.validate.option.RequiredConstraint;
Expand Down Expand Up @@ -101,6 +102,14 @@ public interface ConstraintTranslator<T> {
*/
void visitRequiredField(RequiredFieldConstraint constraint);

/**
* Translates the given {@link IsRequiredConstraint}.
*
* @param constraint
* the constraint of a oneof group
*/
void visitRequiredOneof(IsRequiredConstraint constraint);

/**
* Translates the given {@link CustomConstraint}.
*
Expand Down
15 changes: 15 additions & 0 deletions base/src/main/java/io/spine/validate/Constraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.OneofDeclaration;
import io.spine.type.MessageType;
import io.spine.validate.option.IsRequired;
import io.spine.validate.option.RequiredField;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -77,6 +79,7 @@ static Constraints loadFor(MessageType type, FieldContext context) {
.flatMap(FieldConstraints::of)
.forEach(constraintBuilder::add);
addRequiredField(type, constraintBuilder);
scanIsRequired(type, constraintBuilder);
return new Constraints(constraintBuilder.build());
}

Expand All @@ -89,6 +92,18 @@ private static void addRequiredField(MessageType type,
}
}

private static void scanIsRequired(MessageType type,
ImmutableList.Builder<Constraint> builder) {
IsRequired option = new IsRequired();
type.descriptor()
.getOneofs()
.stream()
.filter(option::valuePresent)
.map(descriptor -> new OneofDeclaration(descriptor, type))
.map(option::constraintFor)
.forEach(builder::add);
}

/**
* Assembles non-standard constraints from the given message type in the given field context.
*/
Expand Down
18 changes: 18 additions & 0 deletions base/src/main/java/io/spine/validate/MessageValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Range;
import com.google.protobuf.Message;
import io.spine.base.Field;
import io.spine.base.FieldPath;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.FieldDeclaration;
Expand All @@ -32,6 +33,7 @@
import io.spine.type.TypeName;
import io.spine.validate.option.DistinctConstraint;
import io.spine.validate.option.GoesConstraint;
import io.spine.validate.option.IsRequiredConstraint;
import io.spine.validate.option.PatternConstraint;
import io.spine.validate.option.RangedConstraint;
import io.spine.validate.option.RequiredConstraint;
Expand Down Expand Up @@ -190,6 +192,22 @@ public void visitRequiredField(RequiredFieldConstraint constraint) {
violation.ifPresent(violations::add);
}

@Override
public void visitRequiredOneof(IsRequiredConstraint constraint) {
Optional<FieldValue> fieldValue = message.valueOf(constraint.declaration());
boolean noneSet = !fieldValue.isPresent();
if (noneSet) {
MessageType targetType = constraint.targetType();
ConstraintViolation violation = ConstraintViolation
.newBuilder()
.setMsgFormat(constraint.errorMessage(message.context()))
.setFieldPath(Field.named(constraint.oneofName().value()).path())
.setTypeName(targetType.name().value())
.build();
violations.add(violation);
}
}

@Override
public void visitCustom(CustomConstraint constraint) {
ImmutableList<ConstraintViolation> violations = constraint.validate(message);
Expand Down
20 changes: 17 additions & 3 deletions base/src/main/java/io/spine/validate/MessageValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.protobuf.Message;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.FieldDeclaration;
import io.spine.code.proto.OneofDeclaration;
import io.spine.type.MessageType;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -147,10 +148,10 @@ public FieldValue valueOf(FieldDeclaration field) {
}

/**
* Obtains the value of a populated {@code Oneof} field.
* Obtains the value of a populated {@code oneof} field.
*
* @param oneof
* the {@code Oneof} descriptor
* the {@code oneof} descriptor
* @return a value of the populated field
* or {@code Optional.empty()} if the field was not populated
* @throws IllegalArgumentException
Expand All @@ -163,6 +164,20 @@ public Optional<FieldValue> valueOf(OneofDescriptor oneof) {
return valueOfNullable(field);
}

/**
* Obtains the value of a populated {@code oneof} field.
*
* @param oneof
* the {@code oneof} declaration
* @return a value of the populated field
* or {@code Optional.empty()} if the field was not populated
* @throws IllegalArgumentException
* if the if the message doesn't declare this oneof
*/
public Optional<FieldValue> valueOf(OneofDeclaration oneof) {
return valueOf(oneof.descriptor());
}

/** Returns the context of the message. */
@SuppressWarnings("unused")
FieldContext context() {
Expand All @@ -188,5 +203,4 @@ private FieldValue valueOfField(FieldDescriptor field) {
private Object readValue(FieldDescriptor field) {
return asFieldAware == null ? message.getField(field) : asFieldAware.readValue(field);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,34 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.validate.option;

import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.Descriptors.OneofDescriptor;
import io.spine.code.proto.OneofDeclaration;
import io.spine.validate.Constraint;

import java.util.Optional;

import static io.spine.option.OptionsProto.isRequired;

/**
* Tests of validating builders are placed into this package
* to simulate usage of the public API.
* A {@code oneof} validation option which constrains the target {@code oneof} group to be set.
*
* <p>If the value of the option is {@code true}, one of the fields in the group must be set.
*/
@CheckReturnValue
@ParametersAreNonnullByDefault
package io.spine.validate.builders;
@Immutable
public class IsRequired implements ValidatingOption<Boolean, OneofDeclaration, OneofDescriptor> {

import com.google.errorprone.annotations.CheckReturnValue;
@Override
public Constraint constraintFor(OneofDeclaration field) {
return new IsRequiredConstraint(field);
}

import javax.annotation.ParametersAreNonnullByDefault;
@Override
public Optional<Boolean> valueFrom(OneofDescriptor descriptor) {
boolean value = descriptor.getOptions()
.getExtension(isRequired);
return value ? Optional.of(true) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.validate.option;

import com.google.errorprone.annotations.Immutable;
import io.spine.code.proto.FieldContext;
import io.spine.code.proto.FieldName;
import io.spine.code.proto.OneofDeclaration;
import io.spine.type.MessageType;
import io.spine.validate.Constraint;
import io.spine.validate.ConstraintTranslator;

/**
* A {@code oneof} group constraint which signifies that one of the fields must be set.
*/
@Immutable
public final class IsRequiredConstraint implements Constraint {

private final OneofDeclaration declaration;

IsRequiredConstraint(OneofDeclaration declaration) {
this.declaration = declaration;
}

@Override
public MessageType targetType() {
return declaration.declaringType();
}

@Override
public String errorMessage(FieldContext field) {
return String.format("One of fields in group `%s` must be set.", declaration.name());
}

@Override
public void accept(ConstraintTranslator<?> visitor) {
visitor.visitRequiredOneof(this);
}

/**
* Obtains the name of the {@code oneof} group.
*/
public FieldName oneofName() {
return declaration.name();
}

/**
* Obtains the {@code oneof} declaration.
*/
public OneofDeclaration declaration() {
return declaration;
}
}
Loading