Search in sources :

Example 16 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class MessageField method getAccessor.

private Method getAccessor(Message message) {
    final Class<? extends Message> messageClass = message.getClass();
    Method method = accessors.get(messageClass);
    if (method == null) {
        final FieldDescriptor fieldDescriptor = getFieldDescriptor(message, this.index);
        final String fieldName = fieldDescriptor.getName();
        final String methodName = toAccessorMethodName(fieldName);
        try {
            method = messageClass.getMethod(methodName);
            method.setAccessible(true);
            accessors.put(messageClass, method);
        } catch (NoSuchMethodException e) {
            throw new IllegalStateException(e);
        }
    }
    return method;
}
Also used : Method(java.lang.reflect.Method) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 17 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class AlternativeFieldValidator method checkField.

private boolean checkField(Message message, String fieldName) {
    final FieldDescriptor field = messageDescriptor.findFieldByName(fieldName);
    if (field == null) {
        ConstraintViolation notFound = ConstraintViolation.newBuilder().setMsgFormat("Field %s not found").addParam(fieldName).build();
        violations.add(notFound);
        return false;
    }
    Object fieldValue = message.getField(field);
    final FieldValidator<?> fieldValidator = FieldValidatorFactory.createStrict(field, fieldValue, rootFieldPath);
    final List<ConstraintViolation> violations = fieldValidator.validate();
    return violations.isEmpty();
}
Also used : FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 18 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor 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 19 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class MessageFieldShould method return_field_descriptor.

@Test
public void return_field_descriptor() {
    final FieldDescriptor descriptor = getFieldDescriptor(stringValue, STR_VALUE_FIELD_INDEX);
    assertEquals(JavaType.STRING, descriptor.getJavaType());
}
Also used : MessageField.getFieldDescriptor(io.spine.protobuf.MessageField.getFieldDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 20 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class ReferenceValidator method findSourceFieldsByNames.

private Collection<FieldDescriptor> findSourceFieldsByNames(String[] names, FieldDescriptor enrichmentField) {
    checkArgument(names.length > 0, "Names may not be empty");
    checkArgument(names.length > 1, "Enrichment target field names may not be a singleton array. " + "Use findSourceFieldByName().");
    final Collection<FieldDescriptor> result = new HashSet<>(names.length);
    FieldDescriptor.Type basicType = null;
    Descriptor messageType = null;
    for (String name : names) {
        final FieldDescriptor field = findSourceFieldByName(name, enrichmentField, false);
        if (field == null) {
            /* We don't know at this stage the type of the event.
                   The enrichment is to be included anyway,
                   but by other ReferenceValidator instance */
            continue;
        }
        if (basicType == null) {
            // Get type of the first field
            basicType = field.getType();
            if (basicType == MESSAGE) {
                messageType = field.getMessageType();
            }
        } else {
            // Compare the type with each of the next
            checkState(basicType == field.getType(), differentTypesErrorMessage(enrichmentField));
            if (basicType == MESSAGE) {
                checkState(messageType.equals(field.getMessageType()), differentTypesErrorMessage(enrichmentField));
            }
        }
        final boolean noDuplicateFiled = result.add(field);
        checkState(noDuplicateFiled, "Enrichment target field names may contain no duplicates. " + "Found duplicate field: %s", name);
    }
    return result;
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) HashSet(java.util.HashSet) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)45 Descriptor (com.google.protobuf.Descriptors.Descriptor)18 Test (org.junit.Test)14 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)11 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 BooleanFieldValidator (io.spine.validate.BooleanFieldValidator)9 ByteStringFieldValidator (io.spine.validate.ByteStringFieldValidator)9 FloatFieldValidator (io.spine.validate.FloatFieldValidator)9 LongFieldValidator (io.spine.validate.LongFieldValidator)9 StringFieldValidator (io.spine.validate.StringFieldValidator)9 TypicalData (protos.TypicalData)5 ArrayList (java.util.ArrayList)4 ByteString (com.google.protobuf.ByteString)3 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)3 ValidationResult (io.spine.server.event.enrich.ReferenceValidator.ValidationResult)3 Map (java.util.Map)3 GeneratedMessage (com.google.protobuf.GeneratedMessage)2 Collection (java.util.Collection)2 TypicalDataMessage (protos.TypicalDataMessage)2 ImageAnnotatorClient (com.google.cloud.vision.spi.v1.ImageAnnotatorClient)1