use of com.google.protobuf.Descriptors.FieldDescriptor.JavaType in project core-java by SpineEventEngine.
the class AlternativeFieldValidator method validate.
List<? extends ConstraintViolation> validate(Message message) {
final Map<FieldDescriptor, Object> options = messageDescriptor.getOptions().getAllFields();
for (FieldDescriptor optionDescriptor : options.keySet()) {
if (OPTION_REQUIRED_FIELD.equals(optionDescriptor.getName())) {
final JavaType optionType = optionDescriptor.getJavaType();
if (optionType == JavaType.STRING) {
final String requiredFieldExpression = (String) options.get(optionDescriptor);
final ImmutableList<RequiredFieldOption> fieldOptions = parse(requiredFieldExpression);
if (!alternativeFound(message, fieldOptions)) {
final String msgFormat = "None of the fields match the `required_field` definition: %s";
ConstraintViolation requiredFieldNotFound = ConstraintViolation.newBuilder().setMsgFormat(msgFormat).addParam(requiredFieldExpression).build();
violations.add(requiredFieldNotFound);
}
} else {
log().warn("`{}` is not of string type. Found: {}", OPTION_REQUIRED_FIELD, optionType);
}
}
}
return violations.build();
}
use of com.google.protobuf.Descriptors.FieldDescriptor.JavaType in project core-java by SpineEventEngine.
the class Sample method valueFor.
/**
* Generates a non-default value for the given message field.
*
* <p>All the protobuf types are supported including nested {@link Message}s and
* the {@code enum}s.
*
* @param field {@link FieldDescriptor} to take the type info from
* @return a non-default generated value of type of the given field
*/
@SuppressWarnings("OverlyComplexMethod")
private static Object valueFor(FieldDescriptor field) {
final Type type = field.getType();
final JavaType javaType = type.getJavaType();
final Random random = new SecureRandom();
switch(javaType) {
case INT:
return random.nextInt();
case LONG:
return random.nextLong();
case FLOAT:
return random.nextFloat();
case DOUBLE:
return random.nextDouble();
case BOOLEAN:
return random.nextBoolean();
case STRING:
final byte[] bytes = new byte[8];
random.nextBytes(bytes);
return new String(bytes);
case BYTE_STRING:
final byte[] bytesPrimitive = new byte[8];
random.nextBytes(bytesPrimitive);
return ByteString.copyFrom(bytesPrimitive);
case ENUM:
return enumValueFor(field, random);
case MESSAGE:
return messageValueFor(field);
default:
throw new IllegalArgumentException(format("Field type %s is not supported.", type));
}
}
Aggregations