Search in sources :

Example 1 with JavaType

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();
}
Also used : JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 2 with JavaType

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));
    }
}
Also used : Type(com.google.protobuf.Descriptors.FieldDescriptor.Type) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) Random(java.util.Random) SecureRandom(java.security.SecureRandom) SecureRandom(java.security.SecureRandom) ByteString(com.google.protobuf.ByteString)

Aggregations

JavaType (com.google.protobuf.Descriptors.FieldDescriptor.JavaType)2 ByteString (com.google.protobuf.ByteString)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 Type (com.google.protobuf.Descriptors.FieldDescriptor.Type)1 SecureRandom (java.security.SecureRandom)1 Random (java.util.Random)1