Search in sources :

Example 51 with FieldDescriptor

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

the class MessageValidator method validateFields.

private void validateFields(Message message, ImmutableList.Builder<ConstraintViolation> result) {
    final Descriptor msgDescriptor = message.getDescriptorForType();
    final List<FieldDescriptor> fields = msgDescriptor.getFields();
    for (FieldDescriptor field : fields) {
        final Object value = message.getField(field);
        final FieldValidator<?> fieldValidator = FieldValidatorFactory.create(field, value, rootFieldPath);
        final List<ConstraintViolation> violations = fieldValidator.validate();
        result.addAll(violations);
    }
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 52 with FieldDescriptor

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

the class ReferenceValidatorShould method skip_mapping_if_no_mapping_function_is_defined.

@Test
public void skip_mapping_if_no_mapping_function_is_defined() {
    final Enricher<?, ?> mockEnricher = mock(Enricher.class);
    when(mockEnricher.functionFor(any(Class.class), any(Class.class))).thenReturn(Optional.<EnrichmentFunction<?, ?, ?>>absent());
    final ReferenceValidator validator = new ReferenceValidator(mockEnricher, UserDeletedEvent.class, EnrichmentBoundWithMultipleFieldsWithDifferentNames.class);
    final ValidationResult result = validator.validate();
    final List<EnrichmentFunction<?, ?, ?>> functions = result.getFunctions();
    assertTrue(functions.isEmpty());
    final Multimap<FieldDescriptor, FieldDescriptor> fields = result.getFieldMap();
    assertEmpty(fields);
}
Also used : ValidationResult(io.spine.server.outbus.enrich.ReferenceValidator.ValidationResult) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 53 with FieldDescriptor

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

the class ReferenceValidatorShould method store_valid_map_of_enrichment_fields_after_validation.

@Test
public void store_valid_map_of_enrichment_fields_after_validation() {
    final ReferenceValidator validator = new ReferenceValidator(enricher, UserDeletedEvent.class, EnrichmentBoundWithMultipleFieldsWithDifferentNames.class);
    final ValidationResult result = validator.validate();
    final Multimap<FieldDescriptor, FieldDescriptor> fieldMap = result.getFieldMap();
    assertNotNull(fieldMap);
    assertFalse(fieldMap.isEmpty());
    assertSize(1, fieldMap);
    final Iterator<? extends Map.Entry<?, ? extends Collection<?>>> fieldsIterator = fieldMap.asMap().entrySet().iterator();
    assertTrue(fieldsIterator.hasNext());
    final Map.Entry<?, ? extends Collection<?>> entry = fieldsIterator.next();
    @SuppressWarnings("unchecked") final Map.Entry<FieldDescriptor, Collection<FieldDescriptor>> fieldEntry = (Map.Entry<FieldDescriptor, Collection<FieldDescriptor>>) entry;
    final FieldDescriptor eventField = fieldEntry.getKey();
    final String eventFieldName = eventField.getName();
    assertEquals("deleted_uid", eventFieldName);
    final Collection<FieldDescriptor> enrichmentFields = fieldEntry.getValue();
    assertFalse(enrichmentFields.isEmpty());
    assertSize(1, enrichmentFields);
    final Iterator<FieldDescriptor> enrichmentFieldIterator = enrichmentFields.iterator();
    assertTrue(enrichmentFieldIterator.hasNext());
    final FieldDescriptor enrichmentField = enrichmentFieldIterator.next();
    final String enrichmentFieldName = enrichmentField.getName();
    assertEquals(USER_GOOGLE_UID_FIELD, enrichmentFieldName);
}
Also used : ValidationResult(io.spine.server.outbus.enrich.ReferenceValidator.ValidationResult) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Collection(java.util.Collection) Map(java.util.Map) Test(org.junit.Test)

Example 54 with FieldDescriptor

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

the class Field method getFieldClass.

/**
 * Returns the class of the Protobuf message field.
 *
 * @param field the field descriptor
 * @return the class of the field
 * @throws IllegalArgumentException if the field type is unknown
 */
// as each branch is a fairly simple.
@SuppressWarnings("OverlyComplexMethod")
public static Class<?> getFieldClass(FieldDescriptor field) {
    checkNotNull(field);
    final FieldDescriptor.JavaType javaType = field.getJavaType();
    final TypeUrl typeUrl;
    switch(javaType) {
        case INT:
            return Integer.class;
        case LONG:
            return Long.class;
        case FLOAT:
            return Float.class;
        case DOUBLE:
            return Double.class;
        case BOOLEAN:
            return Boolean.class;
        case STRING:
            return String.class;
        case BYTE_STRING:
            return ByteString.class;
        case ENUM:
            typeUrl = TypeUrl.from(field.getEnumType());
            final Class<? extends Message> enumClass = typeUrl.getJavaClass();
            return enumClass;
        case MESSAGE:
            typeUrl = TypeUrl.from(field.getMessageType());
            final Class<? extends Message> msgClass = typeUrl.getJavaClass();
            return msgClass;
        default:
            throw newIllegalArgumentException("Unknown field type discovered: %s", field.getFullName());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) TypeUrl(io.spine.type.TypeUrl) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 55 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) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) HashSet(java.util.HashSet)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)58 Descriptor (com.google.protobuf.Descriptors.Descriptor)22 Test (org.junit.Test)17 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 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 TypicalData (protos.TypicalData)5 Collection (java.util.Collection)4 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)3 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)3 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)3 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)3 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)3 ByteString (com.google.protobuf.ByteString)3